CSS Variables — Complete Guide
CSS Variables — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of CSS Tutorial on Toolliyo Academy.
On this page
CSS Tutorial · Lesson 41 of 100
CSS Variables
Foundations & Layout ✓ → Responsive & Motion → Architecture & Perf → Projects
Responsive & Motion · 2 — Adapt · ~13 min read · CSS — Modern CSS3 Features
1. Introduction
Today: CSS Variables. Read the diagram, paste the CSS, then change one value and watch the UI update.
CSS custom properties (variables) store reusable values on :root or any selector — var(--name) reads them and inherits down the tree. They can change at runtime for theming.
2. Real-world story
Partner bank sets --sv-color-primary to their brand green via config.
Outcome: One CSS bundle serves multiple tenant themes.
3. Why it matters
StyleVerse brand colors, spacing, and sidebar width live in variables so white-label banking clients swap themes instantly.
4. Visual understanding
Read this diagram — the mental model for CSS Variables.
:root { --color-brand: #2874f0; }
component uses var(--color-brand)
calc() / clamp() for fluid sizes
modern extras: aspect-ratio, object-fit, backdrop-filter
5. Key concepts (easy words)
| Idea | Meaning |
|---|---|
| What | CSS custom properties (variables) store reusable values on :root or any selector — var(--name) reads them and inherits down the tree. They can change at runtime |
| Remember | --name: value; then var(--name). Variables cascade and inherit. Runtime theme switches override :root or data attributes. |
| Practice tip | Change one property, refresh, watch DevTools Computed. |
6. How it works
- Tokens defined once on root. btn-primary consumes variables — change :root values to retheme all buttons.
- --name: value; then var(--name).
- Variables cascade and inherit.
- Check the result in DevTools → Computed and the box model overlay.
7. Choose wisely
| Option | Notes |
|---|---|
| Do | Practice CSS Variables on a small StyleVerse card |
| Avoid | Copying huge frameworks before learning core CSS |
8. Try this example
Paste into a <style> block (or use Run Example). Refresh to see StyleVerse UI changes.
:root {
--sv-color-primary: #2874f0;
--sv-space-md: 16px;
--sv-radius: 8px;
}
.btn-primary {
background: var(--sv-color-primary);
padding: var(--sv-space-md);
border-radius: var(--sv-radius);
}
Line walkthrough
| Code | What it means |
|---|---|
:root { | Selector — picks which elements get these declarations. |
--sv-color-primary: #2874f0; | Declaration — property and value that change appearance. |
--sv-space-md: 16px; | Declaration — property and value that change appearance. |
--sv-radius: 8px; | Declaration — property and value that change appearance. |
} | Ends the rule block. |
.btn-primary { | Selector — picks which elements get these declarations. |
background: var(--sv-color-primary); | Declaration — property and value that change appearance. |
padding: var(--sv-space-md); | Declaration — property and value that change appearance. |
border-radius: var(--sv-radius); | Declaration — property and value that change appearance. |
} | Ends the rule block. |
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
9. Another real-world angle
10. Best practices checklist
- Prefer classes over ids for reusable StyleVerse components.
- Use design tokens (CSS variables) for color and spacing.
- Mobile-first media queries; test keyboard focus.
- Keep specificity low — avoid !important except rare overrides.
- Animate transform/opacity when you can; respect prefers-reduced-motion.
11. Common mistakes
- Using variables for non-inheriting properties without defining on element.
- Deep nesting of var() without documenting token map.
12. Practice in the browser
- Define tokens on :root in tokens.css.
- Reference var() in component rules.
- Override --sv-color-primary on [data-theme="corp"].
- Provide fallback: var(--missing, #000).
Experiments
- Add --sv-color-primary-hover: color-mix(in srgb, var(--sv-color-primary) 85%, black).
- Scope --sv-sidebar-w on .app-shell only.
13. FAQ
Where should I put CSS for CSS Variables?
Start in a <style> block or styles.css linked from HTML. Later use CSS Modules, Tailwind, or tokens in a design system.
Why is my rule not applying?
Check selector match, typos, specificity, and whether a more specific rule or inline style wins. DevTools Computed tells the truth.
Flexbox or Grid for this?
Flex for one direction (toolbars, card rows). Grid for two-dimensional page frames. Many UIs use both.
14. Interview questions
Explain CSS Variables simply.
CSS custom properties (variables) store reusable values on :root or any selector — var(--name) reads them and inherits down the tree. They can change at runtime for theming.
How do you debug CSS?
Inspect element, toggle declarations, read Computed styles, outline the box model, and reduce specificity conflicts.
How does this show up in production UI?
One CSS bundle serves multiple tenant themes.
15. Remember
- --name: value; then var(--name).
- Variables cascade and inherit.
- Runtime theme switches override :root or data attributes.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!