Creating CSS Global Variables : Stylesheet theme management
Latest Update: 16/01/2020
CSS Custom Properties (Variables) have arrived!
It's 2020 and time to officially roll out this feature in your new applications.
Preprocessor "NOT" required!
There is a lot of repetition in CSS. A single color may be used in several places.
For some CSS declarations, it is possible to declare this higher in the cascade and let CSS inheritance solve this problem naturally.
For non-trivial projects, this is not always possible. By declaring a variable on the :root
pseudo-element, a CSS author can halt some instances of repetition by using the variable.
How it works
Set your variable at the top of your stylesheet:
CSS
Create a root class:
:root {
}
Create variables (-- [String] : [value])
:root {
--red: #b00;
--blue: #00b;
--fullwidth: 100%;
}
Set your variables anywhere in your CSS document:
h1 {
color: var(--red);
}
#MyText {
color: var(--blue);
width: var(--fullwidth);
}
BROWSER SUPPORT / COMPATIBILITY
See caniuse.com for current compatability.
Firefox: Version 31+ (Enabled by default)
Supported since 2014 (Leading the way as usual.)
More info from Mozilla
Chrome: Version 49+ (Enabled by default).
Supported since 2016
Safari/IOS Safari: Version 9.1/9.3 (Enabled by default).
Supported since 2016
Opera: Version 39+ (Enabled by default).
Supported since 2016
Android: Version 52+ (Enabled by default).
Supported since 2016
Edge: Version 15+ (Enabled by default).
Supported since 2017
CSS Custom Properties landed in Windows Insider Preview build 14986
IE: When pigs fly.
It's time to finally let this ship sink. No one enjoyed riding her anyway. ☺
W3C SPEC
Full specification for upcoming CSS variables
Read more
TRY IT OUT
A fiddle and snippet are attached below for testing:
(It will only work with supported browsers.)
DEMO FIDDLE
:root {
--red: #b00;
--blue: #4679bd;
--grey: #ddd;
--W200: 200px;
--Lft: left;
}
.Bx1,
.Bx2,
.Bx3,
.Bx4 {
float: var(--Lft);
width: var(--W200);
height: var(--W200);
margin: 10px;
padding: 10px;
border: 1px solid var(--red);
}
.Bx1 {
color: var(--red);
background: var(--grey);
}
.Bx2 {
color: var(--grey);
background: black;
}
.Bx3 {
color: var(--grey);
background: var(--blue);
}
.Bx4 {
color: var(--grey);
background: var(--red);
}
<p>If you see four square boxes then variables are working as expected.</p>
<div class="Bx1">I should be red text on grey background.</div>
<div class="Bx2">I should be grey text on black background.</div>
<div class="Bx3">I should be grey text on blue background.</div>
<div class="Bx4">I should be grey text on red background.</div>
How to use css variable colors and js to change div background
You can add a data-color
attribute to your buttons which specifies the color (variable) to change to when clicking on the given button. Then you can add the click
event listener to all your buttons with the class color-btn
. Then when you click on a given button the event listener will be called, which will get the data attribute from the button clicked, and then use your changeColor
function to change the div to the appropriate color (using the variable).
Also, you haven't defined your variables correctly. Firstly, you need to target the root
using :root
. Then you nee to set your variables using --variableName: COLOR
. Lastly, in your changeColor
function you need to use .style.backgroundColor = "var(--" +color +")"
to correctly use the variable.
See working example below
[...document.getElementsByClassName("color-btn")].forEach(elem => { elem.addEventListener('click', function() { const btnColor = this.getAttribute('data-color'); changeColor(btnColor); });})
function changeColor(color) { var element = document.getElementById('box'); element.style.backgroundColor = "var(--" +color +")";}
:root { --w: #fff; --b: #000;}
.box { width: 100px; height: 100px; border: 1px solid #aaa; background-color: #fff;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="box" id="box"></div>
<button class="color-btn" data-color='w'>white</button><button class="color-btn" data-color='b'>black</button>
How to create color shades using CSS variables similar to darken() of Sass?
The new Specification introduces "relative color syntax" where you can do the following
:root {
--color-primary: #f00; /* any format you want here */
--color-primary-darker: hsl(from var(--color-primary) h s calc(l - 5%));
--color-primary-darkest: hsl(from var(--color-primary) h s calc(l - 10%));
}
The idea is to convert the main color to hsl
format and using calc()
you adjust the lightness.
There is still no support for this to date so consider the below solution.
You can consider hsl()
colors and simply control the lightness:
:root {
--color:0, 100%; /*the base color*/
--l:50%; /*the initial lightness*/
--color-primary: hsl(var(--color),var(--l));
--color-primary-darker: hsl(var(--color),calc(var(--l) - 5%));
--color-primary-darkest: hsl(var(--color),calc(var(--l) - 10%));
}
.button {
background: var(--color-primary);
display:inline-block;
padding:10px 20px;
color:#fff;
cursor:pointer;
}
.button:hover,
.button:focus {
background: var(--color-primary-darker);
}
.button:active {
background: var(--color-primary-darkest);
}
<span class="button">some text</span>
Related Topics
2 Column Div Layout: Right Column With Fixed Width, Left Fluid
How to Word-Wrap Long Words in a Div
Format Text in a ≪Textarea≫
Size of Html5 Canvas Via CSS Versus Element Attributes
How to Add a Scrollbar to an Html5 Table
Font Awesome Not Working, Icons Showing as Squares
How to Style the Html5 Form Validation Error Messages With Css
Is There a "Previous Sibling" Selector
Align Inline-Block Divs to Top of Container Element
Transitions on the CSS Display Property
Purpose of Asterisk Before a CSS Property
Is the CSS Star Selector Considered Harmful (And Why)
What, Exactly, Is Needed For "Margin: 0 Auto;" to Work
Overflow-X:Hidden Doesn't Prevent Content from Overflowing in Mobile Browsers
Use of Xsl-Fo, Css3 Instead of Css2 to Create Paginated Documents Like Pdf