Sub topic 12
Testing both themes in developer tools
This chapter assumes you have built the theme from earlier chapters. That means CSS custom properties, a prefers-color-scheme media query, and a data-theme toggle saved in localStorage. You should also know how to open your browser's developer tools. Here you will check that each theme works without changing your operating system settings.
Why test with emulation
A dark theme has several states to check. The system can be light or dark, and the saved choice can be light, dark, or absent. Switching your operating system back and forth is slow, and it changes every other app too. Developer tools can override the media query for one tab only.
Emulating prefers-color-scheme
Each browser has its own control.
| Browser | Where to find it |
|---|---|
| Chrome and Edge | Open DevTools, then the command menu (Ctrl+Shift+P or Cmd+Shift+P), run "Show Rendering", and set "Emulate CSS media feature prefers-color-scheme". |
| Firefox | In the Inspector, use the sun and moon buttons in the Rules panel toolbar. |
| Safari | In the Elements tab, use the appearance button to force light or dark. |
Emulation changes what matchMedia and your @media (prefers-color-scheme: dark) rules report. It does not change the operating system or other tabs. Switch between light and dark and look for three problems:
- Text that stays dark on a dark background, which means a color was hard-coded instead of using a token.
- Form controls, scrollbars, or the page canvas that stay light. This usually means
color-schemeis missing from:root. - Borders and shadows that disappear on the dark background.
Inspecting tokens and the cascade
Select <html> in the Elements panel. In the Styles pane you can see which :root block currently defines --bg and --text. Overridden declarations appear with a strikethrough, which shows whether the media query or the data-theme rule is winning.
You can edit values live. Click a color swatch for --bg and change it. Every element that uses var(--bg) updates at once. That is a quick way to try palette changes before editing your stylesheet.
Select an element and open the Computed pane to see the final resolved color. Use it when a light-dark() call or a nested variable makes the source hard to trace.
Testing the manual toggle and stored choice
The toggle adds a second input, so test the combinations in turn. Emulate each system scheme, then set each saved value, and check that the saved value always wins.
You can set the state from the console instead of clicking:
// Force a saved dark choice, then reload to test the early script.
localStorage.setItem("theme", "dark");
location.reload();To return to "system" mode, remove the stored value and the attribute:
localStorage.removeItem("theme");
document.documentElement.removeAttribute("data-theme");
console.log(
"System dark:",
window.matchMedia("(prefers-color-scheme: dark)").matches
);The log line reports what the browser currently believes, including any emulation. If it disagrees with the colors on screen, a rule is overriding the media query.
In Chrome, the Application panel lists localStorage for the origin. You can edit or delete the theme key there. In Firefox, use the Storage panel.
Looking for a flash of the wrong theme
The flash is hard to see on a fast local machine. Slow it down:
- Set the saved choice to the theme that differs from your system (for example, saved
darkwhile emulating light). - Throttle the network, for example to a "Slow 4G" preset in the Network panel.
- Reload with the cache disabled and watch the first frame.
If the page paints light and then turns dark, the script that sets data-theme runs too late. It must be inline in the <head>, before the stylesheet-dependent content paints. The Performance panel's screenshots option shows each frame, so you can see exactly where the change happens.
Common errors
- Emulation seems stuck. The Rendering setting persists while DevTools stays open. Set it back to "No emulation" before testing system behavior.
- A cached stylesheet hides your edit. Tick "Disable cache" in the Network panel while DevTools is open.
- The toggle works but a reload resets it. Check that the key name in
setItemmatches the key you read in the head script. - Only part of the page changes. Search the stylesheet for raw hex values that bypass your tokens.
Exercise
You emulate a dark system scheme, and the page is dark. You click the toggle to choose light, reload, and the page is dark again. Name two things you would check, and the DevTools panel for each.
Show answer
First, check that the toggle saved the choice: open the Application panel (Storage in Firefox) and confirm that localStorage has theme set to "light". Second, check that the head script applies it before paint and that the dark media query uses :not([data-theme="light"]). Select <html> in Elements to see whether data-theme="light" is present, and look in the Styles pane to see which rule is winning.