Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

CSS  Module 6 – Colour and theming

Dark mode, colour schemes and forced colours

Support light and dark themes with color-scheme, prefers-color-scheme and light-dark(), add a theme switch, and keep pages usable in forced colours.

  • Intermediate
  • 30 minutes
  • Examples run with Node.js 24.21.0 and quickjs 0.32.0
  • By MySmartCoPilot
Free core, Pro depth: 11 of 30 minutes free
Free: Declare the supported schemes. With a pass: 1 in-depth part and 3 interview answers, about 19 minutes.

What you will learn

  • Support light and dark themes with color-scheme, prefers-color-scheme and light-dark()
  • Add a user theme switch that overrides the system preference without duplicating rules
  • Check every colour pair of a theme in both schemes
  • Design pages that stay usable in forced-colours (high-contrast) mode

Before you start

What you’ll learn · Before you start

What you will learn

  • Support light and dark themes with color-scheme, prefers-color-scheme and light-dark()
  • Add a user theme switch that overrides the system preference without duplicating rules
  • Check every colour pair of a theme in both schemes
  • Design pages that stay usable in forced-colours (high-contrast) mode

Before you start

On this page

In the supplied menu case, System Light with Dark selected gives a dark card and field but a light preference banner. Those results follow different inputs: a page’s supported and The light or dark scheme an element actually uses after its supported schemes and the user preference are considered., the system preference, and an explicit menu choice.

Declare scheme support with color-scheme and the early HTML meta value, then let light-dark select scheme-dependent colours. Keep every pair checked in both schemes. A user mode in which the browser substitutes a chosen palette and adjusts affected visual properties. is a separate accessibility setting; status labels and an outline must still carry meaning when author colours or shadows disappear.

Declare the supported schemes

Colour schemes tell the browser whether an element supports light, dark or both. Colour schemes also affect the browser’s own controls and system palette.

Dark mode takes three pieces. The user’s setting reaches the page as the prefers-color-scheme media feature, light or dark. The page says which schemes it supports with the color-scheme property, usually color-scheme: light dark on the root element, or with a <meta name="color-scheme"> element; that one declaration lets the browser draw form controls, scrollbars, the page background and its CSS keywords that refer to browser or operating-system palette colours rather than fixed authored RGB values. in the dark scheme. Your own colours then switch with light-dark(), which takes a light and a dark value, or with rules inside @media (prefers-color-scheme: dark). A theme menu that overrides the system needs only one change: set color-scheme on the root to light, dark or light dark. Forced colours mode, which the contrast themes of Windows turn on, goes further: it replaces the page’s colours with the user’s own palette, so a page must stay usable without its colours.

This lesson measures each piece in Chromium 153, Firefox 155 and WebKit 26.6 with light, dark and forced colours emulated, and reproduces the two mistakes that break most dark themes.

color-scheme takes normal, the default, or a list of the schemes an element is designed for: light, dark, or both. When the user’s preferred scheme is in the list, the element uses it; otherwise it uses the first scheme listed; with normal and nothing else on the page, it supports no scheme and gets the browser’s default, which is light. The keyword only, as in only light, also forbids the browser from forcing its own automatic dark version on the element. The property is inherited, so :root { color-scheme: light dark; } covers the whole page, and a part of the page can still opt out with its own value.

The used scheme changes what the browser draws for you: the default colours of form controls, scrollbars and other built-in parts, and, on the root, the page’s canvas behind everything. It also changes the system colours. This page reads them in two panels, one with color-scheme: light and one with color-scheme: dark:

A stylesheet loads after the HTML has started to render, so a page that sets color-scheme only in CSS can flash white for a moment in dark mode. The HTML Standard’s answer is the meta element, which the browser reads before any CSS: <meta name="color-scheme" content="light dark">. Use it together with the CSS property. Both are Baseline widely available.

Interactive · step by step Support affects browser surfaces Follow the root's supported schemes and a dark system preference.
Support affects browser surfaces, step 3 of 3. Showing Supported schemes (light dark), System preference (dark), Used scheme (dark). In focus: Used scheme.readdecideSupported schemeslight darkSystem preferencedarkUsed schemedark

The drawing shows the whole scene; its 3 steps are listed below.

Every step as text
  1. Declare what is supported. The root says the page supports light and dark. This lets a dark preference select the dark used scheme for the supplied case.
  2. Read the browser surfaces. Native controls, scrollbars and system colours use that scheme. A separate plain author colour does not automatically become its dark counterpart.
  3. Declare before CSS arrives. The matching HTML meta declaration lets the browser know the support earlier. Keep the CSS property too because it applies to elements and is inherited.

