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

Fallbacks for Browsers Without Modern Color Space Support

Provide fallback colors using traditional RGB/HSL for browsers that don't yet support advanced color space functions.

Fallbacks for Browsers Without Modern Color Space Support

You'll learn how to ensure your colors work everywhere by providing fallback values for browsers that don't understand modern color space functions.

The Problem

Modern color spaces like oklch(), color(display-p3), and lab() offer stunning, vivid colors — but older browsers simply ignore them, leaving your elements colorless or using a default. This breaks your design for users on unsupported browsers.

The Solution: CSS Fallback Pattern

CSS naturally supports fallbacks through its cascade. When a browser encounters a property value it doesn't understand, it ignores that declaration and uses the previous valid one. You can leverage this by declaring colors twice:

.button {
  /* Fallback: traditional format all browsers understand */
  background-color: rgb(230, 80, 150);
  
  /* Enhancement: modern color space for supported browsers */
  background-color: oklch(0.65 0.25 350);
}

Older browsers read the first line, then skip the second (unrecognized) line — they keep the RGB value. Modern browsers read both, then use the last valid value — the oklch() color.

Think of It Like a Menu

Imagine ordering at a restaurant. You first ask for a basic dish everyone serves (fallback), then ask if they have a premium version (modern feature). If they don't have it, you still get the basic dish — you're not left hungry.

When to Provide Fallbacks

Always provide fallbacks when using oklch(), lch(), lab(), oklab(), or color() functions. Use traditional rgb(), hsl(), or hex values that approximate your desired color as closely as possible.

Key Takeaway: Always declare a traditional color format before modern color space functions to ensure graceful degradation for all users.