Toggle Group
<div class="toggle-group" data-spacing="0" role="group" aria-label="Text alignment"> <button class="toggle toggle-outline" type="button" aria-pressed="true" aria-label="Align left"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> <path d="M3 6h18M3 12h10M3 18h14"></path> </svg> </button> <button class="toggle toggle-outline" type="button" aria-pressed="false" aria-label="Align center"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> <path d="M3 6h18M7 12h10M5 18h14"></path> </svg> </button> <button class="toggle toggle-outline" type="button" aria-pressed="false" aria-label="Align right"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> <path d="M3 6h18M11 12h10M7 18h14"></path> </svg> </button></div><div class="toggle-group" data-spacing="0" role="group" aria-label="Text alignment"> <button class="toggle toggle-outline" type="button" aria-pressed="true">…</button></div>| Attribute | Meaning |
|---|---|
data-spacing="0" | Joined: flat inner corners and shared edges |
data-orientation="vertical" | Stacks the segments |
Spaced
Section titled “Spaced”Without data-spacing="0" the segments keep their own shape and a gap.
<div class="toggle-group" role="group" aria-label="Formatting"> <button class="toggle toggle-outline" type="button" aria-pressed="true">Bold</button> <button class="toggle toggle-outline" type="button" aria-pressed="false">Italic</button> <button class="toggle toggle-outline" type="button" aria-pressed="false">Underline</button></div>Single selection
Section titled “Single selection”When exactly one may be on, it is a radio group — not several toggles. Radios give the arrow keys and the announced state for free.
<div class="toggle-group" data-spacing="0" role="radiogroup" aria-label="View"> <label class="toggle toggle-outline"> <input class="sr-only" type="radio" name="view" value="list" checked /> List </label> <label class="toggle toggle-outline"> <input class="sr-only" type="radio" name="view" value="grid" /> Grid </label></div>Set dir="rtl" on the page or on any subtree — the outer corners round on the correct sides. See
the button page for a live example.
Accessibility
Section titled “Accessibility”- Name the group.
role="group"witharia-label, orrole="radiogroup"for a single choice. - A focused segment lifts above its neighbours, so a joined group never clips the focus ring.
- Icon-only segments each need a name. The group name says what the set is for; each button still says what it does.