Open In App

How to choose background color through color picker?

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

Glimpse of the Project:



Approach:

Example:



HTML:




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

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.




/* 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 */
body{
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
  
/* css effects for the color picker */
input{
    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 */
.fas{
    margin: 1em;
}
  
/* hover effects on color picker */
input:hover{
    animation: round 2s infinite;
}
  
/* animation for our color picker */
@keyframes round{
    0%{
        transform: rotate(0deg);
    }
    10%{
        transform: rotate(60deg);
    }
    20%{
        transform: rotate(120deg);
    }
    40%{
        transform: rotate(180deg);
    }
    60%{
        transform: rotate(240deg);
    }
    80%{
        transform: rotate(300deg);
    }
    100%{
        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
  document.body.style.backgroundColor = 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") ||
    bgclr.value.includes("0f")
  ) {
    headingg.style.color = "#fff";
  } else {
    headingg.style.color = "#000";
  }
});

Output:


Article Tags :