Introducing CSS Custom Properties (Variables)

undefined or mostly null.
Search for a command to run...

undefined or mostly null.
I would like to mention that there is polyfill for IE11: https://github.com/nuxodin/ie11CustomProperties
Thanks for reading 👌
In this series, I will share articles related to best practices for building and deploying applications on the web with the JavaScript and Python programming languages.
Hugo is a fast and modern static site generator written in Go and designed to make website creation fun again. It builds pages when you create or update your content. Websites built with Hugo are extremely fast and secure. Hugo takes a source directo...
I wrote this article in December 2023 for an interview screening task and thought to share it today while clearing my drafts for some new content prep, just in case it's still helpful to someone out t

Hey! I’m super excited to announce that I have joined the Secretariat Team at the Digital Public Goods Alliance. In this role, I leverage my passion for open source and technical background to assess DPG applications, provide technical support and ad...

Creative designs have become more important than ever in the software ecosystem today with many industries and end-consumers having several use cases that require them to offer design editing solutions to either designers or end-consumers. Every busi...

With the rise of artificial intelligence (AI) and large language models (LLMs), it has become easier to solve different human problems than ever before. Even consumers with little to no technical expertise can benefit from AI. Humans can now automate...

Some years ago, GitHub introduced the new Profile README feature that allowed GitHub users to pin a markdown file on their profile using a special repository named after their GitHub username. Since then, developers have used this file as a quick por...

In the past years, maintaining CSS was a very big problem for bigger projects or complex apps as a result, building reusable components and cleaner styles were hard to achieve.
CSS Preprocessors came to solve this problem and have been around for years now (SASS, LESS, e.t.c). They extend CSS with key features like variables, operators, interpolations, functions, imports, mixins e.t.c.
However, in Modern CSS, we now have a new powerful feature called Custom properties, otherwise known as CSS variables or cascading variables. Now you can declare variables directly in your CSS without having to use CSS Preprocessors.
In this article, I’d show you how to start using CSS variables. Let’s roll :)
If you’re familiar with JavaScript or other programming languages, you should understand how variables work already.
let name = 'Bolaji Ayodeji'
A variable stores some sort of information or data, that would be used later in your program.
console.log(name);
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document. They are set using custom property notation. — MDN DOCS
A CSS Variable is defined with a special syntax, with two dashes before the variable name (--variable-name), then a colon and the variable value.
.container {
--black: #000;
}
You can access the already declared variable using var()
.container {
--black: #000
color: var(--black);
}
CSS Variables can be defined inside any CSS element.
body { --color: red;}
h1 { --color: red;}
p { --color: red;}
span { --color: red;}
a { --color: red;}
However, adding variables to a selector makes them available to ONLY their children. If you add a variable inside a .container selector, it’s only going to be available to children of .container . Outside .container , the variable will not work.
Though we set p to be red, it works for only p tags within .container , any p outside .container will not inherit this style.
:root is a CSS pseudo-class that identifies the document itself.
A better way to declare CSS variables is to add the variable to :root, this makes it available to all the elements in the page.
Now all p tags are red irrespective of their position or parent.
This variable:
--color: red;
is not the same as:
--Color: red;
var() accepts a second parameter, which is the default fallback value when the variable value is not set:
p {
color: var(--red, #f00);
}
Here we forgot to declare --red but p still has the red color because we already have a fallback value.
Browser support for CSS Variables according to Can I Use.
CSS variables are supported in modern browsers. I personally don’t focus on IE and far older browsers because a large number of internet users are using modern browsers now, just a few aren’t. So If you don’t need to support Internet Explorer and old versions of the other browsers, CSS variables should be your best friend.
CSS Variables follow the normal CSS cascading rules, with precedence set according to the specificity. We can also use the values of custom properties in JavaScript, just like standard properties.
Complex websites have very large amounts of CSS, often with a lot of repeated values. For example, the same color might be used in hundreds of different places, requiring global search and replace if that color needs to change. Custom properties allow a value to be stored in one place, then referenced in multiple other places. An additional benefit is semantic identifiers. For example, --black is easier to understand than #00000 , especially if this same color is also used in other contexts. -MDN
CSS variables are cool and have come to stay, if you want your CSS styles and development to become clean, easier and more efficient, you should stick to CSS variables :)
For more knowledge, read here