CSS Variables & Modern CSS
CSS variables help you manage colors, spacing, radius and design values from one place. In this chapter, you will learn reusable CSS and modern functions like calc() and clamp().
Custom Properties
Store reusable values like colors and spacing.
:root
Defines global variables for the whole website.
var()
Uses the value stored inside a variable.
calc() & clamp()
Modern functions for flexible sizing.
After This Chapter, You Will Understand
This chapter helps you write cleaner, scalable and professional CSS.
Variables
Create reusable CSS custom properties.
:root
Store global design values in one place.
Theme System
Build reusable colors, spacing and radius.
Modern Functions
Use calc(), min(), max() and clamp().
What are CSS Variables?
CSS variables are reusable values stored with custom property names.
1 Simple Meaning
CSS variables allow you to store a value once and reuse it many times. For example, if your brand color is used in 50 places, you can store it in one variable and use it everywhere.
2 Basic Syntax
--primary-color: #06285f;
--accent-color: #d6a549;
}
.button {
background: var(--primary-color);
}
Custom property names start with --. The var() function uses the stored value.
Global Variables with :root
The :root selector is commonly used to store global design tokens.
3 Why Use :root?
Variables declared in :root are available throughout the webpage. This makes it perfect for brand colors, font sizes, spacing, border radius and shadows.
--navy: #06285f;
--gold: #d6a549;
--radius: 22px;
--space: 24px;
}
#06285f
#d6a549
#00a7b5
#eef7ff
Build a Reusable CSS Theme
A theme system keeps your website design consistent and easy to update.
| Variable Type | Example | Use |
|---|---|---|
| Brand Color | --primary: #06285f; | Header, buttons, headings |
| Accent Color | --accent: #d6a549; | Highlights, badges, icons |
| Spacing | --space-md: 24px; | Padding and gaps |
| Radius | --radius-lg: 22px; | Cards, buttons, panels |
| Shadow | --shadow: 0 10px 30px rgba(...); | Cards and floating boxes |
4 Reusable Card
background: var(--white);
padding: var(--space-md);
border-radius: var(--radius-lg);
box-shadow: var(--shadow);
}
5 Fallback Value
var() can include a fallback value if the variable is missing.
color: var(--text-color, #334155);
}
calc(), clamp(), min() and max()
Modern CSS functions help create flexible and smarter layouts.
calc()
Performs CSS calculations.
width: calc(100% - 40px);
}
clamp()
Sets minimum, flexible and maximum value.
font-size: clamp(28px, 5vw, 54px);
}
min() and max()
Choose minimum or maximum from values.
width: min(90%, 1100px);
}
This heading uses clamp()
The title size adjusts with the screen but stays within a safe minimum and maximum range.
Practice CSS Variables
Edit variable values in :root and see the full design change.
Mistakes Students Should Avoid
CSS variables are simple, but naming and structure should be clean.
Wrong Habits
- Forgetting double hyphen in variable names
- Using var() without correct variable name
- Creating too many random variables
- Using unclear names like --color1, --x, --abc
- Repeating values instead of using variables
Correct Habits
- Use clear names like --primary, --accent
- Declare global values in :root
- Use variables for colors, spacing and radius
- Use fallback values when needed
- Keep theme values consistent
Test Your Understanding
Select the correct answers and check your score.
1. CSS variable names start with:
2. Which function is used to access a CSS variable?
3. Where are global CSS variables commonly declared?
4. Which CSS function performs calculations?
5. Which function is useful for responsive font sizes?
Remember These Points
Revise these before moving to Chapter 24.
CSS Variables
Reusable values declared with double hyphen names.
:root
Common place to declare global variables.
var()
Used to access a CSS variable value.
Theme System
Variables help manage brand colors, spacing and radius.
calc()
Performs CSS calculations.
clamp()
Creates flexible values with minimum and maximum limits.