Custom properties in CSS
Variables that cascade, take fallbacks, and can be set per component.
:root {
--accent: #7c5cff;
--radius: 0.5rem;
--pad: 1rem;
}
.button {
background: var(--accent);
border-radius: var(--radius);
padding: var(--pad) calc(var(--pad) * 1.5);
}
.button.danger {
--accent: #e5484d;
}
.card {
border-radius: var(--card-radius, var(--radius));
}
How it works
- A custom property starts with two hyphens.
var(--name, fallback)covers the unset case.- Redefining one on a child re-themes that subtree only.
Keywords and builtins used here
backgroundbuttoncalccarddangerpaddingremvar
The run, in numbers
- Lines
- 19
- Characters to type
- 273
- Tokens
- 78
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 55 seconds.
Step 1 of 3 in Custom properties, step 5 of 21 in Responsive & modern CSS.