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:
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 objectconst 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 elementconst root = document.querySelector(':root')// changing the value of variableroot.style.setProperty('--primary-bg-color', 'red')
And that's all you need to know about css variables.
Shameless Plug
I have made few project based videos with vanilla HTML, CSS, and JavaScript.
You will learn about:
- Javascript intersection observer to add cool effects
- DOM manipulation
- Aligning elements with CSS positions.
- How to make responsive websites.
- How to create slide based webpage.
These will be great projects to brush up on your front end skills.
If you are interested you can check the videos.
You can also demo the application from here:
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project.
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. See more of my work from here
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
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
- Email: thatanjan@gmail.com
- linkedin: @thatanjan
- portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Instagram (youtube channel): @thatanjan
- twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: