--- url: "https://web.dev/articles/building/a-switch-component" titulek: "Building a switch component | Articles | web.dev" h1: "Building a switch component Stay organized with collections Save and categorize content based on your preferences." popis: "A foundational overview of how to build a responsive and accessible switch component." jazyk: "en" kanonicka: "https://web.dev/articles/building/a-switch-component" drobecky: ["Articles", "Building a switch component"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:35:54" znaku: 21874 slov: 3245 kodovani: "iso-8859-2" sha256: "22396e41bb46f266cf67bacc50f837dcb6c988000f9fb84e42cc1b562a37ee06" --- Building a switch component Stay organized with collections Save and categorize content based on your preferences. A foundational overview of how to build a responsive and accessible switch component. Adam Argyle In this post I want to share thinking on a way to build switch components. [Try the demo](https://gui-challenges.web.app/switch/dist/). [Demo](https://gui-challenges.web.app/switch/dist/) If you prefer video, here's a YouTube version of this post: Overview A [switch](https://w3c.github.io/aria/#switch) functions similar to a checkbox but explicitly represents boolean on and off states. This demo uses `` for the majority of its functionality, which has the advantage of not needing CSS or JavaScript to be fully functional and accessible. Loading CSS brings support for right-to-left languages, verticality, animation and more. Loading JavaScript makes the switch draggable and tangible. Custom properties The following variables represent the various parts of the switch and their options. As the top-level class, `.gui-switch` contains custom properties used throughout the component children, and entry points for centralized customization. Track The length (`--track-size`), padding, and two colors: `.gui-switch { --track-size: calc(var(--thumb-size) * 2); --track-padding: 2px; --track-inactive: hsl(80 0% 80%); --track-active: hsl(80 60% 45%); --track-color-inactive: var(--track-inactive); --track-color-active: var(--track-active); @media (prefers-color-scheme: dark) { --track-inactive: hsl(80 0% 35%); --track-active: hsl(80 60% 60%); } }` Thumb The size, background color, and interaction highlight colors: `.gui-switch { --thumb-size: 2rem; --thumb: hsl(0 0% 100%); --thumb-highlight: hsl(0 0% 0% / 25%); --thumb-color: var(--thumb); --thumb-color-highlight: var(--thumb-highlight); @media (prefers-color-scheme: dark) { --thumb: hsl(0 0% 5%); --thumb-highlight: hsl(0 0% 100% / 25%); } }` Reduced motion To add a clear alias and reduce repetition, a reduced motion preference user media query can be put into a custom property with the [PostCSS plugin](https://github.com/postcss/postcss-custom-media) based on this [draft spec in Media Queries 5](https://drafts.csswg.org/mediaqueries-5/#at-ruledef-custom-media): `@custom-media --motionOK (prefers-reduced-motion: no-preference);` Markup I chose to wrap my `` element with a `