How to randomly change image color using HTML CSS and JavaScript ? Last Updated : 05 Aug, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will create a webpage where we can change image color randomly by clicking on the image using HTML, CSS, and JavaScript. The image color will change randomly using the JavaScript Math random() function.Approach:First of all select your image using HTML <img> tag.In JavaScript, use the Math random() function to select colors randomly.We will use event listeners to change image color. The event listener is used to change random color for the background color of the given image.HTML Code:Create an HTML file.Create a body and select your image using the HTML <img> src.Inside the HTML body tag, create an HTML div and give an id to this. HTML <!DOCTYPE html> <html> <head> <title>random image color change</title> </head> <body> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20220803165300/geeksimage-200x145.png"> <div id="divID"></div> </body> </html> CSS Code: We use CSS mix-blend-mode property to blend element content with parent content and background. CSS * { margin: 0; padding: 0; } #divID { position: absolute; top: 0; width: 100%; height: 100vh; mix-blend-mode: hue; } body { overflow: hidden; } img { height: 100vh; width: 100%; } JavaScript Code:First we use Math Floor() to select colors randomly.After that, we will add a click event listener to change the color. JavaScript const divElem = document.getElementById("divID"); function randomcolor() { return Math.floor(Math.random() * 255); } divElem.addEventListener('click', () => { divElem.style.backgroundColor = "rgba('+randomcolor()+','+randomcolor()+','+randomcolor()+'\)" }) Complete Code: HTML <!DOCTYPE html> <html> <head> <title>Random image color</title> <style> * { margin: 0; padding: 0; } #divID { position: absolute; top: 0; width: 100%; height: 100vh; mix-blend-mode: hue; } body { overflow: hidden; } img { height: 100vh; width: 100%; } </style> </head> <body> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20220803165300/geeksimage-200x145.png"> <div id="divID"></div> <script> const divElem = document.getElementById("divID"); function randomcolor() { return Math.floor(Math.random() * 255); } divElem.addEventListener('click', () => { divElem.style.backgroundColor = 'rgba(' + randomcolor() + ',' + randomcolor() + ',' + randomcolor() + '\)' }) </script> </body> </html> Output: Click on the image to change color randomly. How to randomly change image color using HTML CSS and JavaScript ? Comment S shreyanshporwal469 Follow Improve S shreyanshporwal469 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Projects Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like