CSS Variables (Custom Properties)
Discover the power of CSS Custom Properties, also known as CSS Variables, in modern browsers. These variables allow you to write better CSS by centralizing the values and reducing repetition and inconsistencies. One unique feature of CSS Variables is the ability to access and change their values programmatically using JavaScript.
Introduction
In recent years, CSS preprocessors like Less and SASS have gained popularity for their ability to nest selectors, provide easy imports, and introduce variables. However, modern CSS now includes a powerful feature called CSS Custom Properties, or CSS Variables. While CSS is primarily a declarative syntax for styling HTML, CSS Variables help address the need for variables in CSS.
The basics of using variables
CSS Variables are defined by prefixing a name with two dashes (--variable-name
) and assigning it a value. For example:
1 | :root { |
To use the variable value, you can use the var()
function:
1 | p { |
The value of a CSS Variable can be any valid CSS value. For example:
1 | :root { |
Create variables inside any element
CSS Variables can be defined within any element. For example:
1 | :root { |
The scope of a variable depends on where it is defined.
Variables scope
When variables are added to a selector, they become available to all the children of that selector. The :root
pseudo-class is commonly used to define CSS Variables and points to the root element of a tree. For HTML documents, :root
refers to the html
element with higher specificity than html
.
To limit the scope of a variable to only the children of a specific element, you can define it within that element:
1 | .container { |
Reassigning a CSS Variable within different scopes will override its value accordingly. You can also assign or overwrite a variable inline using inline styles:
1 | <main style="--primary-color: orange;"> |
Interacting with a CSS Variable value using JavaScript
One of the most exciting features of CSS Variables is the ability to access and modify them programmatically using JavaScript. To set a variable value dynamically, you can use the setProperty()
method:
1 | const element = document.getElementById('my-element'); |
To retrieve the value of a variable defined on :root
, you can use the getComputedStyle()
function:
1 | const styles = getComputedStyle(document.documentElement); |
To get the value of a variable applied to a specific element, you can use:
1 | const element = document.getElementById('my-element'); |
Handling invalid values
If a variable is assigned to a property that does not accept that variable value, it is considered invalid and ignored. For example, passing a pixel value to a position
property or a rem
value to a color property would be invalid.
Browser support
CSS Variables have excellent browser support, making them widely accessible in modern browsers. However, if you need to support older browsers like Internet Explorer or older versions of other browsers, you may need to use libraries like PostCSS or Myth. Keep in mind that using these libraries will limit your ability to interact with variables programmatically.
CSS Variables are case sensitive
CSS Variables are case-sensitive. Therefore, --width: 100px;
is different from --Width: 100px;
.
Math in CSS Variables
To perform calculations with CSS Variables, you can use the calc()
function. For example:
1 | :root { |
Media queries with CSS Variables
CSS Variables can be used within media queries just like any other CSS property. For example:
1 | body { |
Setting a fallback value for var()
The var()
function accepts a second parameter, which is a fallback value to be used when the variable value is not set. For example:
1 | .container { |
tags: [“CSS Variables”, “CSS Custom Properties”, “variables”, “web development”, “frontend development”]