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 (20)
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! π
Looking to buy recombinant protein for your lab or biotech project? Our high-quality recombinant proteins are designed to support a wide range of applications, including biomedical research, drug discovery, diagnostics, and therapeutic development. Produced using advanced expression systems like E. coli, yeast, and mammalian cells, each protein is rigorously tested for purity, activity, and consistency.
Car Parking Multiplayer is not just another car simulation game β it's a full open-world experience. Designed with attention to detail, this game lets players explore realistic environments,,,,,,customize their vehicles, and test their parking skills in both single and multiplayer modes.
From realistic car physics to voice chat and police mode, Car Parking Multiplayer brings a variety of features that keep players engaged for hours. Whether you're a beginner learning to park or a pro looking to drift around corners, this game has something for everyone.
In world entertainment news, Coachella outfits continue to dominate headlines as the iconic music and arts festival once again became a global fashion runway. Celebrities and influencers from around the world flocked to Indio, California, not just for the star-studded performances, but to showcase their bold, creative, and trend-setting styles.
This year, Coachella outfits featured a mix of futuristic metallics, Y2K nostalgia, and sustainable fashion statements. Stars like Zendaya, Doja Cat, and international K-pop icons turned heads with custom designs that blended music, art, and culture. Fashion critics and fans alike are calling 2025 one of the most stylish Coachella years yet, cementing the festivalβs role as a global hub for both music and fashion innovation.
Whether you're a fashion enthusiast or simply keeping up with global trends, Coachella outfits are proving to be a key part of world entertainment news this season.
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.
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.
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.
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.
Global CSS variables can be used to set initial or default values for various properties. For example, setting --backcol to #243441 ensures that the initial subway surfers background color of the page matches the color picker value.