Open In App

How to choose background color through color picker?

Last Updated : 05 Mar, 2021
Like Article

In this project, we are going to change the background color with the help of the Color Picker.

Glimpse of the Project:


  • Create an HTML file in which we are going to add the text and a color picker which helps to change the background color of our web-page.
  • Create a CSS style to give some animation effects to the web-page elements.
  • Create a JavaScript file for adding event-listeners that can detect the movement of the mouse.



  • Firstly, create an HTML file (index.html).
  • Then we link the CSS (style.css) file that provides all the animations effect to our HTML page. This is also placed in between the <head> tag.
  • We add a link from Google Fonts to use a different type of font family in our project.
  • Then we have to add an input tag so that we can use a color picker!
  • At the end of the body tag, we have to add 2 <script> tags. One for our index.js file and the other for the icon that we have used on our webpage.


<!DOCTYPE html>
<html lang="en">
    <link rel="stylesheet" href="style.css">
    <script src="index.js"></script>
    <script src=
    <h1 class="head">
      Choose the color from the selector
      to change the bg-color 
      <i class="fas fa-arrow-right">
    <input type="color" id="clr">

CSS: The following is the code for the “style.css” file used above in the HTML code. CSS is used to give different types of animations and effects to our HTML page so that it looks interactive to all users.

  • Restore all the browser effects.
  • Use classes and ids to give effects to HTML elements.
  • Use of :hover to use hover effects.
  • Use of @keyframes to use animations on our webpage.


/* restoring all the default 
   properties of the browser */
    margin: 0;
    padding: 0;
    box-sizing: border-box;
/* all similar effects of the web page 
   so we applied them to the body */
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
/* css effects for the color picker */
    margin: 2em;
    width: 3em;
    height: 3em;
    border: .4em solid black;
    border-radius: 20%;
    outline: none;
    cursor: pointer;
    box-shadow: 0 0 .5em #111;
/* for the icon that we have added */
    margin: 1em;
/* hover effects on color picker */
    animation: round 2s infinite;
/* animation for our color picker */
@keyframes round{
        transform: rotate(0deg);
        transform: rotate(60deg);
        transform: rotate(120deg);
        transform: rotate(180deg);
        transform: rotate(240deg);
        transform: rotate(300deg);
        transform: rotate(360deg);

JavaScript: In this, we write code for the detection of mouse movement for changing the background color.

Returns the first element that matches with specified CSS selector:
document.querySelector(‘css_selector’) =>

Returns all the elements that matches with specified HTML ID:
document.getElementbyId(‘html_id’) =>


// Selection of id's and classes from html document
const bgclr = document.getElementById("clr");
const headingg = document.querySelector(".head");
// Here we are adding event listener which 
// is used to detect the mouse movement
bgclr.addEventListener("input", () => {
  // This updates the background color which is 
  // picked by the user from the picker = bgclr.value;
  // This is the conditional statement that is used
  // to change the text color from BLACK to WHITE
  // when the background color changes to dark!
  if (
    bgclr.value.includes("00") ||
    bgclr.value.includes("0a") ||
    bgclr.value.includes("0b") ||
    bgclr.value.includes("0c") ||
    bgclr.value.includes("0d") ||
    bgclr.value.includes("0e") ||
  ) { = "#fff";
  } else { = "#000";


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads