
Custom properties store values in the CSS cascade. Use them for colors, spacing, and values that vary by component, state, or theme.
1. Define custom properties
Names start with two hyphens and are case-sensitive. Use :root for shared tokens and a component selector to override a value in that component.
:root { --accent: #2656a8; --space: 1rem; }
.promo { --accent: #784b96; }2. Read them with var()
Use var(–name) inside a property value. Add a fallback after the comma for an undefined or otherwise invalid custom property. The fallback does not fix a value that exists but is unsuitable for the receiving property.
.card { border-color: var(--accent, #345); padding: var(--space, 1rem); }3. Work with scope and inheritance
Unregistered custom properties inherit by default. A component can define a local value without changing the rest of the site. Values participate in the cascade, so check which definition wins in DevTools.
4. Use meaningful names and organize definitions
Name shared tokens for roles such as –text-color or –space-small. Keep component-local values close to their rules. Test what happens when a theme overrides one token.
5. Practice
Define two color tokens and a spacing token, use them in a button and card, and override one color inside a single section. Check which values the children inherit.