How to choose background color through color picker?
Last Updated :
05 Mar, 2021
In this project, we are going to change the background color with the help of the Color Picker.
Glimpse of the Project:
Approach:
- 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.
Example:
HTML:
- 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.
index.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.
- 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.
style.css
*{
margin : 0 ;
padding : 0 ;
box-sizing: border-box;
}
body{
height : 100 vh;
display : flex;
justify- content : center ;
align-items: center ;
}
input{
margin : 2em ;
width : 3em ;
height : 3em ;
border : . 4em solid black ;
border-radius: 20% ;
outline : none ;
cursor : pointer ;
box-shadow: 0 0 . 5em #111 ;
}
.fas{
margin : 1em ;
}
input:hover{
animation: round 2 s infinite;
}
@keyframes round{
0% {
transform: rotate( 0 deg);
}
10% {
transform: rotate( 60 deg);
}
20% {
transform: rotate( 120 deg);
}
40% {
transform: rotate( 180 deg);
}
60% {
transform: rotate( 240 deg);
}
80% {
transform: rotate( 300 deg);
}
100% {
transform: rotate( 360 deg);
}
}
|
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’) =>
index.js
const bgclr = document.getElementById( "clr" );
const headingg = document.querySelector( ".head" );
bgclr.addEventListener( "input" , () => {
document.body.style.backgroundColor = bgclr.value;
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...