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.

Everything you need to know about css variables

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.

In this blog, you will learn about css variables also known as custom properties.

You will learn:

  • Why do Css variables exist?
  • How to use it?
  • How cascading works with Css variables?
  • How to access css variables in javascript?
  • How to change css variables with javascript?

For example, You have 5 containers on your webpage. All of the container the background color will be red.

1.container1 {
2 background: red;
3}
4
5.container2 {
6 background: red;
7}
8
9.container3 {
10 background: red;
11}
12
13.container4 {
14 background: red;
15}
16
17.container5 {
18 background: red;
19}

Ok. That works fine. But after some time you started to think that the background color should be blue. Now you want to change the color. But now you have to every single container and apply the change. It will work but that is too much work. So,

What is the solution?

What if we can change the color once and that will be applied everywhere?

Yes, we can do that with the help of css variables. Css variables are officially known as custom properties.

So,

How to use Css variables?

We normally define variables at the root.

At the top of your css file select the root like this:

1:root {
2}

Inside the root you have to declare the variables just like you use properties and value in CSS.

1:root {
2 --primary-bg-color: red;
3}

Syntax

Variable name has to start will --(double dash). For example:

  • --primary-bg-color
  • --secondary-bg-color
  • --primary-font-color

Now you can use those variables as a value in any valid css property. Let's apply it to our example.

1:root {
2 --primary-bg-color: red;
3}
4
5.container1 {
6 background: var(--primary-bg-color);
7}
8
9.container2 {
10 background: var(--primary-bg-color);
11}
12
13.container3 {
14 background: var(--primary-bg-color);
15}
16
17.container4 {
18 background: var(--primary-bg-color);
19}
20
21.container5 {
22 background: var(--primary-bg-color);
23}

Now if you want to change the color, just change the value of the variable. And changes will be applied everywhere.

1:root {
2 --primary-bg-color: blue;
3}
4
5.container1 {
6 background: var(--primary-bg-color);
7}
8
9.container2 {
10 background: var(--primary-bg-color);
11}
12
13.container3 {
14 background: var(--primary-bg-color);
15}
16
17.container4 {
18 background: var(--primary-bg-color);
19}
20
21.container5 {
22 background: var(--primary-bg-color);
23}

How cascading works with Css variables?

You can re-decalre the value for any css selector. Like this:

1:root {
2 --primary-bg-color: blue;
3}
4
5.container1 {
6 background: var(--primary-bg-color);
7}
8
9.container2 {
10 --primary-bg-color: green;
11 background: var(--primary-bg-color);
12}
13
14.container3 {
15 background: var(--primary-bg-color);
16}
17
18.container4 {
19 background: var(--primary-bg-color);
20}
21
22.container5 {
23 background: var(--primary-bg-color);
24}

Now the .container2 will have a background color of green. But other containers' background colors will not change. But why?

All of the places that you are using CSS variables are just inheriting values from the root. But when you redeclare the variable for .container2 all of the elements inside the .container2 are just inheriting the value from the .container2.

If the .container2 would look like below, what would be the color of .subcontainer2?

1:root {
2 --primary-bg-color: blue;
3}
4
5.container2 {
6 --primary-bg-color: green;
7 background: var(--primary-bg-color);
8}
9
10.container2 .subcontainer1 {
11 background: teal;
12}
13
14.container2 .subcontainer1 .subcontainer2 {
15 background: var(--primary-bg-color);
16}

Will it be green or blue???

It will be green. Why?

Because

  • The CSS parser will check inside .subcontainer2 first. Then it will not find anything.
  • Then it will check subcontainer2's parent .subcontainer1. Again it will find anything.
  • Then it will check .subcontainer1's parent .container. This time the parser will see a variable declaration that has the value green. It will not check anything.

So, this is how it works:

I know it sounds complex. If you know javascript, it is like scope chain in javscript

How to access CSS variables in javascript?

Yes, you can access css variables in javascript and also you can manipulate them.

1const allStyles = getComputedStyle(document.documentElement) // returns all css styles as an object
2
3const bgColor = allStyles.getPropertyValue(--primary - bg - color)
4
5console.log(bgColor) // green or whatever the value is

How to change css variables with javascript?

1// Get the root element
2const root = document.querySelector(':root')
3
4// changing the value of variable
5root.style.setProperty('--primary-bg-color', 'red')

And that's all you need to know about css variables.

Shameless Plug

I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.

That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.

Contacts

Blogs you might want to read:

Videos might you might want to watch:

Previous PostAdd a video background to your landing page to make it more gorgeous
Next PostAdd a glass effect with HTML and CSS