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.

.container1 {
background: red;
}
.container2 {
background: red;
}
.container3 {
background: red;
}
.container4 {
background: red;
}
.container5 {
background: red;
}

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:

:root {
}

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

:root {
--primary-bg-color: red;
}

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.

:root {
--primary-bg-color: red;
}
.container1 {
background: var(--primary-bg-color);
}
.container2 {
background: var(--primary-bg-color);
}
.container3 {
background: var(--primary-bg-color);
}
.container4 {
background: var(--primary-bg-color);
}
.container5 {
background: var(--primary-bg-color);
}

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

:root {
--primary-bg-color: blue;
}
.container1 {
background: var(--primary-bg-color);
}
.container2 {
background: var(--primary-bg-color);
}
.container3 {
background: var(--primary-bg-color);
}
.container4 {
background: var(--primary-bg-color);
}
.container5 {
background: var(--primary-bg-color);
}

How cascading works with Css variables?

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

:root {
--primary-bg-color: blue;
}
.container1 {
background: var(--primary-bg-color);
}
.container2 {
--primary-bg-color: green;
background: var(--primary-bg-color);
}
.container3 {
background: var(--primary-bg-color);
}
.container4 {
background: var(--primary-bg-color);
}
.container5 {
background: var(--primary-bg-color);
}

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?

:root {
--primary-bg-color: blue;
}
.container2 {
--primary-bg-color: green;
background: var(--primary-bg-color);
}
.container2 .subcontainer1 {
background: teal;
}
.container2 .subcontainer1 .subcontainer2 {
background: var(--primary-bg-color);
}

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:

Css variables cascading demonstratition

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.

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

How to change css variables with javascript?

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

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

Shameless Plug

I have made a video about how to build a carousel postcard with React, Material-UI, and Swiper.js. If you are interested you can check the video.

You can also demo the application form here

Screenshot of Insta Carousel

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

If you have any questions, feel free to contact me on any social media as @thatanjan. Stay safe. Goodbye.

About me

Why do I do what I do?

The Internet has revolutionized our life. I want to make the internet more beautiful and useful.

What do I do?

I ended up being a full-stack software engineer.

What can I do?

I can develop complex full-stack web applications like social media applications or e-commerce sites.

What have I done?

I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories.

Screenshot

Homepage

More screenshots

I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don't miss the cool stuff.

Want to work with me?

I am looking for a team where I can show my ambition and passion and produce great value for them. Contact me through my email or any social media as @thatanjan. I would be happy to have a touch with you.

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