This content originally appeared on DEV Community and was authored by Anjan Shomooder
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);
}
By the way, this blog is originally published on cules coding website.. I would be glad if you give it a visit.
How cascading works with Css variables?
You can re-declare 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 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
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
If you have any questions, please comment down below.
You can reach out to me on 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.
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
- Facebook: @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
Videos might you might want to watch:
This content originally appeared on DEV Community and was authored by Anjan Shomooder
Anjan Shomooder | Sciencx (2021-11-13T17:39:59+00:00) CSS variables: Everything you need to know about. Retrieved from https://www.scien.cx/2021/11/13/css-variables-everything-you-need-to-know-about/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.