Modern CSS & Tailwind for Beginners / Chapter 1 — Today's CSS (lessons 1–5) / Lesson 1

Using Custom Properties for Design

Time needed: about 16 min (estimate)

☆ Read later

You can try the opening of this lesson without an account.

Picking up from HTML/CSS for Beginners

In lesson 16 of HTML/CSS for Beginners, we got as far as listing colors and spacing on :root and referencing them with var().

This lesson takes it further, to the point where you can use them as a design tool.

We'll add three things: (1) variables can live somewhere other than :root, (2) you can give them a type with @property, and (3) you can swap them from JavaScript.

With all three in place, custom properties go from "a place to keep colors" to "a component's inputs."

Put them on the component root

Custom properties are inherited.

That means you don't have to put them on :root; put them on a component's root element and they become variables that only apply inside it.

Do this, and you can create variations by "just adding a class that overrides the variables."

The descendants' CSS only reads var(), so you never touch it.

editor.html

Tab indents. To leave the editor, press Esc, then Tab.

Create a free account to keep reading

With an account you can read the rest of this lesson, take the quiz, and read every free lesson in this course. Lessons you answer correctly are recorded as complete.

Report a problem with this lesson

Log in to send a report.

Keyboard shortcuts

Keys available in a lesson
KeyAction
[ or Alt + ←Go to the previous lesson
] or Alt + →Go to the next lesson
?Open this list
EscClose this list

The keys do not work while the cursor is in a code editor or an input field.