Creating responsive interfaces that transition smoothly between light and dark visual themes historically required extensive CSS media queries (@media (prefers-color-scheme: dark)) duplicated across multiple selectors. Modern CSS introduces the native light-dark() colour function, allowing developers to define contextual colour tokens directly within a single declaration. Safari on macOS offers integrated tooling within its Web Inspector to inspect computed colour spaces, override colour schemes on a per-element basis, and simulate appearance environments without changing system-wide preferences.
Understanding the CSS light-dark() Function
The light-dark() function accepts two colour arguments: the first is rendered when the computed colour scheme is light, and the second is rendered when the scheme is dark:
:root {
color-scheme: light dark;
--bg-primary: light-dark(#ffffff, #121212);
--text-primary: light-dark(#1f2937, #f9fafb);
}
For light-dark() to resolve correctly, an ancestor element or the :root selector must explicitly declare the color-scheme property. If color-scheme is omitted or set to normal, the browser defaults to the light value regardless of the user’s operating system theme.
Prerequisites and Inspector Setup
To access developer features in Safari on macOS:
- Launch Safari and open Settings (or press Cmd + ,).
- Click on the Advanced tab.
- Check the box labelled Show features for web developers (in older macOS versions: Show Develop menu in menu bar).
- Right-click on any element within a webpage and select Inspect Element to launch Safari Web Inspector.
Simulating Appearance Modes in Safari Web Inspector
Rather than toggling your entire macOS appearance between Light and Dark in System Settings, Safari allows instantaneous mode switching within the inspector interface:
- Open the Elements tab in Safari Web Inspector.
- Locate the device and preference emulation toolbar at the top of the Elements or Console pane.
- Find the Appearance Emulation icon (depicted as a half-filled circle representing sun/moon states).
- Click the icon to cycle between:
- System Default: Inherits your current macOS appearance.
- Force Light Appearance: Evaluates
color-scheme: lightand renders the first argument inlight-dark(). - Force Dark Appearance: Evaluates
color-scheme: darkand renders the second argument inlight-dark().
- Observe how the rendered page elements and computed CSS values update instantaneously.
Inspecting Resolved light-dark() Properties in the Styles Pane
When selecting an element that employs light-dark(), Safari Web Inspector provides visual indicators to distinguish which branch of the function is actively applied:
- In the Elements tree, select the target DOM node.
- Inspect the Styles sidebar on the right.
- Examine the property rule (e.g.,
background-color: light-dark(#ffffff, #1a1a1a);). - Safari highlights the active colour token while dimming the inactive token, making it immediately apparent whether the light or dark branch was evaluated.
- Click on the adjacent colour swatch next to the declaration to open the Web Inspector Colour Picker. You can adjust either colour token interactively, convert colour spaces (sRGB, Display P3, OKLCH), and verify contrast ratios against adjacent background layers.
Debugging Scoped color-scheme Overrides
A major advantage of light-dark() is that it respects locally scoped color-scheme properties. For instance, a dark card container can be embedded within an otherwise light page layout:
.dark-card {
color-scheme: dark;
background-color: light-dark(#f3f4f6, #1f2937);
color: light-dark(#111827, #f9fafb);
}
To verify scoped resolution in Safari:
- Select the child container in the Elements DOM tree.
- Navigate to the Computed styles tab in the sidebar.
- Filter for
color-schemeto confirm that the computed value showsdarkrather than inheritinglightfrom:root. - Check the
colorandbackground-colorproperties to verify that the dark variant values were assigned to the element.
Common Pitfalls and Best Practices
- Missing color-scheme Declaration: If
light-dark()fails to switch when toggling emulation, confirm thatcolor-scheme: light dark;is declared on the:rootorhtmlselector. - Wide Gamut Colours: Combine
light-dark()with modern colour notations such asoklch()to ensure vibrant, consistent gamut mapping across Apple Retina displays. - Automated Audits: Use the Audit tab within Safari Web Inspector to test for accessibility contrast failures in both light and dark operational states.