Declaration
--accent: #8cffc1 defines a reusable value.
CSS Variables, officially custom properties, let you store reusable values in CSS and let those values participate in the cascade.
:root { --accent: #8cffc1; }
.button { color: var(--accent); }
Cascade & Control
A CSS custom property starts with two dashes, such as --color-accent. You read it with var(--color-accent).
Custom properties are not simple search-and-replace constants. They cascade and inherit, which means their value can change depending on where they are defined.
This makes them excellent for color systems, spacing scales, component themes and responsive adjustments. Used well, variables make CSS more coherent and easier to redesign.
--accent: #8cffc1 defines a reusable value.
color: var(--accent) reads that value.
var(--accent, hotpink) uses fallback if the variable is missing.
Variables can be global on :root or local inside a component.
Visual model
The browser does not apply styles emotionally. It follows a priority system. These diagrams make that priority visible before you start changing declarations.
Global design tokens such as colors and spacing.
Theme overrides can redefine the same variable.
Components consume variables without knowing the exact value.
The browser resolves the current value at computed-value time.
Examples
:root {
--surface: #101827;
--text: #f7f9ff;
--accent: #8cffc1;
}
.card {
background: var(--surface);
color: var(--text);
}
.card a {
color: var(--accent);
}
.card { background: #101827; color: #f7f9ff; }
.hero { background: #101827; color: #f7f9ff; }
.button { background: #8cffc1; }
.link { color: #8cffc1; }
Rules that matter
CSS becomes easier when every override has a reason. If a rule wins by accident, the next developer has to debug your intention instead of the code.
--color-accent is better than --green if the color may change later.
Global brand, spacing and typography values usually belong on :root.
A component can define --card-bg or --button-bg for easy variants.
var(--accent, #8cffc1) prevents missing variables from breaking the value.
A variable for every one-off value makes CSS harder to read.
Variables can change by context, which is powerful and sometimes surprising.
Production thinking
Variables make CSS feel like a design system rather than a pile of declarations. They let a redesign change a few values instead of hunting through the whole stylesheet.
Variables are useful for maintaining contrast pairs. Store text and background values together so themes do not accidentally become unreadable.
Production design systems often use custom properties for tokens, themes, component options and responsive values.
Variables do not directly affect SEO, but they help keep visual quality consistent across many pages and reduce maintenance errors.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these questions before moving on. If the answer is vague, inspect the lab example and trace which declaration wins.
Senior audit upgrade
CSS variables are not preprocessor constants. They are resolved by the browser at computed-value time and can change by scope, state and media query.
A variable defined on .card is available to descendants of that card, not globally.
var(--x, fallback) only helps when --x is missing or invalid at computed time.
Typed custom properties can define syntax, inheritance and initial values for advanced animation and safety.
@property --angle {
syntax: '<angle>';
inherits: false;
initial-value: 0deg;
}
Chapter project
Trace which rule wins, reduce specificity pressure and move repeated values into custom properties.
a controlled component variant system without random overrides
Can you explain exactly why the final declaration wins?