🎨 CSS color-scheme β€” Complete Tutorial

🌐 What Is color-scheme?

The color-scheme property tells the browser which **color themes** (light, dark, or both) your component or site is designed to support. This enables the browser to automatically adjust built-in UI elements such as:

  • βœ” Form controls
  • βœ” Scrollbars
  • βœ” Input fields
  • βœ” Dialogs & widgets
>>β€œUse color-scheme to let browsers render native UI with the correct theme (light or dark).”

Note

βœ” Essential for dark-mode support
βœ” Works with system preferences β†’ prefers-color-scheme
βœ” Applies to UI controls, not just text

πŸ“¦ Syntax

color-scheme syntax

color-scheme: normal | light | dark | light dark;
ValueDescription
normalNo special theme support (default).
lightComponent supports only light mode.
darkComponent supports only dark mode.
light darkSupports both modes β€” browser picks based on user preference.

πŸ“Œ 1. Enable Dark & Light Mode Automatically

Both modes

:root {
  color-scheme: light dark;
}

Allows browser UI to match the user's system theme automatically.

πŸ“Œ 2. A Component That Supports Only Dark Mode

Dark UI only

.dark-widget {
  color-scheme: dark;
}

Browser renders scrollbars, inputs, and form controls using dark styles.

πŸ“Œ 3. Force Light Appearance for a Section

Light only

.light-section {
  color-scheme: light;
}

Ensures UI controls use light styling even in dark mode.

πŸ“Œ 4. Combine With prefers-color-scheme

System theme support

@media (prefers-color-scheme: dark) {
  body {
    background: #111;
    color: #eee;
  }
}

@media (prefers-color-scheme: light) {
  body {
    background: #fff;
    color: #222;
  }
}

Let CSS detect the user's system preference and theme the page accordingly.

πŸ“Œ 5. Improve Form Input Styling Automatically

Form controls

form {
  color-scheme: light dark;
}

Browser-native controls (checkboxes, radios, selects) automatically adapt.

πŸ“Œ 6. Use Inside Shadow DOM Components

Shadow DOM

:host {
  color-scheme: dark;
}

Ensures web components render UI consistent with their internal theme.

πŸ§ͺ Real-World Examples

1️⃣ Whole Site Supports Light & Dark Modes

Site-wide theme support

html {
  color-scheme: light dark;
}

2️⃣ Dashboard Widgets With Forced Dark Theme

Dashboard

.widget {
  background: #1e1e1e;
  color: #fafafa;
  color-scheme: dark;
}

3️⃣ Modal or Card That Must Stay Light Even in Dark Mode

Force light mode

.modal {
  background: white;
  color: black;
  color-scheme: light;
}

⚠️ Common Mistakes

  • ❌ Assuming color-scheme changes your page’s background β€” it doesn’t
  • ❌ Forgetting that it affects only system-drawn UI parts
  • ❌ Using dark-only UI while your text remains light β†’ contrast issues

Note

color-scheme does not style elements itself. It informs the browser how to style its **native UI components**.

πŸ”₯ Summary

The color-scheme property helps developers create seamless light/dark mode experiences by letting the browser automatically style built-in UI parts like scrollbars, inputs, and form controls. It improves accessibility, consistency, and user experience.

>>β€œLet the browser handle native UI theming β€” you focus on design.”

Learn more β†’MDN Docs πŸ“˜