Lesson 2 of 8

Custom properties

Goal

One value in one place

01

Explanation

A custom property, often called a CSS variable, is a property whose name starts with two hyphens, and you read its value with the var() function.

You define one like this: :root { --brand: #1d4ed8 }, and use it like this: color: var(--brand).

What sets them apart from Sass variables is that they work in the browser itself and are inherited by nested elements. Redefine one on any element and its value changes for everything inside that element, which is why dark mode takes two rules instead of a whole second stylesheet.

02

Your task

Define --brand on :root and use it in .btn, then redefine it inside .alt so the second button shows a different color without writing any new color rule.

Checks

4 checks
  1. The --brand variable is defined
  2. The button gets its background from var()
  3. The .alt selector redefines the variable
  4. The two buttons show different colors
03

Hints

0/3
Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/4

Press “Run and check” to grade your code.

4 checks listed under “Your task”.

Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.