Keep your coding agent up to date with the latest web best practices
Release v0.0.157
github-actions[bot]a0a6a79e054c
diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.jsonindex a9b9369..4c2ec5e 100644--- a/.claude-plugin/marketplace.json+++ b/.claude-plugin/marketplace.json@@ -8,7 +8,7 @@"name": "modern-web-guidance","source": "./","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.156"+ "version": "0.0.157"}]}diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.jsonindex a864d17..069bb86 100644--- a/.claude-plugin/plugin.json+++ b/.claude-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "modern-web-guidance","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.156",+ "version": "0.0.157","author": {"name": "Google Chrome"}diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.jsonindex a864d17..069bb86 100644--- a/.cursor-plugin/plugin.json+++ b/.cursor-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "modern-web-guidance","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.156",+ "version": "0.0.157","author": {"name": "Google Chrome"}diff --git a/README.md b/README.mdindex 8b377c8..196beb1 100644--- a/README.md+++ b/README.md@@ -26,7 +26,7 @@ Our content is evergrowing, we cover the bleeding edge of the web platform as we### Core disciplines-Here's a tiny sampling of the **124 use-case-centric guides**:+Here's a tiny sampling of the **123 use-case-centric guides**:* **User Experience**: Smooth and modern visual states: View Transitions, CSS `scrollbar-color` styling, high-contrast adaptation, entry/exit transition animations, parallax scrolling.* **CSS layout:** container queries (both size and style queries), modern color spaces (`oklch`, `color-mix`) and `subgrid`, text-wrap tuning (`balance`, `pretty`), subgrid, and typography line height trimming (`text-box`)@@ -36,7 +36,7 @@ Here's a tiny sampling of the **124 use-case-centric guides**:* **Accessibility & Security**: accessible error announcements, keyboard focus management.* **Built-in AI**: Leveraging local, on-device client models (native Language Detection, Summarization, and Translation APIs).-#### Full Skill Coverage (v0.0.156)+#### Full Skill Coverage (v0.0.157)<details><summary>Includes expert guidance across <strong>99 modern web features</strong></summary>@@ -143,7 +143,7 @@ Here's a tiny sampling of the **124 use-case-centric guides**:</details><details>-<summary>Covers <strong>124 real-world developer use cases</strong> with production-ready code patterns</summary>+<summary>Covers <strong>123 real-world developer use cases</strong> with production-ready code patterns</summary><h3>accessibility</h3>@@ -216,25 +216,24 @@ Here's a tiny sampling of the **124 use-case-centric guides**:<h3>user-experience</h3>- **[adapt-scrollbar-to-contrast-preferences](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/adapt-scrollbar-to-contrast-preferences.md)**: Enhance scrollbar visibility for users who prefer high-contrast interfaces-- **[adapt-scrollbar-to-light-dark-preferences](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/adapt-scrollbar-to-light-dark-preferences.md)**: Ensure the scrollbar visually matches the user's operating system light/dark mode preference- **[anchor-positioning-tab-underline](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/anchor-positioning-tab-underline.md)**: Transition an element seamlessly between two target element positions. For example, moving a selected tab underline between the previously selected tab and the currently selected tab.- **[animate-element-entry-exit](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/animate-element-entry-exit.md)**: Smoothly hide/show elements as they are added/removed from the DOM or as their display values are toggled.- **[animate-to-from-top-layer](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/animate-to-from-top-layer.md)**: Animate elements such as dialogs, popovers, and tooltips as they're entering/exiting the top layer.- **[animate-to-intrinsic-sizes](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/animate-to-intrinsic-sizes.md)**: Smoothly animate interactive components (like accordions, menus, and expanding cards) to and from their natural dimensions.- **[apply-webgl-shaders](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/apply-webgl-shaders.md)**: Apply custom visual effects with WebGL shaders to HTML content.-- **[browser-ui-color-theme](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/browser-ui-color-theme.md)**: Configure built-in browser UI (e.g. scrollbars, form controls, etc) to respect the user's light/dark theme preference.- **[calculate-event-differentials](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/calculate-event-differentials.md)**: Calculate the duration and time remaining between dates and times.- **[calculate-with-intrinsic-sizes](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/calculate-with-intrinsic-sizes.md)**: Calculate the size of an element based on its intrinsic size, while ensuring it fits within given design constraints.- **[capture-location-agnostic-data](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/capture-location-agnostic-data.md)**: Record chronological data that should not change based on a user's location, such as birthdates, recurring alarms, or national holidays.- **[carousel-slide-effects](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/carousel-slide-effects.md)**: Create a carousel of slides with images or other visual elements, where each slide animates as they enter/center/exit their scroller. For example, the slides may fade-in/fade-out, rotate, get bigger or smaller, etc.- **[carousel-snap-highlights](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/carousel-snap-highlights.md)**: Visually highlight the currently snapped non-interactive item in scroll-snapping carousels, galleries, or full-page swipe experiences. For example, expanding a card when snapped, or revealing hidden content.- **[child-state-based-styling](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/child-state-based-styling.md)**: Build a component that changes its styling based on the state of one of its child elements. For example, a component that renders in light or dark mode based on whether a theme toggle is checked (or not).-- **[component-specific-light-dark-theme](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/component-specific-light-dark-theme.md)**: Create component-specific themes by forcing explicit color schemes on individual UI elements, giving users theme choices that are decoupled from their global operating system preferences+- **[component-specific-light-dark-theme](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/component-specific-light-dark-theme.md)**: Force certain elements to be in light mode or dark mode (e.g. code blocks, media players, etc) independently of the page's color-scheme.- **[consistent-cross-document-transitions](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/consistent-cross-document-transitions.md)**: Ensure critical page state is loaded and stable before initiating a cross-document view transition. This means critical CSS styles are loaded and applied, critical JavaScript is loaded and run, and the HTML visible for the user's initial view of the page has been parsed before the transition runs.- **[content-based-styling](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/content-based-styling.md)**: Build a component that changes its layout based on whether it contains specific child elements (or not). For example, if the component contains an image, use a multi-column layout, otherwise default to a single-column layout.- **[coordinate-global-events](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/coordinate-global-events.md)**: Schedule future meetings or events by explicitly binding them to a geographical IANA time zone so that event times remain accurate regardless of Daylight Saving Time (DST) transitions, "skipped" or "repeated" hours during clock changes.- **[cross-document-transitions](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/cross-document-transitions.md)**: Create smooth, seamless transitions between full page navigations, such as cross-fades, custom reveal effects, or morphing of content from one page to the next.- **[customize-scrollbar-color-and-thickness](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/customize-scrollbar-color-and-thickness.md)**: Customize the color or thickness of a scrollbar+- **[dark-mode](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/dark-mode.md)**: Implement dark mode support in a way that respects the user's light/dark theme preference and adapts browser UI (e.g. scrollbars, form controls, etc)- **[declarative-button-actions](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/declarative-button-actions.md)**: Declaratively connect a button to any element to trigger custom, application-specific actions using declarative button commands, invoker commands, button commands, custom commands, or declarative toggle actions.- **[declarative-dialog-popover-control](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/declarative-dialog-popover-control.md)**: Toggle the visibility of a dialog or popover from a button without writing JavaScript.- **[deliver-optimized-decorative-images](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/deliver-optimized-decorative-images.md)**: Deliver optimized decorative images (such as backgrounds, UI icons, or complex masks) by simultaneously providing next-generation image formats (like AVIF or WebP) alongside multiple pixel densities (like 1x and 2x) so the browser can dynamically negotiate the best combination of file size and visual quality for the user's device capabilities.diff --git a/gemini-extension.json b/gemini-extension.jsonindex a864d17..069bb86 100644--- a/gemini-extension.json+++ b/gemini-extension.json@@ -1,7 +1,7 @@{"name": "modern-web-guidance","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.156",+ "version": "0.0.157","author": {"name": "Google Chrome"}diff --git a/package.json b/package.jsonindex b384d50..bd0db0a 100644--- a/package.json+++ b/package.json@@ -3,7 +3,7 @@"displayName": "Modern Web Guidance","description": "Keep your agent up to date on the latest web best practices.","license": "Apache-2.0",- "version": "0.0.156",+ "version": "0.0.157","publisher": "GoogleChrome","engines": {"vscode": "^1.90.0",diff --git a/skills/modern-web-guidance/guides/css-layout/css-layout.md b/skills/modern-web-guidance/guides/css-layout/css-layout.mdindex e3b663d..3c46dad 100644--- a/skills/modern-web-guidance/guides/css-layout/css-layout.md+++ b/skills/modern-web-guidance/guides/css-layout/css-layout.md@@ -261,7 +261,7 @@ Manage layout shifts, scrollbars, and clipping predictably.}```-> For `overflow: clip` and `overflow-clip-margin` in depth, see `overflow-clipping-control` (via `npx -y modern-web-guidance@latest retrieve "overflow-clipping-control"`). For scrollbar color, sizing, and theming, see `customize-scrollbar-color-and-thickness` (via `npx -y modern-web-guidance@latest retrieve "customize-scrollbar-color-and-thickness"`), `adapt-scrollbar-to-light-dark-preferences` (via `npx -y modern-web-guidance@latest retrieve "adapt-scrollbar-to-light-dark-preferences"`), and `adapt-scrollbar-to-contrast-preferences` (via `npx -y modern-web-guidance@latest retrieve "adapt-scrollbar-to-contrast-preferences"`).+> For `overflow: clip` and `overflow-clip-margin` in depth, see `overflow-clipping-control` (via `npx -y modern-web-guidance@latest retrieve "overflow-clipping-control"`). For scrollbar color, sizing, and theming, see `customize-scrollbar-color-and-thickness` (via `npx -y modern-web-guidance@latest retrieve "customize-scrollbar-color-and-thickness"`), `dark-mode` (via `npx -y modern-web-guidance@latest retrieve "dark-mode"`), and `adapt-scrollbar-to-contrast-preferences` (via `npx -y modern-web-guidance@latest retrieve "adapt-scrollbar-to-contrast-preferences"`).## 7 Viewport mechanics and track distributiondiff --git a/skills/modern-web-guidance/guides/css/css.md b/skills/modern-web-guidance/guides/css/css.mdindex 90afd9c..2485c97 100644--- a/skills/modern-web-guidance/guides/css/css.md+++ b/skills/modern-web-guidance/guides/css/css.md@@ -238,8 +238,9 @@ Check for any existing conventions around naming and levels before inventing you- Use `color-scheme: light dark` on `:root` to enable dark mode support that automatically adapts to the system setting. You can also specify `color-scheme` on individual elements to force a different value for that subtree (`light`/`dark` or `light dark` for the system default)- Use `light-dark()` to provide alternatives that automatically resolve based on the element's `color-scheme`.Typically this happens in Tier 2 or Tier 3 tokens.-- IMPORTANT: When using `light-dark()` on an inherited `<color>` property, it will resolve to a specific color based on that element's `color-scheme` and inherit as that resolved color, not as a `light-dark()` value. It will NOT adapt to any descendant-specific `color-scheme` overrides. To keep `light-dark()` color tokens dynamic resolve them as late as possible by only passing them around as unregistered custom properties.+- IMPORTANT: When using `light-dark()` on an inherited `<color>` property, it will resolve to a specific color based on that element's `color-scheme` and inherit as that resolved color, not as a `light-dark()` value. It will NOT adapt to any descendant-specific `color-scheme` overrides. To keep `light-dark()` color tokens dynamic resolve them as late as possible by only passing them around as unregistered custom properties and avoid relying on inherited color values across `color-scheme` boundaries.+See `dark-mode` (via `npx -y modern-web-guidance@latest retrieve "dark-mode"`) for tips & best practices on supporting dark mode switching and `component-specific-light-dark-theme` (via `npx -y modern-web-guidance@latest retrieve "component-specific-light-dark-theme"`) for more on applying different `color-scheme` modes than the page-wide setting on certain elements.### Forced Colors Modediff --git a/skills/modern-web-guidance/guides/user-experience/adapt-scrollbar-to-light-dark-preferences.md b/skills/modern-web-guidance/guides/user-experience/adapt-scrollbar-to-light-dark-preferences.mddeleted file mode 100644index 2226fcf..0000000--- a/skills/modern-web-guidance/guides/user-experience/adapt-scrollbar-to-light-dark-preferences.md+++ /dev/null@@ -1,112 +0,0 @@-# Adapt scrollbar colors to light/dark preferences--Users expect UI components, including scrollbars, to match the color scheme (light or dark mode) chosen in their operating system settings. The most essential step to achieving this is utilizing the `color-scheme` property. If you choose to apply explicit custom colors, you can use the `@media (prefers-color-scheme: dark)` CSS media feature to modify those colors dynamically.--## 1. System Default Adaptation--The simplest and most robust way to ensure the scrollbar adapts to the user's light/dark mode preference is to let the browser handle it via the `color-scheme` property. When a dark color scheme is enabled, the browser will automatically render its dark-variant scrollbar.--MANDATORY: Define `color-scheme` on the `:root` pseudo-class.--```css-:root {- /* DO: Declare support for both light and dark systems */- color-scheme: light dark;-}-```--## 2. Custom Color Adaptation--If you are using `scrollbar-color` or the non-standard `::-webkit-scrollbar` pseudo-elements to explicitly define custom scrollbar colors, you MUST ensure these colors are legible and appropriate in both light and dark modes.--When using `scrollbar-color`, use CSS variables to keep thumb and track colors separate, for readability and maintainability (especially when using fallbacks).--```css-.scroller {- --scrollbar-thumb: var(--color-neutral-70);- --scrollbar-track: var(--color-neutral-90);-- scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);-}-```--You can use either the `prefers-color-scheme` media query or the `light-dark()` function to define how these colors behave in light and dark mode. The latter will adapt to color-scheme overrides as well (local or global), but has narrower browser support.--Using `light-dark()`:--```css-/* DO: Define light mode colors as variables */-.scroller {- --scrollbar-thumb: light-dark(#bbb, #555555);- --scrollbar-track: light-dark(#f1f1f1, #222222);-- scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);- scrollbar-width: thin;- scrollbar-gutter: stable;-}-```--Using `prefers-color-scheme`:--```css-/* DO: Define light mode colors as variables */-.scroller {- --scrollbar-thumb: #bbb;- --scrollbar-track: #f1f1f1;-- scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);- scrollbar-width: thin;- scrollbar-gutter: stable;-}--/* DO: Override variables for dark mode */-@media (prefers-color-scheme: dark) {- .scroller {- --scrollbar-thumb: #555555;- --scrollbar-track: #222222;- }-}-```--### Issues to be aware of when using scrollbar-color--- Do NOT animate or transition `scrollbar-color`. A [WebKit bug](https://bugs.webkit.org/show_bug.cgi?id=311752) causes the scrollbar to flicker every time `scrollbar-color` changes.-- On macOS, `scrollbar-color` (standard) and `::-webkit-scrollbar` (legacy) properties are ignored by default because macOS uses native "overlay" scrollbars. You MUST pair custom colors with `scrollbar-width` (e.g., `thin` or `auto`) to force macOS to render them.-- Even with `scrollbar-width` applied, macOS overlay scrollbars render the track (gutter) as transparent by default. If the design requires a visible track background color on MacOS, you MUST apply `scrollbar-gutter: stable;` to the scrollable container, but note that it only appears after the user hovers over the scrollbar.-- Even with `scrollbar-gutter: stable` the track may be transparent on MacOS. The thumb should not depend on the track color to be visible.--## Fallbacks & Browser Support--### Fallbacks & browser support for scrollbar-color--Baseline status for scrollbar-color: Newly available. It's been Baseline since 2025-12-12.-Supported by: Chrome 121 (Jan 2024), Edge 121 (Jan 2024), Firefox 64 (Dec 2018), and Safari 26.2 (Dec 2025).--This feature is progressive enhancement and does not always require fallbacks.--If the styling is important and the user's Baseline target is "Baseline Widely Available" or earlier, you SHOULD include the non-standard `::-webkit-scrollbar` pseudo-elements as fallbacks.--Wrap legacy fallbacks in an `@supports not (scrollbar-color: auto)` block to prevent conflicts between standard properties and legacy WebKit selectors in browsers that support both natively.--If you are using custom properties to define colors, these will cascade to the legacy WebKit selectors automatically. You do NOT need to duplicate them.--```css-/* Legacy fallback for WebKit/Blink browsers */-@supports not (scrollbar-color: auto) {- .scroller::-webkit-scrollbar {- /* Must define base size in WebKit for custom colors to be visual */- width: 12px;- height: 12px;- }-- .scroller::-webkit-scrollbar-thumb {- background: var(--scrollbar-thumb);- }-- .scroller::-webkit-scrollbar-track {- background: var(--scrollbar-track);- }-}-```--diff --git a/skills/modern-web-guidance/guides/user-experience/browser-ui-color-theme.md b/skills/modern-web-guidance/guides/user-experience/browser-ui-color-theme.mddeleted file mode 100644index 43391b0..0000000--- a/skills/modern-web-guidance/guides/user-experience/browser-ui-color-theme.md+++ /dev/null@@ -1,159 +0,0 @@-# Browser UI color theme--The `color-scheme` property indicates which color schemes (such as light or dark) your page supports. This informs the browser that it can automatically theme native UI elements—like scrollbars, form controls, and the default canvas background—to match your site's design and help minimize white flashes during initial loading.--## Implementation--### 1. Declare supported schemes in HTML-MANDATORY: To help prevent a "flash of un-themed content" (FOUC), place a `<meta>` tag in your `<head>` to ensure the browser knows which themes you support before it even starts rendering. While this `<meta>` tag helps to avoid FOUC by setting the initial canvas color early, it may not completely eliminate flashes in all browsers or loading conditions.--```html-<!-- MANDATORY: Declare support for both light and dark themes -->-<meta name="color-scheme" content="light dark">-```--### 2. Apply theme to CSS :root or html-MANDATORY: Apply the `color-scheme` property to the `html` element or the `:root` pseudo-class. Browsers specifically look to the root element to determine the theme for the entire viewport—including the root scrollbars and the initial "canvas" background. If applied only to the `body`, these global UI surfaces may remain in light mode because the `body` does not control the window's rendering context.--```css-/* MANDATORY: Apply color-scheme to :root or html for viewport-wide theming */-:root {- /* MANDATORY: Automatically adapt native UI to user system preferences */- color-scheme: light dark;-}-```--### 3. Component-Specific Overrides-You can override the global theme for specific elements. This is useful for "dark mode" sections within a light-themed site (e.g., a dark sidebar or a code editor).--```css-pre, code {- /* Forces element and its children to use dark themed UI */- color-scheme: dark;-}-```--### 4. OPTIONAL: Fine-grained control with custom properties and `light-dark()`-For more control over the colors of built-in UI such as `accent-color` or `scrollbar-color`, authors **can optionally** add their own dynamic colors with use of custom properties and/or the `light-dark()` function. This function automatically picks the correct color based on the computed `color-scheme` of the element and eliminates the need for redundant media queries, but is not required for a basic implementation.--```css-:root {- /* Define browser UI accent color for each mode */- --brand-accent-light: #0056b3; /* Deep blue for light mode */- --brand-accent-dark: #00e5ff; /* Bright cyan for dark mode */--- /* Define colors for other parts of the theme */- --brand-bg-light: hsl(0, 0%, 100%);- --brand-bg-dark: hsl(212, 33%, 10%);-- --brand-text-light: hsl(210, 100%, 2%);- --brand-text-dark: hsl(0, 0%, 100%);-- /* Optional: Use light-dark() for more control of built-in UI colors */- --background-color: light-dark(var(--brand-bg-light), var(--brand-bg-dark));- --text-color: light-dark(var(--brand-text-light), var(--brand-text-dark));-- /* Set dynamic browser UI accent color for each mode */- --accent-color: light-dark(var(--brand-accent-light), var(--brand-accent-dark));-- /* MANDATORY: Automatically adapt native UI to user system preferences */- color-scheme: light dark;-}--body {- background-color: var(--background-color);- color: var(--text-color);- /* Apply custom accent color to UI elements*/- accent-color: var(--accent-color);-}--pre, code {- /* Apply dark scheme to built-in UI elements */- color-scheme: dark;- /* **Mandatory**: when nesting schemes, dynamic built-in inherited properties (e.g. color, accent-color) set with- light-dark() must also be set on the element where the scheme changes.-- When setting a different color-scheme on a component, **DO NOT** redefine custom properties (e.g. --accent-color: light-dark(black, white);)- unless the custom property needs to be updated. The custom property only needs- to be applied to a property (e.g. accent-color: var(--accent-color);).- */- accent-color: var(--accent-color);- /* Set colors for other theme styles */- background-color: var(--background-color);- color: var(--text-color);-}-```--## Best Practices- - The `@property` Risk **:- - **Mandatory**: Do not register properties meant to be design tokens that dynamically switch based on `light-dark()` as `<color>`. If you need to animate a color variable, use a separate property. `light-dark()` resolves at computed value time, and registered properties inherit resolved, computed values, so the color would no longer be reactive to local theme changes. Descendant elements that change their `color-scheme` (e.g., a dark-themed card on a light page) will inherit the *already-resolved color* instead of the dynamic function.- - Built-in inherited properties such as `color` and `accent-color` can also be affected by this issue. To prevent this, always reassign the dynamic theme variable on these built-in properties when they are inside of a nested theme.- - **Example: Handling Built-In Inherited Properties**-- ```css- :root {- /* Define browser UI accent color for each mode */- --brand-accent-light: #0056b3;- --brand-accent-dark: #00e5ff;- /* OPTIONAL: Apply dynamic color switching based on light-dark */- --accent-color: light-dark(var(--brand-accent-light), var(--brand-accent-dark));-- /* MANDATORY: Automatically adapt native UI to user system preferences */- color-scheme: light dark;- /* Set built-in inherited property */- accent-color: var(--accent-color);- }-- textarea {- color-scheme: dark;- /* **Mandatory**: when nesting schemes, dynamic values set with light-dark() on built-in inherited properties must also be re-assigned on the element where the scheme changes */- accent-color: var(--accent-color);- }- ```-- **System Colors**: Use system color keywords like `Canvas` (background) and `CanvasText` (text) for your custom components when you want them to match the browser's native themed surfaces exactly. This is ideal for ensuring consistency between your content and the browser's UI (like scrollbars) while automatically respecting OS-level accessibility features like High Contrast mode.-- **Respect User Preference**: **MANDATORY**: Avoid forcing a single theme on the overall page. While specific components (like a code editor) may benefit from a fixed theme, the main application should respect the user's system preference and ideally provide a manual toggle to allow users to choose between light, dark, or system-default modes.-- **Forcing a Theme**: Use a single value like `color-scheme: dark` or `color-scheme: light` ONLY for specific sections of your site (like a code editor or a video player) that must remain in one theme. Avoid applying this to the root element unless it's the result of an explicit user selection via a theme toggle.-- **Opting out of Auto-Dark Mode**: Use `color-scheme: only light` to prevent browsers (particularly on mobile) from automatically inverting your colors if you haven't yet implemented a dedicated dark theme or prefer not to show the component with a dark theme.--## Fallback strategies--Baseline status for color-scheme: Widely available. It's been Baseline since 2022-02-03.-Supported by: Chrome 98 (Feb 2022), Edge 98 (Feb 2022), Firefox 96 (Jan 2022), and Safari 13 (Sep 2019).-Baseline status for light-dark(): Newly available. It's been Baseline since 2024-05-13.-Supported by: Chrome 123 (Mar 2024), Edge 123 (Mar 2024), Firefox 120 (Nov 2023), and Safari 17.5 (May 2024).--- **Progressive Enhancement**: Browsers that do not support `color-scheme` will ignore this property and use their default light-mode UI.-- **Handling `light-dark()` Support**: For browsers that support `color-scheme` but not yet `light-dark()`, light and dark versions of colors should first be defined as custom properties, and the `prefers-color-scheme` media query should be used to set colors for the respective mode like in the example below:--```css-:root {- /* Define browser UI accent color for each mode */- --brand-accent-light: #0056b3;- --brand-accent-dark: #00e5ff;- --accent-color: var(--brand-accent-light, AccentColor);-- /* MANDATORY: Automatically adapt native UI to user system preferences */- color-scheme: light dark;- /* Set accent color */- accent-color: var(--accent-color);-- /* MANDATORY: Fallback for browsers without light-dark support */- @media (prefers-color-scheme: dark) {- --accent-color: var(--brand-accent-dark);- }-- /* OPTIONAL: use light-dark() for more control of built-in UI colors */- @supports (color: light-dark(white, black)) {- --accent-color: light-dark(var(--brand-accent-light), var(--brand-accent-dark));- }-}--textarea {- color-scheme: dark;- /* **Mandatory**: when nesting schemes, dynamic values set with light-dark() used in inherited properties must also be re-assigned on the element where the scheme changes */- accent-color: var(--accent-color);-}-```-- **Manual Dark Mode Styling**: For older browsers, continue to use `prefers-color-scheme` media queries to provide custom styles for your own components.-- **Custom Scrollbars**: If you need consistent scrollbar styling across all modern browsers, use the `scrollbar-color` property. For older WebKit-based browsers that do not support the standard property, continue to use the non-standard `::-webkit-scrollbar` pseudo-elements.diff --git a/skills/modern-web-guidance/guides/user-experience/component-specific-light-dark-theme.md b/skills/modern-web-guidance/guides/user-experience/component-specific-light-dark-theme.mdindex d07def2..c547849 100644--- a/skills/modern-web-guidance/guides/user-experience/component-specific-light-dark-theme.md+++ b/skills/modern-web-guidance/guides/user-experience/component-specific-light-dark-theme.md@@ -1,177 +1,179 @@-# Component-Specific Light/Dark Themes+# Component-specific light/dark themes-The `light-dark()` function allows you to define two colors for a single property, which the browser automatically switches based on the current `color-scheme`. By scoping both the `light-dark()` variables and the `color-scheme` property to a specific component, you can create UI elements that can be independently forced into light or dark mode, regardless of the user's global system preference.+While more commonly set on the root, the `color-scheme` property can be set on individual elements to force them into a different color scheme from the rest of the page.+This can be useful for components that must always be viewed in a specific color scheme (e.g. always in dark or light mode).-## Why use component-specific themes?+Example use cases include:+- Elements that are often in dark mode even on light mode pages for aesthetic reasons, e.g. code blocks, media players, photo galleries+- Areas that contain media designed for a light background (e.g. images, videos, illustrations, print previews) can be set to light mode even if the rest of the page is in dark mode.+- Elements whose color-scheme is controlled by a user-level setting, such as component previews+- Embeds that don't support both light and dark modes+- Design tools, maps, visualizations, games etc.-While most element's should follow the page-wide color scheme, there are several scenarios where you need more granular control over an element's theme:+## When to change colors vs. when to force `color-scheme`?-- **Content-Driven Aesthetics**: Certain components work best in a specific mode. For example, **code editors**, **video players**, and **photo galleries** often use a particular theme to minimize distraction and make colors "pop," regardless of the user's OS setting.-- **User Preference Overrides**: You can empower users to manually toggle the theme of a specific component, like a chat box or a music player for example. This allows them to view a component in the mode that fits their environment or eye comfort.+Not every element that uses lighter text on darker background in light mode or darker text on lighter background in dark mode needs a different `color-scheme`.+For example, a primary button may be rendered as blue with white text in light mode, but that does not warrant a `color-scheme: dark`.-## When to Change Colors vs. When to Force `color-scheme`+As a rule of thumb, typically elements using a different `color-scheme` are complex surfaces establishing their own visual context, rather than simple shallow containers.-### 1. Change Colors when:-* You only need to update certain properties of author styled elements (like the background of a `<div>` or the color of an `<h1>`).-* The component doesn't contain any "native" interactive parts.-* **Result:** Only the parts you explicitly styled will change.+When considering using a different `color-scheme` on an element, ask yourself:-### 2. Force `color-scheme` when:-* You've decided that the built-in browser UI like **scrollbars** (e.g., a scrollable code block or sidebar) or **form controls** (like `<select>` menus, checkboxes, or date pickers) should use the colors of a particular color scheme.-* The component is best viewed in a particular color mode based on its content, for example, the dominant colors of a video, image, or graphic.-* **Result:** The browser automatically themes the "hidden" parts you can't easily reach with CSS, or the component is always viewed as intended in the design.+- Should built-in browser UI that is not otherwise customized (e.g. form controls, scrollbars, etc) use that color-scheme or adapt to the page's color-scheme? -> if the former, don't use `color-scheme`.+- Should any `light-dark()` colors resolve like they do for the rest of the page or based on the override? -> if the former, don't use `color-scheme`.+- Should descendants be in that `color-scheme`? If not, don't use `color-scheme`.-## Implementation Steps+## Basic implementation-### 1. Declare supported schemes in HTML-OPTIONAL: To help prevent a "flash of un-themed content" (FOUC), place a `<meta>` tag in your `<head>` to ensure the browser knows which themes you support before it even starts rendering. While this `<meta>` tag helps to avoid FOUC by setting the initial canvas color early, it may not completely eliminate flashes in all browsers or loading conditions.+Component-specific overrides are typically (though not strictly necessarily) used on pages that also support multiple color schemes via a global `color-scheme`.+For implementing page-wide dark mode well, see `dark-mode` (via `npx -y modern-web-guidance@latest retrieve "dark-mode"`).-```html-<!-- Optional: Declare support for both light and dark themes -->-<meta name="color-scheme" content="light dark">-```--### 2. Enable `color-scheme` support-OPTIONAL: Enable global support for both color schemes by setting `color-scheme: light dark;` on the `:root`.+Once a page-wide `color-scheme` is in place, and you are using color tokens sensitive to it (e.g. via `light-dark()`), you can simply set `color-scheme` on specific components to override the color mode for that subtree:```css-:root {- /* **Optional:** Enable global support for both color schemes */- color-scheme: light dark;+pre, code, .dark {+ color-scheme: dark;}```-### 3. Define theme-aware variables-Using custom properties creates a **semantic abstraction layer** for your component's theme. This allows your styling to remain constant while the *values* of your variables adapt to the theme context. It also simplifies fallback management and provides the essential mechanism for re-resolving values in nested "theme islands" to avoid inheritance issues without repeating complex logic on every property.+Note that some browsers automatically adapt components to a different color scheme anyway.+To force the specified color scheme in all cases, use `only`, i.e. `color-scheme: only dark;` instead of `color-scheme: dark;`.-```css-.themed-card {- /* 1. Define raw brand colors for each mode */- --card-bg-light: #ffffff;- --card-bg-dark: #2d2e31;- --card-text-light: #202124;- --card-text-dark: #f8f9fa;-- /* 2. Modern enhancement using light-dark() */- --card-bg: light-dark(var(--card-bg-light), var(--card-bg-dark));- --card-text: light-dark(var(--card-text-light), var(--card-text-dark));-- /* 3. Apply custom properties on relevant properties values */-- background-color: var(--card-bg);- /* **Mandatory**: Dynamic built-in properties that inherit (like color and accent-color) and are set with light-dark() must be applied on the element where the scheme changes.- */- color: var(--card-text);- padding: 1.5rem;- border-radius: 8px;-}-```+### Adapting non-color values++`light-dark()` currently only works for colors.++## Best practices++- **MANDATORY**: Do not set `color-scheme` on elements without a background, as that risks mixing background and text color pairs from different color-schemes, resulting in unreadable text.+- **OPTIONAL**: While it is easier to reuse the same color pairs as the page-wide dark mode, we _can_ define different color pairs for these components. For example, we may want a dark mode component used in a light mode page to be a little less dark than when the same dark mode component is used in a page that is overall in dark mode.++## Known issues to be aware of-### 4. Create theme overrides-Force a component instance into a specific theme by setting its `color-scheme` property. This allows the component to ignore the global system preference.+### Important gotcha: Inheritance of `light-dark()` colors++**`light-dark()` resolves at computed value time.**+This means that any inherited `<color>` properties set to a `light-dark()` color will only pass down one of the two colors to their descendants, not the `light-dark()` expression itself.++This includes:+- Built-in color properties that inherit, such as `color`, `accent-color`, `fill`, `stroke`, `text-shadow`, `caret-color`+- Any registered inheritable custom properties with `syntax: <color>` and `inherits: true`+- Any other `<color>` property set to `inherit`++This means you should:+- **NOT** register custom properties meant to hold *design tokens* (e.g. `--surface-color`) as `<color>`. Tokens need to keep their `light-dark()` expression live so descendants can re-resolve them under a different `color-scheme`.+- When setting `color-scheme` on an element, re-specify any inherited `<color>` properties that may have been set to `light-dark()` values (directly or via design tokens), even if that's to the same design token.+- **NOT** use `inherit` on `<color>` properties on elements with a `color-scheme` override (fine to use on their descendants).+- **DO** use registered `<color>` properties for the *opposite* use case: when you deliberately want to snapshot the ancestor's resolved color and prevent it from re-resolving under the descendant's `color-scheme`. For example, capturing the page background to use elsewhere.+- If you need to animate a color, use a separate `@property`-registered `<color>` property on the element being animated (registration is required for color interpolation) — this is not a design token, but a per-element animation target, so it does not conflict with the rule above.++Example:```css-/* Force this specific card into dark mode */-.themed-card.force-dark {- /* Dynamic custom properties don't need to be reapplied here since they were already defined on `.themed-card` */- /* **Mandatory**: force component to use a particular color mode */- color-scheme: dark;+:root {+ --accent-color: light-dark(blue, skyblue);+ --surface-color: light-dark(white, #222);+ --text-color: light-dark(#111, white);++ color-scheme: light dark;+ accent-color: var(--accent-color);+ color: var(--text-color);+}++body {+ /* --surface-color dynamically switches despite being inherited because --surface-color is not registered */+ background: var(--surface-color);}-/* Force this specific card into light mode */-.themed-card.force-light {- /* Dynamic custom properties don't need to be reapplied here since they were already defined on `.themed-card` */- /* **Mandatory**: Force component to use a particular color mode */- color-scheme: light;+pre, code {+ color-scheme: dark;+ background: var(--surface-color);++ /* Without this, accent-color would be blue, not skyblue! */+ accent-color: var(--accent-color);++ /* Without this, text-color would be #111, not white! */+ color: var(--text-color);}```-## Critical Considerations--- **The `@property` Risk**:- - **Mandatory**: Do not register properties meant to be design tokens that dynamically switch based on `light-dark()` as `<color>`. If you need to animate a color variable, use a separate property.-- **Mandatory**: Do not set `color-scheme` on elements without a background.- - The `@property` Risk **:- - **Mandatory**: Do not register properties meant to be design tokens that dynamically switch based on `light-dark()` as `<color>`. If you need to animate a color variable, use a separate property. `light-dark()` resolves at computed value time, and registered properties inherit resolved, computed values, so the color would no longer be reactive to local theme changes. Descendant elements that change their `color-scheme` (e.g., a dark-themed card on a light page) will inherit the *already-resolved color* instead of the dynamic function.- - Built-in inherited properties such as `color` and `accent-color` can also be affected by this issue. To prevent this, always reassign the dynamic theme variable on these built-in properties when they are inside of a nested theme.- - **Example: Handling Built-In Inherited Properties**-- ```css- :root {- /* Define browser UI accent color for each mode */- --brand-accent-light: #0056b3;- --brand-accent-dark: #00e5ff;- /* OPTIONAL: Apply dynamic color switching based on light-dark */- --accent-color: light-dark(var(--brand-accent-light), var(--brand-accent-dark));-- /* MANDATORY: Automatically adapt native UI to user system preferences */- color-scheme: light dark;- /* Set built-in inherited property */- accent-color: var(--accent-color);- }-- textarea {- color-scheme: dark;- /* **Mandatory**: when nesting schemes, dynamic values set with light-dark() on built-in inherited properties must also be re-assigned on the element where the scheme changes */- accent-color: var(--accent-color);- }- ```-- **System Colors**: Use system color keywords like `Canvas` (background) and `CanvasText` (text) for your custom components when you want them to match the browser's native themed surfaces exactly. This is ideal for ensuring consistency between your content and the browser's UI (like scrollbars) while automatically respecting OS-level accessibility features like High Contrast mode.-- **Respect User Preference**: **MANDATORY**: Avoid forcing a single theme on the overall page. While specific components (like a code editor) may benefit from a fixed theme, the main application should respect the user's system preference and ideally provide a manual toggle to allow users to choose between light, dark, or system-default modes.-- **Forcing a Theme**: Use a single value like `color-scheme: dark` or `color-scheme: light` ONLY for specific sections of your site (like a code editor or a video player) that must remain in one theme. Avoid applying this to the root element unless it's the result of an explicit user selection via a theme toggle.-- **Opting out of Auto-Dark Mode**: Use `color-scheme: only light` to prevent browsers (particularly on mobile) from automatically inverting your colors if you haven't yet implemented a dedicated dark theme or prefer not to show the component with a dark theme.+### Issues to be aware of when using color-scheme++- Chrome and Firefox respect `color-scheme` for iframes: they render embedded pages in the correct color scheme and adjust the embedded page's `prefers-color-scheme` media query to reflect the embedding context's `color-scheme`. Safari does not, and resolves `prefers-color-scheme` to the system setting even inside iframes.+ - **If you control both parent and iframe:** pass the parent's color scheme to the iframe explicitly — via a URL parameter (`?theme=dark`) at iframe construction time, or via `postMessage()` (which also lets you react to runtime changes). In the iframe, set a class on `<html>` (and/or `color-scheme` on `:root`) from that signal instead of relying on `prefers-color-scheme`.+ - **If you only control the embedded page:** there is no reliable way to detect the embedding context's `color-scheme` from inside the iframe in Safari. Expose an explicit theme parameter on your embed API (e.g. a query string or `postMessage` protocol) and document it for embedders.## Fallback strategies-Baseline status for light-dark(): Newly available. It's been Baseline since 2024-05-13.-Supported by: Chrome 123 (Mar 2024), Edge 123 (Mar 2024), Firefox 120 (Nov 2023), and Safari 17.5 (May 2024).+### Fallbacks & browser support for color-scheme+Baseline status for color-scheme: Widely available. It's been Baseline since 2022-02-03.Supported by: Chrome 98 (Feb 2022), Edge 98 (Feb 2022), Firefox 96 (Jan 2022), and Safari 13 (Sep 2019).-- **Non-Color Properties**: Currently, `light-dark()` only supports color values. For other properties (like `padding` or `border-width`), you must continue using standard media queries or CSS Style Queries.-- **Progressive Enhancement**: Browsers that do not support `color-scheme` will ignore this property and use their default light-mode UI.-- **Handling `light-dark()` Support**: For browsers that support `color-scheme` but not yet `light-dark()`, light and dark versions of colors should first be defined as custom properties, and the `prefers-color-scheme` media query should be used to set colors for the respective mode like in the example below:+The `color-scheme` property is **progressive enhancement**.+Browsers that do not support it will ignore this property and use their default light-mode UI.++To adapt to the user's preferences in older browsers, use `prefers-color-scheme` media queries to provide different colors when dark mode is preferred.++- DO use the media query to switch custom properties on `:root` or `html`+- Avoid using the media query on individual components unless the component requires a very specific type of dark mode customization beyond colors.```css-.themed-card {- /* 1. Define raw brand colors */- --card-bg-light: #ffffff;- --card-bg-dark: #2d2e31;- --card-text-light: #202124;- --card-text-dark: #f8f9fa;-- /* 2. Assign default (light) values */- --card-bg: var(--card-bg-light);- --card-text: var(--card-text-light);-- /* 3. Apply custom properties on relevant properties (light) values */-- background-color: var(--card-bg);- /* **Mandatory**: Dynamic built-in properties that inherit (like color and accent-color) and are set with light-dark() must be applied on the element where the scheme changes.- */- color: var(--card-text);- padding: 1.5rem;- border-radius: 8px;+:root {+ /* Define brand colors for each mode */+ --color-brand-light: #0056b3;+ --color-brand-dark: #00e5ff;+ --color-brand: var(--color-brand-light);++ /* MANDATORY: Fallback for browsers without light-dark support */+ @media (prefers-color-scheme: dark) {+ --color-brand: var(--color-brand-dark);+ }++ /* Ignored in older browsers */+ color-scheme: light dark;}-/* 3. Fallback for browsers without light-dark() support but with prefers-color-scheme */-@media (prefers-color-scheme: dark) {- .themed-card {- --card-bg: var(--card-bg-dark);- --card-text: var(--card-text-dark);- }+button.primary {+ background-color: var(--color-brand);}+```++### Fallbacks & browser support for light-dark()-/* 4. Modern enhancement using light-dark() */-@supports (color: light-dark(white, black)) {- .themed-card {- --card-bg: light-dark(var(--card-bg-light), var(--card-bg-dark));- --card-text: light-dark(var(--card-text-light), var(--card-text-dark));+Baseline status for light-dark(): Newly available. It's been Baseline since 2024-05-13.+Supported by: Chrome 123 (Mar 2024), Edge 123 (Mar 2024), Firefox 120 (Nov 2023), and Safari 17.5 (May 2024).++For browsers that support `color-scheme` but not yet `light-dark()`, light and dark versions of colors should first be defined as custom properties, and the `prefers-color-scheme` media query should be used to set colors for the respective mode like in the example below:++```css+:root {+ /* Define browser UI accent color for each mode */+ --brand-accent-light: #0056b3;+ --brand-accent-dark: #00e5ff;+ --accent-color: var(--brand-accent-light);++ /* MANDATORY: Fallback for browsers without light-dark support */+ @media (prefers-color-scheme: dark) {+ --accent-color: var(--brand-accent-dark);}++ /* OPTIONAL: use light-dark() for more control of built-in UI colors */+ @supports (color: light-dark(white, black)) {+ --accent-color: light-dark(var(--brand-accent-light), var(--brand-accent-dark));+ }++ /* MANDATORY: Automatically adapt native UI to user system preferences */+ color-scheme: light dark;++ /* Example inherited color property */+ accent-color: var(--accent-color);}-/* 5. Force a component into a specific color-scheme */-.themed-card.force-dark {+pre, code {color-scheme: dark;++ /* **Mandatory**: any inherited color properties must be set again, even if to the same design tokens */+ accent-color: var(--accent-color);}```diff --git a/skills/modern-web-guidance/guides/user-experience/dark-mode.md b/skills/modern-web-guidance/guides/user-experience/dark-mode.mdnew file mode 100644index 0000000..15aee24--- /dev/null+++ b/skills/modern-web-guidance/guides/user-experience/dark-mode.md@@ -0,0 +1,296 @@+# Dark mode++The `color-scheme` property indicates which color schemes (such as light or dark) your page supports. This informs the browser that it can automatically theme native UI elements—like scrollbars, form controls, and the default canvas background—to match your site's design and help minimize white flashes during initial loading.++## Implementation++### 1. Declare supported schemes in HTML++MANDATORY: To help prevent a "flash of un-themed content" (FOUC), place a `<meta>` tag in your `<head>` to ensure the browser knows which themes you support before it even starts rendering. While this `<meta>` tag helps to avoid FOUC by setting the initial canvas color early, it may not completely eliminate flashes in all browsers or loading conditions.++```html+<!-- MANDATORY: Declare support for both light and dark themes -->+<meta name="color-scheme" content="light dark">+```++### 2. Apply page-wide color scheme to CSS :root or html++MANDATORY: Apply the `color-scheme` property to the `html` element or the `:root` pseudo-class. Browsers specifically look to the root element to determine the theme for the entire viewport—including the root scrollbars and the initial "canvas" background. If applied only to the `body`, these global UI surfaces may remain in light mode because the `body` does not control the window's rendering context.++```css+/* MANDATORY: Apply color-scheme to :root or html for viewport-wide theming */+:root {+ /* MANDATORY: Automatically adapt native UI to user system preferences */+ color-scheme: light dark;+}+```++### 3. Define light and dark color tokens++You can use the `light-dark()` function to define color tokens that automatically adapt to different `color-scheme` values.++It is recommended that you also keep the raw color values in separate custom properties, which makes it easier to combine them in different ways (and makes fallback behavior easier, if needed).++For more control over the colors of built-in UI such as `accent-color` or `scrollbar-color`, authors **can optionally** add their own dynamic colors with use of custom properties and/or the `light-dark()` function. This function automatically picks the correct color based on the computed `color-scheme` of the element and eliminates the need for redundant media queries, but is not required for a basic implementation.++```css+:root {+ --color-brand-light: oklch(45% 0.23 270);+ --color-brand-dark: oklch(85% 0.15 210);+ --color-brand-text-light: white;+ --color-brand-text-dark: oklch(40% 0.23 270);++ --color-brand: light-dark(var(--color-brand-light), var(--color-brand-dark));+ --color-brand-text: light-dark(var(--color-brand-text-light), var(--color-brand-text-dark));++ /* MANDATORY: Automatically adapt native UI to user system preferences */+ color-scheme: light dark;+}++button.primary {+ /* These automatically adapt to color scheme */+ background-color: var(--color-brand);+ color: var(--color-brand-text);+}+```++OPTIONAL: A number of system colors are available, which also automatically adapt to the used color scheme (and other color modes, e.g. forced colors), such as `canvas`, `canvastext`, `accentcolor` (check support) , `buttonborder` etc. These are typically too limited to be useful, beyond very specific cases where you need to exactly match certain default browser UI or as fallbacks/defaults.++#### OPTIONAL: Tailor color pairs to context++Even when overriding the system default, it can be useful to use the `prefers-color-scheme` media query to define **different** color pairs that take into account the colors of the browser and OS chrome around the page (or of the surrounding page, when the page is used as an iframe).++For example, use a slightly dimmer light theme when the system setting is `dark`, or a more contrasting dark theme when the system setting is `light`, so the page is not visually overpowered by the surrounding UI.+++## Fine-grained browser UI customization++Setting `color-scheme` already adapts browser UI to the used color scheme, but this will use OS defaults and/or system colors that may not perfectly align with the website design.+Modern browsers expose several fine-grained customization hooks for these.+Do not reimplement native controls simply to customize their appearance without exhausting the customization hooks modern browsers provide.++### Setting the accent color++Some browser UI (e.g. checked checkboxes or sliders) uses an accent color.+This resolves to the OS setting by default, but you can use the `accent-color` property to set it to a color that better aligns with the page, such as the page's brand color.++```css+html {+ accent-color: light-dark(var(--color-accent-light), var(--color-accent-dark));+}+```++### Issues to be aware of when using accent-color++- When placing visual elements over the accent color (e.g. a checkbox checkmark), Chrome and Safari will automatically select a contrasting color, whereas Safari will modify the accent color, and may not maintain adequate contrast.++### Scrollbar colors++You can use `scrollbar-color` together with `light-dark()` to set custom scrollbar colors that adapt to the color scheme used.++```css+:root {+ --color-scrollbar-track: light-dark(#eee, #222);+ --color-scrollbar-thumb: light-dark(#999, #666);+ scrollbar-color: var(--color-scrollbar-thumb) var(--color-scrollbar-track);+}+```++### Issues to be aware of when using scrollbar-color++- Do NOT animate or transition `scrollbar-color`. A [WebKit bug](https://bugs.webkit.org/show_bug.cgi?id=311752) causes the scrollbar to flicker every time `scrollbar-color` changes.+- On macOS, `scrollbar-color` (standard) and `::-webkit-scrollbar` (legacy) properties are ignored by default because macOS uses native "overlay" scrollbars. You MUST pair custom colors with `scrollbar-width` (e.g., `thin` or `auto`) to force macOS to render them.+- Even with `scrollbar-width` applied, macOS overlay scrollbars render the track (gutter) as transparent by default. If the design requires a visible track background color on MacOS, you MUST apply `scrollbar-gutter: stable;` to the scrollable container, but note that it only appears after the user hovers over the scrollbar.+- Even with `scrollbar-gutter: stable` the track may be transparent on MacOS. The thumb should not depend on the track color to be visible.++### Further customization++Most browser UI exposes pseudo-elements to fully customize its appearance, such as:+- `::placeholder`+- `::spelling-error`+- `::grammar-error`+- `::selection`+- `::search-text`+- `::target-text`+- `::file-selector-button`++You can use `light-dark()` colors on any of these to apply colors that adapt to the used color scheme.++## OPTIONAL: Implementing a color-scheme toggle++**DO NOT** set `color-scheme: light` or `color-scheme: dark` on the root element by default.+The default color-scheme MUST be the user's system preference, which happens automatically when setting `color-scheme` to `light dark`.++For website-specific customization, a manual toggle could be provided to allow users to choose between light, dark, or system-default modes.++If a user-facing toggle to override it is desired, it should:+- Update the `<meta name="color-scheme">` element to reflect the chosen theme (`light dark` for system default, `light` for light, and `dark` for dark).+- If branching is desired for non-color values, set a class on `<html>` to match the theme preference and use descendant selectors. While `:root:has(> head > meta[name="color-scheme"][content="dark"])` would technically work, it is slower and confers no benefit, since we are already using JS to update the `<meta>` element.+- Persist user choice in `localStorage`.+- **IMPORTANT**: The CSS should be written to default to the system preference, with overrides for user-specified color-schemes. That way, if JS fails to execute, the site still defaults to the system color-scheme.+- The system-level OS theme can change at any time. If you are using JS to read `matchMedia("(prefers-color-scheme: dark)").matches`, you MUST also use `addEventListener("change", fn)` to react to changes. CSS automatically adapts to changes.+- **IMPORTANT**: To avoid a Flash of Unstyled Content (FOUC) for users who have pinned a different color scheme than their system default, use an inline script (NOT `type=module`, NOT `defer`) to set it when the page loads:++```html+<meta name="color-scheme" content="light dark">+<script>+{+ const colorScheme = localStorage.getItem("color-scheme");+ if (colorScheme) {+ document.querySelector('meta[name="color-scheme"]').content = colorScheme;+ }+}+</script>+```++### UX considerations++Use a two-state control:+1. System setting.+2. The opposite (e.g. light when the system setting is dark, and dark when the system setting is light). Selecting this setting must pin that exact color scheme, not a dynamically computed "opposite of system setting" value. Example scenario:+ 1. The OS is set to light mode.+ 2. The user selects the opposite setting for this website (dark).+ 3. The user changes their system setting to dark.+ 4. The website should remain dark.++**DON'T** expose all three states (system, light, dark). While the rationale is plausible — "Follow system (currently dark)" is a distinct user intent from "Always dark" — it provides suboptimal UX:+- Users cannot meaningfully express intent for problems they don't currently have. A manual toggle is a temporary comfort adjustment ("it's too bright right now"), not a long-term preference ("make sure this never changes").+- Two of the three options always produce the same visual result, violating the principle of feedback.++## Component-specific overrides++You can override the global theme for specific elements by setting `color-scheme` on them.+This is useful for "dark mode" sections within a light-themed site, such as code blocks or media players.++```css+pre, code {+ /* Forces element and its children to use dark themed UI */+ color-scheme: dark;+}+```++For more information about component-specific overrides and their gotchas, see `component-specific-light-dark-theme` (via `npx -y modern-web-guidance@latest retrieve "component-specific-light-dark-theme"`).++## Known issues to be aware of++### Issues to be aware of when using color-scheme++- Chrome and Firefox respect `color-scheme` for iframes: they render embedded pages in the correct color scheme and adjust the embedded page's `prefers-color-scheme` media query to reflect the embedding context's `color-scheme`. Safari does not, and resolves `prefers-color-scheme` to the system setting even inside iframes.+ - **If you control both parent and iframe:** pass the parent's color scheme to the iframe explicitly — via a URL parameter (`?theme=dark`) at iframe construction time, or via `postMessage()` (which also lets you react to runtime changes). In the iframe, set a class on `<html>` (and/or `color-scheme` on `:root`) from that signal instead of relying on `prefers-color-scheme`.+ - **If you only control the embedded page:** there is no reliable way to detect the embedding context's `color-scheme` from inside the iframe in Safari. Expose an explicit theme parameter on your embed API (e.g. a query string or `postMessage` protocol) and document it for embedders.++## Fallback strategies++### Fallbacks & browser support for color-scheme++Baseline status for color-scheme: Widely available. It's been Baseline since 2022-02-03.+Supported by: Chrome 98 (Feb 2022), Edge 98 (Feb 2022), Firefox 96 (Jan 2022), and Safari 13 (Sep 2019).++The `color-scheme` property is **progressive enhancement**.+Browsers that do not support it will ignore this property and use their default light-mode UI.++To adapt to the user's preferences in older browsers, use `prefers-color-scheme` media queries to provide different colors when dark mode is preferred.++- DO use the media query to switch custom properties on `:root` or `html`+- Avoid using the media query on individual components unless the component requires a very specific type of dark mode customization beyond colors.++```css+:root {+ /* Define brand colors for each mode */+ --color-brand-light: #0056b3;+ --color-brand-dark: #00e5ff;+ --color-brand: var(--color-brand-light);++ /* MANDATORY: Fallback for browsers without light-dark support */+ @media (prefers-color-scheme: dark) {+ --color-brand: var(--color-brand-dark);+ }++ /* Ignored in older browsers */+ color-scheme: light dark;+}++button.primary {+ background-color: var(--color-brand);+}+```++### Fallbacks & browser support for light-dark()++Baseline status for light-dark(): Newly available. It's been Baseline since 2024-05-13.+Supported by: Chrome 123 (Mar 2024), Edge 123 (Mar 2024), Firefox 120 (Nov 2023), and Safari 17.5 (May 2024).++For browsers that support `color-scheme` but not yet `light-dark()`, light and dark versions of colors should first be defined as custom properties, and the `prefers-color-scheme` media query should be used to set colors for the respective mode like in the example below:++```css+:root {+ /* Define browser UI accent color for each mode */+ --brand-accent-light: #0056b3;+ --brand-accent-dark: #00e5ff;+ --accent-color: var(--brand-accent-light);++ /* MANDATORY: Fallback for browsers without light-dark support */+ @media (prefers-color-scheme: dark) {+ --accent-color: var(--brand-accent-dark);+ }++ /* OPTIONAL: use light-dark() for more control of built-in UI colors */+ @supports (color: light-dark(white, black)) {+ --accent-color: light-dark(var(--brand-accent-light), var(--brand-accent-dark));+ }++ /* MANDATORY: Automatically adapt native UI to user system preferences */+ color-scheme: light dark;++ /* Example inherited color property */+ accent-color: var(--accent-color);+}++pre, code {+ color-scheme: dark;++ /* **Mandatory**: any inherited color properties must be set again, even if to the same design tokens */+ accent-color: var(--accent-color);+}+```++### Fallbacks & browser support for scrollbar-color++Baseline status for scrollbar-color: Newly available. It's been Baseline since 2025-12-12.+Supported by: Chrome 121 (Jan 2024), Edge 121 (Jan 2024), Firefox 64 (Dec 2018), and Safari 26.2 (Dec 2025).++This feature is progressive enhancement and does not always require fallbacks.++If the styling is important and the user's Baseline target is "Baseline Widely Available" or earlier, you SHOULD include the non-standard `::-webkit-scrollbar` pseudo-elements as fallbacks.++Wrap legacy fallbacks in an `@supports not (scrollbar-color: auto)` block to prevent conflicts between standard properties and legacy WebKit selectors in browsers that support both natively.++If you are using custom properties to define colors, these will cascade to the legacy WebKit selectors automatically. You do NOT need to duplicate them.++```css+/* Legacy fallback for WebKit/Blink browsers */+@supports not (scrollbar-color: auto) {+ .scroller::-webkit-scrollbar {+ /* Must define base size in WebKit for custom colors to be visual */+ width: 12px;+ height: 12px;+ }++ .scroller::-webkit-scrollbar-thumb {+ background: var(--scrollbar-thumb);+ }++ .scroller::-webkit-scrollbar-track {+ background: var(--scrollbar-track);+ }+}+```++### Fallbacks & browser support for accent-color++accent-color has limited availability.+Supported by: Chrome 93 (Aug 2021), Edge 93 (Sep 2021), and Firefox 92 (Sep 2021).+Unsupported in: Safari.++The `accent-color` property is progressive enhancement.+Browsers that do not support this property will ignore it and use their default UI colors.