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.
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.
The drawing shows the whole scene; its 3 steps are listed below.
Every step as text
- 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.
- 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.
- 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
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.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
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.
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.
-
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.
-
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?
-
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.
Key takeaways
-
Put
color-scheme: light darkon:rootand 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, notprefers-color-scheme: withoutcolor-schemeit always picks its light value. -
A theme menu needs one change,
color-schemeon the root; rules written insideprefers-color-schememedia 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
- CSS Color Adjustment Module Level 1: the color-scheme property (W3C CSS Working Group)
- CSS Color Adjustment Module Level 1: effects of the used colour scheme (W3C CSS Working Group)
- CSS Color Adjustment Module Level 1: properties affected by forced colors mode (W3C CSS Working Group)
- CSS Color Adjustment Module Level 1: the forced-color-adjust property (W3C CSS Working Group)
- CSS Color Module Level 5: the light-dark() function (W3C CSS Working Group)
- Media Queries Level 5: prefers-color-scheme and forced-colors (W3C CSS Working Group)
- HTML Standard: the color-scheme meta name (WHATWG)
- CSS Color Module Level 4: system colours (W3C CSS Working Group)
- Web Content Accessibility Guidelines (WCAG) 2.2: Use of Color (W3C)
- light-dark() (MDN Web Docs) (Mozilla)
- color-scheme (MDN Web Docs) (Mozilla)
- forced-colors (MDN Web Docs) (Mozilla)
- light-dark() (web-features explorer) (W3C WebDX Community Group)
- Forced colors (web-features explorer) (W3C WebDX Community Group)
- Page: emulateMedia (Playwright documentation) (Microsoft (Playwright))
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress