CSS Variables
Use CSS variables when you want to apply a CSS style to many elements (selectors) so you don't need to individually style them.
1. First you need to declare your variables inside a selector. ":root" is used so that the variables can be applied to any selectors on the page. The variable name/custom property begins with two dashes "--".
When you create a variable, it becomes available for you to use inside the element in which you create it. It also becomes available within any elements nested within it. This effect is known as cascading. Because of cascading, CSS variables are often defined in the :root element.
:root {
--main-bg-color: coral; }
2. You then insert the variable to the properties of elements you want it applied to using
var("variable name"). This var function has two properties where value is the fallback value:
var("custom name", "value")
#div1 {
background-color: var(--main-bg-color); }
#div2 {
background-color: var(--main-bg-color);}
Good to use inside media queries!
1. First you need to declare your variables inside a selector. ":root" is used so that the variables can be applied to any selectors on the page. The variable name/custom property begins with two dashes "--".
When you create a variable, it becomes available for you to use inside the element in which you create it. It also becomes available within any elements nested within it. This effect is known as cascading. Because of cascading, CSS variables are often defined in the :root element.
:root {
--main-bg-color: coral; }
2. You then insert the variable to the properties of elements you want it applied to using
var("variable name"). This var function has two properties where value is the fallback value:
var("custom name", "value")
#div1 {
background-color: var(--main-bg-color); }
#div2 {
background-color: var(--main-bg-color);}
Good to use inside media queries!
Comments
Post a Comment