Course contentsShow
← Web Development
Lesson 478 of 2,9828. Advanced CSS Selectors and FeaturesFree lesson

Browser Support and Invalid Values

Understanding custom property support, handling invalid values, and providing progressive enhancement.

Browser Support and Invalid Values

You'll learn: How to handle browser compatibility and gracefully manage invalid custom property values in production code.

The Core Concept

Custom properties (CSS variables) enjoy excellent modern browser support, but you still need strategies for older browsers and handling invalid values. Unlike traditional CSS where invalid values are simply ignored, custom properties have unique fallback behavior that requires understanding.

Understanding Invalid Values

When a custom property contains an invalid value for its usage context, CSS treats it differently than a normal property:

.box {
  --color: 20px; /* Valid custom property */
  color: var(--color); /* Invalid for color context */
  /* Browser uses 'initial' value (black), NOT the fallback! */
}

The property becomes "invalid at computed-value time" — it doesn't fall back to previous rules, but to the property's initial value.

Providing Safe Fallbacks

Always provide a fallback value in var() for critical properties:

.element {
  background: #333; /* Fallback for old browsers */
  background: var(--bg-color, #333); /* Fallback for undefined/invalid */
}

Browser Support Strategy

For legacy browser support (IE11 and below), use a two-line approach:

.button {
  padding: 1rem 2rem; /* Static fallback */
  padding: var(--btn-padding); /* Enhancement */
}

Modern browsers override the first line; older browsers ignore the second.

Testing for Validity

You can check support using CSS @supports:

@supports (--css: variables) {
  /* Use custom properties safely */
}

Key Takeaway: Custom properties have near-universal modern browser support, but always provide fallback values in var() and legacy fallback declarations — invalid values resolve to initial, not previous rules, making defensive coding essential.