Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.6 — Custom Properties
CSS · Example 2 of 3

A light/dark theme switch

Click the button. JavaScript only flips one attribute; the variables do the rest.

Custom properties (CSS variables) are properties you name yourself, like --brand, read with var(--brand, fallback); they inherit down the page like color does, so redefining one on a parent re-themes everything inside it.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Create tokens --space-1 (0.25rem), --space-2 (0.5rem) and --space-4 (1rem) on :root and use them for a .card's padding (space-4) and gap between its children (space-2).

02

Make an .alert component whose border and text use --alert-color, defaulting to blue when not set, and an .alert.error variant that sets it to red.

03

Write a JavaScript line that reads --brand from the root, and one that sets it to #7c3aed.

Explain it without notes

01

What is a custom property and how do you read it?

02

Explain how scope and inheritance make custom properties good for theming.

03

When is the fallback in var(--x, fallback) used, and when is it not?

04

What does "invalid at computed-value time" mean, and what value does the property get?

05

How do CSS custom properties differ from Sass variables?

A light/dark theme switch

Preview

Press Run to see the page.

Console

Nothing logged yet.