---
title: "How do I add dark mode in CSS? prefers-color-scheme, color-scheme and light-dark()"
description: "Build dark mode with plain CSS: color-scheme tells the browser which themes you support, light-dark() picks colours, and prefers-color-scheme handles the rest."
url: https://www.danny.engineering/answer/css/how-do-i-add-dark-mode-in-css-prefers-color-scheme-color-scheme-and-light-dark
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["CSS"]
---

# How do I add dark mode in CSS? prefers-color-scheme, color-scheme and light-dark()

## The short answer


Declare which themes your page supports with `color-scheme`, then give each colour a light and a dark value with `light-dark()`. The browser picks the right value from the user's system setting and also switches its own form controls and scroll bars to match.


```css
:root {
  color-scheme: light dark;
  --bg: light-dark(#ffffff, #121212);
  --text: light-dark(#1a1a1a, #e8e8e8);
}

body {
  background: var(--bg);
  color: var(--text);
}
```


## Why it happens


There are three separate pieces of dark mode in CSS, and they are often confused.

- **`prefers-color-scheme`** is a media feature. It reports whether the user's operating system or browser is set to light or dark. It does not change anything on its own.
- **`color-scheme`** is a property. It tells the browser which colour schemes an element can be shown in. With `light dark`, the browser uses dark defaults for things it draws itself: the page background, form inputs, buttons, scroll bars and the text cursor.
- **`light-dark()`** is a colour function. It returns its first value when the element's used colour scheme is light and its second when it is dark. It only works when `color-scheme` allows both.

If you only write a `prefers-color-scheme` media query, your own colours change but native controls stay light, so you end up with white date pickers and scroll bars on a dark page. If you only set `color-scheme`, the browser switches its defaults but your custom colours stay the same.


`light-dark()` has been supported in Chrome, Edge, Firefox and Safari since 2024.


## How to fix it


### Use color-scheme with light-dark()


This is the shortest setup for current browsers. Keep colours in custom properties so the theme lives in one place.


```css
:root {
  color-scheme: light dark;
  --surface: light-dark(#f7f7f8, #1c1c1f);
  --border: light-dark(#d4d4d8, #3f3f46);
  --link: light-dark(#0b57d0, #8ab4f8);
}
```


Also add the `color-scheme` meta tag to the document head. The browser reads it before your CSS loads, which avoids a white flash on dark systems.


```html
<meta name="color-scheme" content="light dark" />
```


### Use prefers-color-scheme for anything that is not a colour


`light-dark()` only accepts colours. For images, shadows built from several values or other changes, use the media query.


```css
.hero {
  background-image: url('/hero-light.jpg');
}

@media (prefers-color-scheme: dark) {
  .hero {
    background-image: url('/hero-dark.jpg');
  }
}
```


For images in HTML, the `picture` element accepts the same media query:


```html
<picture>
  <source srcset="/logo-dark.svg" media="(prefers-color-scheme: dark)" />
  <img src="/logo-light.svg" alt="Company logo" />
</picture>
```


### Add a manual theme toggle


Because `light-dark()` follows the element's `color-scheme`, a toggle only needs to change that one property. Set an attribute on the root element from JavaScript and save the choice.


```css
:root {
  color-scheme: light dark;
}

:root[data-theme='light'] {
  color-scheme: light;
}

:root[data-theme='dark'] {
  color-scheme: dark;
}
```


```javascript
function setTheme(theme) {
  if (theme === 'system') {
    delete document.documentElement.dataset.theme;
  } else {
    document.documentElement.dataset.theme = theme;
  }
  localStorage.setItem('theme', theme);
}
```


Run the code that reads the saved theme in a small inline script in the head, before the page paints, to avoid a flash of the wrong theme.


### Support older browsers with the media query


If you need to support browsers without `light-dark()`, define the dark values in a media query instead. It is more verbose but works everywhere.


```css
:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --text: #1a1a1a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --text: #e8e8e8;
  }
}
```


## Common pitfalls

- Using `light-dark()` without setting `color-scheme`. The function always returns the light value.
- Forgetting the meta tag. Without it, dark-mode users can see a white flash before the stylesheet loads.
- Using pure black and pure white. Very high contrast is tiring to read; slightly softer values usually work better. Check contrast ratios in both themes.
- Forgetting that `color-scheme` inherits. Setting `color-scheme: dark` on one component switches every `light-dark()` value and native control inside it, which is handy for an always-dark header but surprising if done by accident.
- Inverting the whole page with `filter: invert(1)`. It also inverts photos and brand colours.

## Related reading

- [MDN: color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme)
- [MDN: light-dark()](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark)
- [MDN: prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme)
