Switch

ui.css · Cross-system: Cross-system reference § Forms

Overview

A toggle for a single on/off setting that takes effect immediately (e.g. enabling a processing step). It is a plain <input type="checkbox" role="switch">: the native checkbox already provides keyboard toggling (Space), focus, the change event, <label> association, and the checked → on/off mapping for assistive tech. CSS hides the default checkbox and draws a track with a sliding thumb — no JavaScript and no custom element.

A switch controls one on/off value. For a one-of-many choice use radio buttons; for a value you submit with a form (rather than apply instantly), a plain checkbox is usually clearer.

Anatomy

An <input type="checkbox"> with role="switch", paired with a <label>. role="switch" both selects the styles and makes screen readers announce it as a switch — there is no separate class, so the visual and announced control types stay consistent. Wrapping the input in the label (below) makes the label text toggle the switch with no for/id wiring.

Usage

Show code

States

Show code

In context

The common layout is a settings row: a <label> on the left and the switch on the right, separated with justify-content: space-between. Put the label text in its own element so the whole row reads as one labelled control.

Show code

Theming

The switch is driven entirely by the app's design tokens, so its theme updates automatically — including dark mode, which is handled centrally in index.css by re-defining the semantic tokens. There is no switch-specific dark-mode CSS.

To restyle only the switch in a given scope — without side effects — override these custom properties:

Set them on the switch or on any ancestor scope — an inline style on the wrapping <label> (as the demo below does), a section, :root — and the value inherits down. That works because the rule resolves each hook through a var() fallback (--_track-on: var(--switch-track-on, var(--brand))) instead of declaring --switch-* on the element itself; declaring them on the element would shadow an inherited value and prevent the override from taking effect.

Show code

Accessibility

Cross-system

See Cross-system reference § Forms for mappings to the Open UI "Active" explainer, Material md-switch, Radix Switch, Bootstrap "Switches", and the WAI-ARIA APG Switch pattern.

See also