Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Since CSS variables follow the standard cascade rules, they behave like regular CSS properties. That means they inherit, cascade and can be declared on (or scoped to) any CSS selector. You can define and reset a variable at different levels of specificity.
-
0:00
You learned that to define a CSS variable you begin by defining its scope.
-
0:04
And scoping a variable to the root implies that the variable is available to
-
0:08
all elements.
-
0:10
Well, using root is a great way to set global variables and
-
0:13
keep all your variables in one place for easy maintenance like this.
-
0:16
You don't always need to define your variables inside root.
-
0:20
Since CSS variables follow the standard cascade rules,
-
0:23
they behave like regular CSS properties.
-
0:25
So that means they inherit, cascade, and can be declared on or
-
0:29
scoped to any CSS selector.
-
0:32
You can define and reset a variable at different levels of specificity.
-
0:35
So in the latest project files,
-
0:38
I have this base class of button for creating a button set,
-
0:42
that uses var(--btn-bg) to set the background color of a button.
-
0:48
Now the --btn-bg variable and
-
0:50
value have not been declared anywhere in the style sheet yet, we'll do that next.
-
0:55
So instead of defining the --btn-bg variable
-
0:58
in the root scope like we've been doing all along, I'll declare and
-
1:01
assign the values inside the button modifier selector given to each button.
-
1:06
So as you can see in index.html,
-
1:09
the call out button has the base button class and a call out class and the learn
-
1:14
more buttons in the cards also have the base button class and an info class.
-
1:21
So first in variables.css, I'll create a chained class selector
-
1:27
that targets elements with the class button and also a class name of callout.
-
1:33
And here I'll set the value of the --btn-bg variable to #b13c69.
-
1:43
Then right below I'll create a class that targets elements
-
1:48
with the class button and the class info.
-
1:53
And here I'll set the --btn-bg
-
1:58
value to #60a4b3.
-
2:02
The custom properties are resolved according to the normal inheritance in
-
2:07
cascade rules.
-
2:08
So the browser updates the background color value of a button
-
2:12
based on the class or element the --btn-bg variable is scoped to.
-
2:18
Scoping custom properties to DOM elements gives you even more interesting
-
2:21
possibilities.
-
2:23
Again, since CSS variables follow the cascade and inheritance,
-
2:27
we can style elements based on where they appear in the HTML.
-
2:30
In other words,
-
2:31
we can efficiently set how an element looks when it's inside of another element.
-
2:36
For example, to update the styles of a button
-
2:39
based on whether it appears inside an intro div, a card item or
-
2:43
a modal window, you might create three descendant selectors like this.
-
2:48
Well with custom properties, we can update the styles without
-
2:52
having to write descendant selectors that couple your CSS to he DOM structure.
-
2:56
Like earlier, we declare the regular CSS properties who's values we wanna
-
3:01
update inside the base button rule, like font size and background color.
-
3:06
So in the base button class,
-
3:09
I'll add the font size property and set the value to var and
-
3:14
specify a custom property name of --btn-font-size.
-
3:20
And now I can update how the button appears inside an intro, card,
-
3:25
and modal element by setting the custom property values on those selectors.
-
3:31
So I'll replace the chained class selector with the intro class,
-
3:37
I already have the background set.
-
3:40
I'll set the --btn-font-size variable to 1.1em.
-
3:47
And right below I'll replace the selector with a class card.
-
3:54
And set --btn-font-size to 0.85em.
-
4:02
The page also has a modal window that's set to display none for now, and
-
4:07
you can see what it looks like if I comment out the display property.
-
4:12
So back in the modal rule,
-
4:17
I'll set --btn-font-size to 1em,
-
4:25
and --btn-bg to #e85838.
-
4:31
So each of these selectors creates a new context for the variables.
-
4:36
So now any button descendent of intro, card, and
-
4:40
modal can inherit the property set on their parent selectors.
-
4:45
So that means I can go over to my HTML and
-
4:48
remove the extra call out and info modifier classes from the markup.
-
4:54
So the call up button will simply have the class button and
-
4:58
it's inside the div intro and then inside each div with a class card,
-
5:03
I'll remove the info class from the button inc elements.
-
5:12
As you can see the custom properties don't have to know anything about their context.
-
5:18
And as a result we get this perfectly themeable and reusable button component.
You need to sign up for Treehouse in order to download course files.
Sign up