Is there a way to set global variables in css such as:
@Color1 = #fff; @Color2 = #b00; h1 { color:@Color1; background:@Color2; }
To create a variable with global scope, declare it inside the :root selector. The :root selector matches the document's root element. To create a variable with local scope, declare it inside the selector that is going to use it.
To declare a variable in CSS, come up with a name for the variable, then append two hyphens (–) as the prefix. The element here refers to any valid HTML element that has access to this CSS file. The variable name is bg-color , and two hyphens are appended.
Global variables are generic variables that will be used to keep the consistency between all our components. Some examples of global variables are font, default font-size and color palette. It's super simple to define global variables on CSS, we use the :root selector and inside it, we define our variables.
CSS variables, more accurately known as CSS custom properties, are landing in Chrome 49. They can be useful for reducing repetition in CSS, and also for powerful runtime effects like theme switching and potentially extending/polyfilling future CSS features.
Latest Update: 16/01/2020
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.
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); }
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
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>
You can't create variables in CSS right now. If you want this sort of functionality you will need to use a CSS preprocessor like SASS or LESS. Here are your styles as they would appear in SASS:
$Color1:#fff; $Color2:#b00; $Color3:#050; h1 { color:$Color1; background:$Color2; }
They also allow you to do other (awesome) things like nesting selectors:
#some-id { color:red; &:hover { cursor:pointer; } }
This would compile to:
#some-id { color:red; } #some-id:hover { cursor:pointer; }
Check out the official SASS tutorial for setup instructions and more on syntax/features. Personally I use a Visual Studio extension called Web Workbench by Mindscape for easy developing, there are a lot of plugins for other IDEs as well.
As of July/August 2014, Firefox has implemented the draft spec for CSS variables, here is the syntax:
:root { --main-color: #06c; --accent-color: #006; } /* The rest of the CSS file */ #foo h1 { color: var(--main-color); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With