$html-docs
InstallDevelopersPricingSign inOpen drive
All HTML guides
Foundations6 min read·July 29, 2026

How to Add Dark Mode Without Breaking Your HTML

Respect system preferences, preserve contrast, and keep native controls and embedded content legible in both themes.

The short version

Dark mode is a complete color system, not an inverted background.

01

Declare that both schemes are supported

The color-scheme property tells the browser which schemes your page can render. That helps native controls, scrollbars, and form fields match the surrounding interface.

css
:root {
  color-scheme: light dark;
  --surface: #fafaf7;
  --text: #151513;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: #151513;
    --text: #e8e8e2;
  }
}
02

Design each role, not each hex value

Map colors to jobs such as surface, text, muted text, border, action, warning, and focus. Review the relationship between those roles in both modes. Pure white on pure black can feel harsher than a tuned near-white and near-black pair.

03

Test the awkward parts

If you offer a manual switch, save the preference locally and apply it before the page becomes visible to avoid a theme flash. The system preference remains a sensible default for first-time visitors.

  • Form controls and validation states.
  • Code blocks, charts, and screenshots.
  • Logos with transparent backgrounds.
  • Focus indicators and text selection.
  • Printed output, which usually needs a light theme.

Official references

  • web.dev: Color schemes
  • W3C: Media Queries Level 5

In this guide

  1. 01Declare that both schemes are supported
  2. 02Design each role, not each hex value
  3. 03Test the awkward parts
Publish a page

Keep going

Related HTML guides

Browse all 24 guides
Foundations7 min read

How to Write Semantic HTML That Stays Useful

Choose elements for their meaning, build a clear document outline, and make your page easier to navigate, edit, and reuse.

Read guide
Foundations6 min read

A Reusable HTML Page Structure for New Projects

Begin with a small, valid document shell that supports accessibility, responsive design, metadata, and progressive enhancement.

Read guide
Foundations7 min read

How to Organize CSS Around Your HTML

Keep selectors shallow, components readable, and design tokens explicit so a page remains easy to revise after launch.

Read guide
© 2026 HTML DocsInstallSupportPrivacyTermsAPI