Fallback Values and Browser Support
What You'll Learn
You'll learn how to write resilient CSS that works across browsers by providing fallback values and using progressive enhancement strategies.
The Safety Net Pattern
When using modern CSS features like custom properties, not all browsers understand them immediately. A fallback value is like a safety net—you provide a simpler, widely-supported value first, then override it with the modern feature for capable browsers.
Think of it like ordering at a restaurant: you give your first choice, but also say "if you don't have that, I'll take this instead." Older browsers take the fallback; modern ones use the enhanced version.
How Fallbacks Work
Browsers ignore CSS declarations they don't understand. By placing a fallback before the modern version, older browsers use the fallback and skip what they can't parse:
.button {
/* Fallback: all browsers understand hex colors */
background-color: #3498db;
/* Progressive enhancement: modern browsers use the theme variable */
background-color: var(--color-primary, #3498db);
}
The older browser stops at line 2. The modern browser reads both but uses line 5, which overrides line 2.
Progressive Enhancement Strategy
Always layer your CSS from least to most capable:
.card {
/* Base: works everywhere */
padding: 16px;
/* Enhanced: browsers with custom properties */
padding: var(--spacing-medium, 16px);
}
Notice the custom property itself contains a fallback (16px) as its second parameter—defense in depth!
Multi-Level Fallbacks for Theme Switching
.theme-element {
color: #333; /* Level 1: universal */
color: var(--text-color, #333); /* Level 2: custom props */
}
Key Takeaway: Always provide fallback values before modern CSS features, creating a cascade from universally-supported to cutting-edge that ensures your site works everywhere while progressively enhancing for capable browsers.