Controls in this document share its theme and use the demo-only
storage key theme-switcher-demo. Framed examples
have their own page themes and storage settings.
Use Tab to reach a group, arrow keys to move between choices, and Space to select. Only System follows changes to your device’s color preference.
Start with an empty control
Drop in the theme picker
Use an empty <theme-switcher> to
generate a legend and System, Light, and Dark radios.
Before upgrade, or if JavaScript fails, this version has
no usable controls. You can
supply your own radio markup
instead when you need native controls without
JavaScript.
<theme-switcher
storage-key="theme-switcher-demo">
</theme-switcher>
/* Your page styles */
:root { color-scheme: light dark; }
:root[data-theme='light'] {
color-scheme: light;
}
:root[data-theme='dark'] {
color-scheme: dark;
}
body {
background: Canvas;
color: CanvasText;
}
Reload to restore the saved choice.
Supply the radio markup yourself
As an alternative to the empty control, wrap a labeled
radio group in
<theme-switcher>. The browser
provides selection and form submission; the component
adds theme changes, persistence, and segmented-control
styles. Without JavaScript, the radios still work, but
changing them doesn’t apply a theme. A server can use
the submitted value on the next page load.
<form>
<theme-switcher
storage-key="theme-switcher-demo">
<fieldset>
<legend>Color theme</legend>
<label><input type="radio" name="theme"
value="system" checked>System</label>
<label><input type="radio" name="theme"
value="light">Light</label>
<label><input type="radio" name="theme"
value="dark">Dark</label>
</fieldset>
</theme-switcher>
<button type="reset">Reset appearance</button>
</form>
Reset restores the authored System default, applies it to the page, and saves it.
Submit without the component
This frame deliberately loads no component module. Select a radio and submit to see its value in the frame’s URL. No loading-state CSS hides the inputs, and no script changes the theme or saves the choice. Disable JavaScript and reload to compare the authored and generated examples.
<form action="./fallback.html" method="get">
<theme-switcher>
<fieldset>
<legend>Color theme</legend>
<label><input type="radio" name="theme"
value="system" checked>System</label>
<label><input type="radio" name="theme"
value="light">Light</label>
<label><input type="radio" name="theme"
value="dark">Dark</label>
</fieldset>
</theme-switcher>
<p><button>Submit to the URL</button></p>
</form>
Choose themes and labels
Add a reading palette
Keep the exact values system,
light, and dark, and add your
own choices and CSS palettes. This separate page adds
Sepia and uses storage="none", so it
neither saves the choice nor changes the parent page.
<theme-switcher storage="none"
label="Reading palette"
choices='[
{"label":"System","value":"system"},
{"label":"Light","value":"light"},
{"label":"Dark","value":"dark"},
{"label":"Sepia","value":"sepia"}
]'>
</theme-switcher>
:root[data-theme='sepia'] {
color-scheme: light;
--page-background: #f6ecd6;
--page-color: #493921;
}
Use your application’s vocabulary
Change the generated legend and default choice labels without changing their values. For authored radios, edit the legend and label text instead. See the configuration reference for shared configuration and custom choices.
<theme-switcher
storage-key="theme-switcher-demo"
label="Reading appearance"
system-label="Use device setting"
light-label="Day"
dark-label="Night">
</theme-switcher>
Translate the labels into French
Translate the group name and each choice. Language inherits from surrounding markup, or you can set it on the component.
<theme-switcher lang="fr"
storage-key="theme-switcher-demo"
label="Thème"
system-label="Appareil"
light-label="Clair"
dark-label="Sombre">
</theme-switcher>
Use Arabic labels and right-to-left layout
Set lang="ar" dir="rtl" on a wrapper to
pass language and direction to the native controls.
These labels also work without the decorative icons
shown here; their CSS appears in the styling examples
below.
<div lang="ar" dir="rtl">
<theme-switcher class="icons"
storage-key="theme-switcher-demo"
label="مظهر الموقع"
system-label="النظام"
light-label="فاتح"
dark-label="داكن">
</theme-switcher>
</div>
Save preferences and restore them early
The component uses localStorage by default. A
valid saved choice wins over author defaults, including a
saved System choice. Additional controls join the current
page preference. See the
preference order and readiness rules.
Share the preference with your server
Add cookie to mirror the choice in a cookie
named after the storage key. A valid
localStorage value wins on startup; the
cookie is a fallback. Your server must validate the
value against your themes before rendering it.
storage="none" disables both stores.
<theme-switcher
storage-key="theme-switcher-cookie-demo"
cookie
label="Saved site appearance">
</theme-switcher>
Supply cookie="server-theme" to use a
different cookie name. See the
cookie settings and security constraints.
Storage errors don’t prevent theme changes. Listen for
theme-switcher:error and inspect
detail.phase and detail.error.
If one store fails, the other can still save the choice;
later changes retry storage. The
isolated storage-failure fixture
retains the diagnostic example without changing the
stores on this page.
Apply a saved choice before first paint
Read the preference before your stylesheets load. Match the key and allowed values to the control, and fall back to System when storage is unavailable or invalid. Choose a theme in this frame, then use its reload link. See the README for cookie restoration and Content Security Policy guidance.
<script>
let theme;
try {
theme = localStorage.getItem(
'theme-switcher-early-demo');
} catch (error) {
console.warn('Theme storage unavailable.',
error);
}
document.documentElement.setAttribute(
'data-theme',
['system', 'light', 'dark'].includes(theme)
? theme : 'system');
</script>
<link rel="stylesheet" href="./demo-shell.css">
<link rel="stylesheet" href="./styles.css">
<link rel="stylesheet" href="./embedded.css">
<!-- In the body: -->
<theme-switcher
storage-key="theme-switcher-early-demo"
label="Saved appearance">
</theme-switcher>
Style the controls
Restyle the segments
Use the inherited
--multi-switch-* properties
to change the shape and colors. Keep readable text, a
visible selection, and a clear focus outline.
<theme-switcher class="pill"
storage-key="theme-switcher-demo"
label="Site appearance">
</theme-switcher>
.pill {
--multi-switch-radius: 999px;
--multi-switch-border: #173f63;
--multi-switch-background: #eef4ff;
--multi-switch-color: #173f63;
--multi-switch-selected-background: #173f63;
--multi-switch-selected-color: #fff;
--multi-switch-focus-color: #173f63;
}
Add decorative icons beside text
CSS masks add icons without replacing the labels. Each mask uses the segment’s current text color. In forced colors, the mask retains its paint while the label keeps automatic color adjustment.
<theme-switcher class="icons"
storage-key="theme-switcher-demo"
label="Icons and text">
</theme-switcher>
.icons [data-multi-switch-choice]::before {
content: '';
inline-size: 1.25em;
block-size: 1.25em;
flex-shrink: 0;
background: currentColor;
mask: var(--theme-icon) center / contain
no-repeat;
}
.icons [data-value='system'] {
--theme-icon: url('./system.svg');
}
.icons [data-value='light'] {
--theme-icon: url('./light.svg');
}
.icons [data-value='dark'] {
--theme-icon: url('./dark.svg');
}
@media (forced-colors: active) {
.icons [data-multi-switch-choice]::before {
background: currentColor;
forced-color-adjust: none;
}
}
Hide choice text and the legend visually
icon-only visually hides choice text, but
doesn’t add icons or hide the legend. This example
reuses the masks above and separately hides the legend
with your own CSS. The label text and group name remain
available to assistive technology. Keep visible context
for the control’s purpose.
<theme-switcher
class="icons hide-legend" icon-only
storage-key="theme-switcher-demo"
label="Icon-only appearance">
</theme-switcher>
/* Reuse the .icons rules above. */
theme-switcher.hide-legend legend {
position: absolute;
inline-size: 1px;
block-size: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}