Overview
A custom property is any property whose name starts with --, read with var(--name, fallback). Define design tokens on :root and override them per theme or component, so dark mode is a variable swap rather than a second stylesheet.
Rules
- Custom properties inherit and cascade like ordinary properties (cascade), and resolve at computed-value time. Changing one at runtime (a media query, a container query, or
element.style.setProperty) updates everyvar()use, which Sass variables cannot do. - Values are untyped by default. Register a property with
@propertyto get a type and make it animatable;syntaxandinheritsare required, andinitial-valueis required unlesssyntaxis"*".@propertyhas been available in all major browsers since July 2024. @container style(--x: y)queries read custom property values (container-query).- Tailwind 4 emits its theme tokens as CSS variables (tailwind-theme).
:root { --color-bg: #fff; --color-text: #111; }
@media (prefers-color-scheme: dark) { :root { --color-bg: #111; --color-text: #fff; } }
.card { background: var(--color-bg); color: var(--color-text); }Related
- cascade: custom properties follow the normal cascade.
- container-query: style queries read them.
- css-custom-properties: token and theming patterns.
- tailwind-theme: Tailwind theme variables.
- css: CSS conventions.