Core Foundations · 9 min read · Beginner
Modern Colors, Units & Custom Properties
Build flexible design systems using CSS custom properties (--vars), rem/ch/dvh units, color-mix(), and light-dark().
What you'll learn
- Define and reuse theme tokens with CSS Custom Properties (--accent)
- Choose accessible relative units (rem, ch, %) over rigid pixels
- Mix and adapt colors dynamically with color-mix() and light-dark()
Modern CSS gives you built-in design-token variables and dynamic color functions, no preprocessor required.
CSS Custom Properties (--vars)
Declare variables on :root and read them anywhere with var(--name, fallback):
:root {
--brand: #2563eb;
--radius: 0.75rem;
}
.button {
background: var(--brand);
border-radius: var(--radius);
}
Choosing the Right CSS Units
| Unit | Relative To | Best Used For |
|---|---|---|
rem | Root <html> font size (usually 16px) | Font sizes, spacing (respects user zoom settings!) |
ch | Width of the "0" character | Comfortable paragraph line lengths (max-width: 65ch) |
dvh | Dynamic mobile viewport height | Full-screen heroes without mobile address-bar cutoff |
% | Parent container dimension | Fluid widths |
Fill-in-the-Blank Exercise
CSS Custom Properties
Define a custom property named --accent on :root and apply it to the button background.
:root {
: #2563eb;
}
.btn {
background: (--accent);
}Try It Yourself
Change --brand in :root from #2563eb to #7c3aed or #059669 and watch the entire card theme update automatically!
One-Token Theme with color-mix()
Live Result
Check Your Understanding
Score: 0 / 1
1. Why is rem preferred over px for font sizes?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.