The supplied panels retain every system-colour observation. The page declares which schemes it supports; the browser selects a used scheme for its own surfaces. The early HTML meta declaration and the CSS property serve different points in loading.

Checkpoint

Check: Declare the supported schemes

  1. Question 1 of 2 The root supports light dark and the supplied system preference is dark. What changes with the used scheme?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: Native controls and system colours

    These browser-managed surfaces use the supported and selected scheme.

  2. Question 2 of 2 The page supports both schemes but initially flashes a light canvas before its CSS. Which declaration addresses early support?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The matching color-scheme meta declaration in the HTML

    The browser can read support before the stylesheet arrives.

Light-dark selects from the element’s used scheme, not directly from the preference query. The unsupported card therefore stays on its light value even when the system prefers dark.

A theme menu represents an explicit choice separately from the system preference. The used scheme changes scheme-dependent values, while preference-driven rules keep reporting the system.

Forced colours can substitute the user’s palette and remove a shadow-only focus cue. Keep status words and a surviving outline so meaning and focus do not depend on the author colours.

Trace theme inputs, native surfaces and surviving cues Pro: opens with a Pro, Premium or Ultimate pass

Follow all six original menu and preference combinations without merging their inputs. Keep the native-field, contrast and forced-colour cases beside the rules they test.

In this part:

  • Compare the recorded system-colour panels
  • Resolve light-dark from the scheme
  • Separate menu choice from preference
  • Keep meaning without author colours
  • Implement the scheme and cue rules
  • Work out the colour scheme a browser uses, and what forced colours remove
    • Results of the sample tests
  • Keep the depth decisions together

4 diagrams · 5 runnable examples (HTML, JavaScript) · 1 exercise with sample tests · about 20 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Practise: separate the input from the chosen value

Practice that adapts to you

Practise this lesson

2 questions about 4 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 2 Why does a page add <meta name="color-scheme" content="light dark"> when its CSS already sets color-scheme: light dark on :root?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: So the browser knows before the CSS loads, and can paint the first frame dark instead of flashing white

    The HTML Standard defines the meta element so the browser can render the page background with the right scheme immediately, rather than waiting for all the CSS. Without it, a dark-mode visitor may see a white page for a moment on a slow connection.

  2. Question 2 of 2 The root supports light dark and uses dark, but an authored hex card remains pale. Which conclusion follows?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: Scheme support changes browser-managed surfaces; plain authored colours need their own theme rule

    Separate the used scheme from the authored colour values. A page supplies its own scheme-dependent values while browser-managed controls and system colours use the selected scheme.

Interview questions

Fresher · What does color-scheme: light dark on the root element do?

It tells the browser that the page is designed for both a light and a dark colour scheme, so the browser may use the dark one when the user prefers it. The browser then draws everything it controls in that scheme: the page background, form controls, scrollbars and the system colours such as Canvas and Field. It also decides which value light-dark() picks, so light-dark(white, black) turns black only where the used scheme is dark. It does not change any colour the page sets itself with a plain value. Without it, the used scheme stays light even when the user prefers dark, which is why light-dark() seems not to work. A <meta name="color-scheme" content="light dark"> element says the same before the CSS loads, which avoids a white flash.

  1. Mid to senior

    After dark mode was added with @media (prefers-color-scheme: dark) rules, users report a white flash on load and white inputs on dark pages, and a new theme menu recolours the page but not the native controls. Explain each symptom and fix them.

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Mid to senior

    A monitoring dashboard shows service health with green and red chips and draws keyboard focus with box-shadow. In Windows contrast themes users can't tell the services apart or see where focus is. What do you change, and how do you test it?

    Model answer, Pro. First step and follow-ups

  3. Mid to staff

    A remembered theme menu overrides the system preference. A later system change unexpectedly resets the menu, native fields disagree with the page, and forced-colour users lose focus. Specify the independent inputs and tests without deleting any of the original six menu/preference cases.

    Model answer, Pro. First step and follow-ups

Key takeaways

  • Put color-scheme: light dark on :root and the matching meta element in the HTML: form controls, scrollbars, the canvas and system colours then follow the reader’s scheme.

  • light-dark() follows the used colour scheme, not prefers-color-scheme: without color-scheme it always picks its light value.

  • A theme menu needs one change, color-scheme on the root; rules written inside prefers-color-scheme media queries ignore it.

Terms in this lesson

Forced colours
A user mode in which the browser substitutes a chosen palette and adjusts affected visual properties.
System colours
CSS keywords that refer to browser or operating-system palette colours rather than fixed authored RGB values.
Used colour scheme
The scheme selected for an element from its supported schemes and the user’s preference.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.