Hello Everyone,
This is a Quick Tutorial that describes how to dynamically adjust background color when you choose a color from your custom color picker. CSS variables are the key concept for this tutorial.Demo
index.html
<!DOCTYPE html>
<html>
<head>
<!-- google fonts -->
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Fredoka+One&display=swap" rel="stylesheet">
<!--import style sheet-->
<link href="style.css" rel="stylesheet">
</head>
Here, you can see that I've already added two google fonts and my external stylesheet within my <head>
tags.
To use google fonts - https://fonts.google.com/
<body>
<div class="color-picker">
<div class="picktxt">PICK<br>A<br>COLOR</div>
<div class="picker">
<input type="color" id="favcolor" class="inputcolor" value="#243441">
</div>
<div class="color-code" id="c-code">#243441</div>
</div>
</div>
<script src="main.js"></script>
</body>
Within the body tags, you can see four divs
.They are
-
color-picker
- outer mostdiv
(Main container) -
pictxt
- Sample text -
picker
- contains the color-picker(input) -
color-code
- Display color-code
<input type="color" id="favcolor" class="inputcolor" value="#243441">
Be sure to set the type(attribute) of input as the color
and if you need to override the default color of the color picker, you can set your own color value for the value attribute
.I set the value as #243441
.
style.css
:root{
--backcol:#243441;
}
body{
background-color: var(--backcol);
}
The :root CSS pseudo-class selector
allows you to target the highest-level parent element in the DOM or document tree. If both the: root and HTML selectors target the same HTML elements, you should know that: root actually has a higher specificity.
:root can be useful for declaring global CSS variables
(--backcol). Previously, we set the color picker value to #243441
.So we have to set the initial or default background color as #243441
as well. Then if we open the index.html, you will see the color picker color and the background color of the page is the same. To achieve this, you must set the --backcol within the var function as the value of the body background-color.
.color-picker{
display: inline-flex;
justify-content: center;
align-items: center;
background-color: white;
padding: 50px 50px;
border-radius: 25px;
}
.picktext{
font-size: 25px;
color: black;
font-weight: 700;
padding: 0px 10px;
text-align: center;
font-family: 'Fredoka One', cursive;
}
.picker{
width: 90px;
height: 90px;
border-radius: 50%;
overflow: hidden;
position: relative;
}
.inputcolor{
width: 150px;
height: 150px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
cursor: pointer;
}
.color-code{
width: 100px;
padding: 10px;
font-family: 'Montserrat' ,sans-serif;
}
main.js
var inputcolor = document.getElementById("favcolor");
var root = document.documentElement;
inputcolor.addEventListener("input",(e) => {
document.getElementById("c-code").innerHTML = e.target.value;
root.style.setProperty("--backcol",e.target.value);
});
The color-picker input value set to the inputcolor(variable)
.
documentElement
returns an element which is the root element of the document.
addEventListener() method attaches an event handler to the inputcolor(variable)
.
As a result, color-code and background-color changes.
Top comments (16)
Hey there, would you consider embedding your video here instead of just linking to it? This way folks can see your content and discuss it right here on CodeNewbie without having to navigate elsewhere.
You might not have realized, but CodeNewbie actually allows folks to embed YouTube & Vimeo videos via Liquid Tags:
By the way, here's a link to the editor guide where you can see other liquid tags and formatting options.
Hope this is helpful! π
Your article made me feel truly engaged and connected. Itβs rare to find writing that feels both authoritative and comforting at the same time. The way you presented each avatar world point felt thoughtful and balanced, showing your respect for the readerβs time and attention. I appreciate the care you put into making sure your writing is clear and welcoming. Thank you for sharing this wonderful pieceβitβs truly valuable!
Medical massage can be used to treat a wide range of conditions, including back pain, arthritis, carpal tunnel syndrome, and fibromyalgia. It can also be used to alleviate symptoms of anxiety and depression, as well as improve circulation and flexibility. I have avail the services of medicinal massage islip and I am very satisfied with their services. Before a medical massage session, the therapist will typically conduct a thorough assessment of the patient's health history and current symptoms. This allows them to develop a customized treatment plan that is tailored to the patient's individual needs.
Broken Planet Shops is a cutting-edge streetwear brand known for its bold, futuristic designs and commitment to creating a unique and distinctive aesthetic. The brandβs name, "Broken Planet," reflects a deep sense of environmental awareness and social commentary, using fashion as a medium to explore themes of dystopia, resilience, and the modern worldβs challenges.
In the world of digital mysteries, kisskh was more than just a nameβit was a symbol of an elusive online presence. Known only to a select few, kisskh was a hacker with skills so advanced that no system was beyond their reach. Whispers of kisskh spread across the darkest corners of the internet, leaving traces of their encrypted messages and cryptic symbols wherever they went.
In the heart of the neon-lit city, the streets buzzed with energy as the sun dipped below the horizon. The night was alive with the pulse of music, and amidst the crowd, a figure stood outβknown only as Runasian. A master of both speed and strategy, runasian weaved through the crowd with effortless precision, always one step ahead of those who sought to challenge. Whether on foot or in the digital realm, Runasian was a force to be reckoned with, blending ancient wisdom with futuristic skills.
Dramaacoool is the perfect blend of intense emotions and effortless style, where every moment unfolds with an edge of sophistication. In this world, the stakes are high, but there's always a cool composure that keeps everything grounded. It's the essence of dramaacoool, where powerful conflicts are handled with flair, making every dramatic twist feel both captivating and effortlessly chic. Whether it's in a story, a moment, or a vibe, dramaacoool is the energy that brings depth to the cool and coolness to the drama, creating a unique balance that leaves an impression. Live in the world of dramaacoool and experience drama redefined with unmatched style.
The nasal polyps surgeon is a specialist who performs surgery on the nasal passages. Those who need this type of surgery include those who suffer from chronic sinusitis and those who have suffered an injury to their nose. Additionally, nasal polyps surgeon louisville ky has a very specialized training that includes both medical and surgical training. They must be able to handle all types of surgical procedures, including the removal of polyps, reconstruction of bones, and septoplasty (surgical removal of septum).
Pulseofideas is a Professional Blogging Platform. Here we will provide you only interesting content, which you will like very much. Weβre dedicated to providing you the best of Blogging, with a focus on dependability and Business,Tech, Lifestyle, Fashion and Many More. Weβre working to turn our passion for Blogging into a booming online website. We hope you enjoy our Blogging as much as we enjoy offering them to you.
To create natural and SEO-friendly backlinks for a topic like "Change Background-color Dynamically" in the context of Geometry Dash Unblocked, you can link the concept of changing background colors to the game's vibrant and dynamic visual style.
As the game progresses, the ball rolls faster, making it harder to avoid obstacles and stay on the track. This escalating speed adds a sense of urgency to each run, pushing players to react faster and stay focused in slope game unblocked if they want to set a new high score.