CSS Variables - The var() Function
Solomon Iniodu
Remote Telesales & Sales Performance Leader | AWS Cloud Practitioner | Data-Driven Business Growth | Cloud Solutions & Cost Optimization Enthusiast | AI-Powered Sales Strategy
One feature that was on?CSS wish lists?long before it became a standard is CSS Variables, officially referred to as CSS Custom Properties in the specification. CSS Variables have been a standard now for about a decade and all modern browsers have supported them for some time.
The?var()?function is used to insert the value of a CSS variable.
CSS variables have access to the DOM, which means that you can create variables with local or global scope, change the variables with JavaScript, and change the variables based on media queries.
A good way to use CSS variables is when it comes to the colors of your design. Instead of copy and paste the same colors over and over again, you can place them in variables.
CSS variables have access to the DOM, which means that you can create variables with local or global scope, change the variables with JavaScript, and change the variables based on media queries.
Syntax of the var() Function
The?var()?function is used to insert the value of a CSS variable.
The syntax of the?var()?function is declared thus;
var(--name, value)
To declare a variable in plain CSS, place two hyphens in front of the custom name you choose for the variable, or property, then use the?var()?function to place that value wherever you desire:
:root
--main: #030303;
--accent: #5a5a5a;
}
.container {
color: var(--main);
}
.footer {
border-color: var(--main);
}
.sidebar {
color: var(--accent);
}
.module {
border-color: var(--accent);
}{
In this example, I’ve defined two CSS variables:?--main?and?--accent. I’ve then used each CSS variable on two different elements, demonstrating the flexibility they open up. As with variables in any programming language, this allows you to declare a variable in a single place. If you later want to change that value everywhere in your stylesheet, it’s simply a matter of changing the original declaration and it will apply to all the places where you use the variable.
Note:?The variable name must begin with two dashes (--) and it is case sensitive!
How the var() function Works
Where are CSS Variables defined?
As you can see in the example I provided above, you’ll often see CSS custom properties defined directly on the root element of an HTML document or shadow DOM. The :root?pseudo-class selector accomplishes this.
领英推荐
:root{
--main: #030303;
--accent: #5a5a5a;
}
But CSS Variables aren’t limited to only being defined on the root element and it’s often beneficial to define them elsewhere. The?:root?selector is commonly chosen because this always targets the uppermost element in the DOM tree (whether it’s the full document or shadow DOM).
In most cases, you’d get the same result by defining custom properties on the?html?element (which is the root element in HTML documents) or even the?body?element. Using?:root?allows code to be more future-proof (e.g. if the spec one day add a new element as the root, the CSS would stay the same) and I suppose also allows for a stylesheet to apply to a different type of document that has a different root element.
Why “Custom Properties”?
In everyday conversation, developers often refer to this feature as “CSS variables”, in line with how preprocessors and programming languages refer to the same feature. But from a strictly technical perspective, these are not really “variables” but rather are?Custom Properties.
They have the same syntax as any predefined property in CSS, except for the two dashes that appear in front of the property name. The two dashes allow for CSS authors to use any valid?dashed identifier?without fear of conflict with regular CSS properties.
The spec explains that two dashes alone are invalid (apparently reserved for future use) and CSS will never give meaning to any valid dashed-identifier beyond its use as an author-defined custom property.
Unlike regular CSS properties, Custom Properties are case sensitive. This means?--main-color?is not the same as?--Main-Color. Valid characters to include in the custom property name are letters, numbers, underscores, and hyphens.
Advantages of using var()
Technical notes about CSS Variables
In addition to being able to apply to any element, CSS Variables are fairly flexible and easy to deal with.
Here are some things worth noting:
Tricks with CSS Variables
Several developers have shared tips and tricks using CSS Variables over the years. here’s a quick rundown of some coined from my research:
Conclusion
CSS Variables, or CSS Custom Properties, are ready to use today with well over 90% of globally in-use browsers supporting this handy feature. I hope this discussion of the basics and syntax will encourage you to consider CSS Variables in your newest projects if you haven’t done so already.
Rust | Cairo | Solidity | Yul - Assembly | Typescript - @codingcas
2 年CSS has really evolved to what we used to do with it, now it has a lot of power compared to the days of just normal CSS when we use floats to layout a website. Now we have flex box, css grids, sass etc.