Section 33: CSS Custom Properties and Cascade Layers
1. Define and use custom properties
:root {
--color-primary: #2563eb;
--color-text: #1e293b;
--space-md: 1rem;
--radius-card: .75rem;
}
.button {
background: var(--color-primary);
color: white;
padding: var(--space-md);
}
.card {
border-radius: var(--radius-card);
color: var(--color-text);
}Custom properties are declared with names beginning with two hyphens. The var() function reads their values.
2. Fallback values
.button {
color: var(--button-text, white);
}If the referenced custom property is missing or has an invalid value for the consuming property, the fallback can be used. Custom properties inherit by default and participate in the cascade.
3. Theme switching with a class
:root {
--surface: #ffffff;
--text: #172033;
}
.dark-theme {
--surface: #172033;
--text: #f8fafc;
}
.panel {
background: var(--surface);
color: var(--text);
}Apply the dark-theme class to a parent element to change the values inherited by its descendants.
4. Cascade layers
@layer reset, base, components, utilities;
@layer base {
p { color: #334155; }
}
@layer components {
.message { color: #166534; }
}
@layer utilities {
.text-red { color: #b91c1c; }
}For normal declarations, later named layers have higher priority than earlier layers. Normal unlayered author declarations outrank normal declarations in named layers. Important declarations reverse the layer order, so avoid using !important casually.
Runnable example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Tokens and Layers</title>
<style>
@layer reset, base, components, utilities;
@layer reset {
* { box-sizing: border-box; }
}
@layer base {
:root { --surface: #ffffff; --text: #172033; --primary: #2563eb; }
body { margin: 0; padding: 24px; font-family: Arial, sans-serif; background: #f1f5f9; }
}
@layer components {
.panel { max-width: 400px; padding: 20px; border-radius: 12px; background: var(--surface); color: var(--text); }
.button { padding: 10px 14px; border: 0; border-radius: 6px; background: var(--primary); color: white; }
}
@layer utilities {
.text-center { text-align: center; }
}
</style>
</head>
<body>
<section class="panel">
<h2>Design tokens</h2>
<p>Shared values make design updates easier.</p>
<button class="button">Continue</button>
</section>
</body>
</html>Common mistakes
- Confusing CSS custom properties with preprocessor variables; custom properties are available at runtime and can inherit.
- Expecting a custom property declared on a child to be available on its parent.
- Assuming cascade layers eliminate all specificity rules. Specificity still matters within the same layer.
- Forgetting that normal unlayered declarations outrank normal declarations in named layers.
Practice exercise
Create light and dark themes using custom properties. Organize your reset, base styles, components, and utilities using cascade layers.
Knowledge check: 10 MCQs
- How does a CSS custom property name begin? Answer: With two hyphens. Explanation: For example, --primary-color.
- Which function reads a custom property? Answer: var(). Explanation: var(--primary-color) retrieves its value.
- What is a benefit of custom properties? Answer: Reusing and changing values consistently. Explanation: Multiple components can share the same token.
- Do custom properties normally inherit? Answer: Yes. Explanation: They inherit unless their behavior is changed, for example using @property.
- What is a var() fallback used for? Answer: Providing an alternative value. Explanation: The fallback can be used when the referenced property cannot supply a valid value.
- What does @layer organize? Answer: Groups of CSS declarations by cascade priority. Explanation: Layers provide an explicit precedence system.
- Which normal layer has greater priority: base or a later utilities layer? Answer: The later utilities layer. Explanation: Named layer order controls normal declaration priority.
- Do normal unlayered declarations outrank normal named-layer declarations? Answer: Yes. Explanation: Unlayered normal author declarations take precedence over normal layered declarations.
- Does specificity still matter within a layer? Answer: Yes. Explanation: Layer priority is evaluated before specificity.
- Why use tokens for themes? Answer: To change shared design values centrally. Explanation: Changing token values can update many components at once.