The light-dark function selects one of two color values according to the element's active color scheme. The color-scheme property tells the browser which schemes a region supports and also affects native controls.
CSS light-dark: bind component colors to the active scheme
When to use it
A case detail panel appears in light and dark dashboard themes. Give the region permission to use both schemes, then select matching foreground and background colors. The sample includes a readable light fallback before the enhanced rules. Do not apply color-scheme light dark to a section whose icons, borders, or embedded images are still designed only for light mode. The function follows the active scheme, which can come from user preference or an ancestor's scheme choice; it is not a JavaScript theme switch. Check native inputs, links, focus rings, images, and print in both modes. A card that looks fine in isolation can fail against a dark page edge or a transparent overlay.
.case-detail { color: #173148; background: #f7fafc; }
@supports (color: light-dark(black, white)) { .case-detail { color-scheme: light dark; color: light-dark(#173148, #f2f7fa); background: light-dark(#f7fafc, #142a38); } }Cost and verification
The function's runtime cost is small; the testing matrix is not. Every interactive state needs paired colors, and images with fixed light backgrounds may need special treatment. Color-scheme can change browser-rendered form controls and scrollbars, so review them rather than assuming only your CSS values change. Fallback declarations should preserve a complete light presentation if the function is unavailable. Do not state that dark mode is complete after switching only text and panel background. Verify contrast on the final composite, including disabled and error states.
Common Mistakes
- Do not opt into dark controls without styling the rest of the region.
- Do not switch the background without a matching foreground.
- Do not treat light-dark as a replacement for testing both schemes.
