# Dark Mode & color-scheme

> Implement Dark Mode effortlessly using the color-scheme property and the new light-dark() CSS function.

Subject: CSS | Lesson 42 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/css/dark-mode-and-color-scheme
Previous: [Logical Properties & i18n](https://codewithanimation.in/css/logical-properties-and-i18n.md) | Next: [Naming, BEM & Design Tokens](https://codewithanimation.in/css/naming-bem-and-tokens.md)

## The evolution of Dark Mode

In the early days of dark mode, developers had to write extensive `@media (prefers-color-scheme: dark)` blocks to manually override the colors of every single element.

Later, we moved to CSS Variables, swapping `--bg-color: white` to `--bg-color: black` inside a `.dark-theme` class.

Modern CSS introduces a native, streamlined approach using the `color-scheme` property and the `light-dark()` function, removing the need for media queries entirely.

## Less code, native UI support

By simply adding `color-scheme: light dark;` to your `:root`, you immediately get dark mode support for native browser elements. Scrollbars, form inputs, checkboxes, and default backgrounds will automatically adapt to the user's OS preference.

For custom elements, the `light-dark()` function allows you to define both the light and dark colors inline. `background-color: light-dark(white, black);` is infinitely more readable than maintaining a separate stylesheet for dark overrides.

## Implementing light-dark()

First, opt-in at the root level: `:root { color-scheme: light dark; }`.

Next, use the function for your colors: `.card { background: light-dark(#ffffff, #1a1a1a); color: light-dark(#333333, #f5f5f5); }`.

The browser will read the current `color-scheme` (which defaults to the OS preference) and automatically select the first color for light mode, or the second color for dark mode.

## Common mistake: Forcing a specific theme

Often, you want to provide a toggle button so users can manually override their OS preference. You cannot use `light-dark()` if you don't control the `color-scheme`.

To do this, you simply change the `color-scheme` property on the `:root` or a specific container. Applying `color-scheme: dark;` to a specific section will instantly force all `light-dark()` functions inside that section to evaluate to their dark values.

## Tip: Combining with Custom Properties

For large codebases, combine `light-dark()` with CSS Variables. Define your tokens once: `--surface: light-dark(#fff, #222);`. Then use `var(--surface)` everywhere. Your entire app will swap themes instantly.

## Try it: Try it

Toggle the `color-scheme` property of the container below. Watch the `light-dark()` functions and native UI elements instantly repaint.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/dark-mode-and-color-scheme

## Check yourself

### Question 1

What does `color-scheme: light dark;` applied to the `:root` do?

- A. It tells the browser the page supports both modes, allowing it to automatically theme native UI like scrollbars.
- B. It automatically inverts all colors on the page.
- C. It prevents the OS from changing the website's theme.
- D. It is a legacy property replaced by `prefers-color-scheme`.

Correct answer: A

Without this property, native browser elements (like scrollbars and select dropdowns) will remain glaringly white even if you manually style your `<body>` to be dark.

- Why not B: It does not invert colors; it only changes native UI and enables functions like `light-dark()`.
- Why not C: It actually *enables* the website to respond to the OS theme.
- Why not D: It works *alongside* media queries and is the modern standard for opting-in.

### Question 2

Given `color: light-dark(#ff0000, #0000ff);`, which color will be displayed if the user's OS is in Dark Mode?

- A. Blue (#0000ff)
- B. Red (#ff0000)
- C. Purple (Mixed)
- D. Black

Correct answer: A

The `light-dark()` function takes two arguments: the value for light mode, and the value for dark mode. Since the OS is in Dark Mode, it picks the second argument (Blue).

- Why not B: The first argument is the light mode color.
- Why not C: You are thinking of `color-mix()`. `light-dark()` picks one or the other.
- Why not D: It will evaluate to the second argument provided.

### Question 3

How can you manually force a specific container (and all its children) to display in dark mode, overriding the OS preference?

- A. Apply `color-scheme: dark;` to that container.
- B. Apply `@media (prefers-color-scheme: dark)` to that container.
- C. Use `filter: invert(100%);` on the container.
- D. You cannot override the OS preference using `light-dark()`.

Correct answer: A

The `light-dark()` function resolves based on the computed `color-scheme` of the element. Forcing `color-scheme: dark;` on a wrapper instantly switches all nested `light-dark()` functions to their dark values.

- Why not B: Media queries cannot be scoped to specific containers; they read the global OS state.
- Why not C: While this makes it dark, it messes up images and colors. It's not a true theme.
- Why not D: You absolutely can, by controlling the computed `color-scheme` property.

## Remember this

- Add `color-scheme: light dark;` to `:root` to get free dark-themed scrollbars and form inputs.
- Use `light-dark(light_value, dark_value)` to define both themes inline without media queries.
- Force a theme manually by overriding the `color-scheme` property on a specific element.
