Light and dark themes

W3C CSS Snapshot 2026

A theme is a set of colors used together. Name shared colors with CSS custom properties, then change those properties when the chosen theme changes.

On this page

Follow the system’s color preference

prefers-color-scheme is a media feature. It can match the user’s dark preference. color-scheme tells the browser which theme to use for native controls as well.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Follow the system’s color preference</title>
  <style>
    :root { --page: #fff; --ink: #17233a; color-scheme: light; }
    @media (prefers-color-scheme: dark) {
      :root { --page: #17233a; --ink: #fff; color-scheme: dark; }
    }
    body { background: var(--page); color: var(--ink); }
  </style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>

What to expect Toggle your system color preference or emulate prefers-color-scheme in DevTools to see the page colors swap.

Set a page theme with an attribute

An attribute selector can select a specific setting on an element. [data-theme="dark"] matches an element with that exact attribute value. A future JavaScript button can change the attribute when the reader chooses a theme.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Set a page theme with an attribute</title>
  <style>
    .panel { --page: #fff; --ink: #17233a; }
    .panel[data-theme="dark"] { --page: #17233a; --ink: #fff; }
    .panel { background: var(--page); color: var(--ink); padding: 12px; }
  </style>
</head>
<body>
<section class="panel" data-theme="dark"><p>Pens and notebooks</p></section>
</body>
</html>

What to expect The panel is dark. Change data-theme to light, save, and reload to use the base colors.

Official references