Accordions contain of a number of content panels, each of wich can be expanded or collapsed vertically by the user.
Accordions help to save vertical space and prevent from visual noise. Some accordions allow only a single panel to be expanded at a time, others allow multiple.
Before you continue, please read Tablist widgets (or: tab panels, tabs) to understand why accordions simply are extended variants of tablists, providing a slightly different layout and (sometimes) expandability of multiple panels.
<p><button>Focusable element before</button></p><divclass="accordion"data-adg-accordion="my-accordion"><h2class="header"><buttonaria-controls="first_item"><spanclass="visually-hidden">Show panel </span>Rose</button></h2><sectionclass="panel"id="first_item"><p>
A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over three hundred species and tens of thousands of cultivars.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Rosa_rubiginosa_1.jpg/259px-Rosa_rubiginosa_1.jpg"alt="Rosa rubiginosa by Stan Shebs (wikimedia.org)"width="200"><h3>
Botany
</h3><p>
The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four).
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Rose"target="_blank">Rose (wikipedia.org)</a></p><p><labelfor="first_input"><strong>Name your favorite rose:</strong></label><inputid="first_input"type="text" /></p></section><h2class="header"><buttonaria-controls="second_item"><spanclass="visually-hidden">Show panel </span>Tulip</button></h2><sectionclass="panel"id="second_item"><p>
Tulips are a genus of spring-blooming perennial herbaceous bulbiferous geophytes. The flowers are usually large, showy and brightly colored, generally red, pink, yellow, or white.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/The_Dallas_Arboretum_and_Botanical_Garden.jpg/320px-The_Dallas_Arboretum_and_Botanical_Garden.jpg"alt="Red and pink tulips by Inuyas (wikimedia.org)"width="200"><h3>
Description
</h3><p>
The tulip’s flowers are usually large and are actinomorphic (radially symmetric) and hermaphrodite (contain both male and female characteristics), generally erect, or more rarely pendulous, and are arranged more usually as a single terminal flower. In structure, the flower is generally cup or star shaped.
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Tulip"target="_blank">Tulip (wikipedia.org)</a></p><p><labelfor="second_input"><strong>Name your favorite tulip:</strong></label><inputid="second_input"type="text" /></p></section><h2class="header"><buttonaria-controls="third_item"><spanclass="visually-hidden">Show panel </span>Sunflower</button></h2><sectionclass="panel"id="third_item"><p>
Helianthus annuus, the common sunflower, is a large annual forb of the genus Helianthus grown as a crop for its edible oil and edible fruits. The name sunflower may derive from the flower’s head’s shape, which resembles the sun.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Sunflower_sky_backdrop.jpg/217px-Sunflower_sky_backdrop.jpg"alt="Sunflower against a blue sky by Fir0002 (wikimedia.org)"width="200"><h3>
Description
</h3><p>
The plant has an erect rough-hairy stem, reaching typical heights of 3 metres. The tallest sunflower on record achieved 9.17 metres. Sunflower leaves are broad, coarsely toothed, rough and mostly alternate. What is often called the "flower" of the sunflower is actually a "flower head" or pseudanthium of numerous small individual five-petaled flowers. The outer flowers, which resemble petals, are called ray flowers.
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Helianthus_annuus"target="_blank">Sunflower (wikipedia.org)</a></p><p><labelfor="third_input"><strong>Name your favorite sunflower:</strong></label><inputid="third_input"type="text" /></p></section></div><p><button>Focusable element after</button></p>
A link with an aria-expanded="true" attribute is placed around each panel’s header; its value (true/false) and the visibility of the corresponding panel is toggled using JavaScript. See Marking elements expandable using aria-expanded.
While this may feel tempting in some circumstances, there are several drawbacks:
It needs more JavaScript (instead of relying on browser standard behaviour).
The current implementation allows multiple elements to be open. If you wanted to restrict it to one element though, a lot of additional JavaScript would be needed to manage states - something that radio buttons would offer "for free".
This solution is less intuitive: a screen reader announcement "link X collapsed" is less expressive than "show panel X checkbox not checked" or "show panel X radio button not checked 2 of 3".
Missing backwards compatibility for older clients with incomplete/missing ARIA support.
<p><button>Focusable element before</button></p><divclass="accordion"data-adg-radio-accordion="my-accordion"><h1class="visually-hidden">
flowers (accordion)
</h1><pclass="visually-hidden">
Tablist help: use the accordion controls to toggle the visibility of their respective panels (below the controls).
</p><divclass="header"><inputchecked="checked"class="trigger visually-hidden"id="accordion_flowers_panel_rose"name="accordion_flowers"type="radio"value="accordion_flowers_panel_rose" /><h2><labelfor="accordion_flowers_panel_rose"><spanclass="visually-hidden">Show panel </span>Rose</label></h2></div><divclass="panel"id="accordion_flowers_panel_rose_panel"><p>
A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over three hundred species and tens of thousands of cultivars.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Rosa_rubiginosa_1.jpg/259px-Rosa_rubiginosa_1.jpg"alt="Rosa rubiginosa by Stan Shebs (wikimedia.org)"width="200"><h3>
Botany
</h3><p>
The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four).
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Rose"target="_blank">Rose (wikipedia.org)</a></p><p><labelfor="rose_input"><strong>Name your favorite rose:</strong></label><inputid="rose_input"type="text" /></p></div><divclass="header"><inputclass="trigger visually-hidden"id="accordion_flowers_panel_tulip"name="accordion_flowers"type="radio"value="accordion_flowers_panel_tulip" /><h2><labelfor="accordion_flowers_panel_tulip"><spanclass="visually-hidden">Show panel </span>Tulip</label></h2></div><divclass="panel"id="accordion_flowers_panel_tulip_panel"><p>
Tulips are a genus of spring-blooming perennial herbaceous bulbiferous geophytes. The flowers are usually large, showy and brightly colored, generally red, pink, yellow, or white.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/The_Dallas_Arboretum_and_Botanical_Garden.jpg/320px-The_Dallas_Arboretum_and_Botanical_Garden.jpg"alt="Red and pink tulips by Inuyas (wikimedia.org)"width="200"><h3>
Description
</h3><p>
The tulip’s flowers are usually large and are actinomorphic (radially symmetric) and hermaphrodite (contain both male and female characteristics), generally erect, or more rarely pendulous, and are arranged more usually as a single terminal flower. In structure, the flower is generally cup or star shaped.
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Tulip"target="_blank">Tulip (wikipedia.org)</a></p><p><labelfor="tulip_input"><strong>Name your favorite tulip:</strong></label><inputid="tulip_input"type="text" /></p></div><divclass="header"><inputclass="trigger visually-hidden"id="accordion_flowers_panel_sunflower"name="accordion_flowers"type="radio"value="accordion_flowers_panel_sunflower" /><h2><labelfor="accordion_flowers_panel_sunflower"><spanclass="visually-hidden">Show panel </span>Sunflower</label></h2></div><divclass="panel"id="accordion_flowers_panel_sunflower_panel"><p>
Helianthus annuus, the common sunflower, is a large annual forb of the genus Helianthus grown as a crop for its edible oil and edible fruits. The name sunflower may derive from the flower’s head’s shape, which resembles the sun.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Sunflower_sky_backdrop.jpg/217px-Sunflower_sky_backdrop.jpg"alt="Sunflower against a blue sky by Fir0002 (wikimedia.org)"width="200"><h3>
Description
</h3><p>
The plant has an erect rough-hairy stem, reaching typical heights of 3 metres. The tallest sunflower on record achieved 9.17 metres. Sunflower leaves are broad, coarsely toothed, rough and mostly alternate. What is often called the "flower" of the sunflower is actually a "flower head" or pseudanthium of numerous small individual five-petaled flowers. The outer flowers, which resemble petals, are called ray flowers.
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Helianthus_annuus"target="_blank">Sunflower (wikipedia.org)</a></p><p><labelfor="sunflower_input"><strong>Name your favorite sunflower:</strong></label><inputid="sunflower_input"type="text" /></p></div></div><p><button>Focusable element after</button></p>
Using .accordion:focus-within .control label, a style can be applied to all radio button labels upon interacting with the accordion.
This gives users a clue that they are interacting with a single control now (indicating to use the Arrow keys instead of Tab to navigate through accordion items).
If you would rather like to make each control focusable on its own, you could use a group of checkboxes instead of radio buttons.
- Do not forget to make sure only one of them is checked at a time though (using some JavaScript).
<p><button>Focusable element before</button></p><divclass="accordion"data-adg-checkbox-accordion="my-accordion"><h1class="visually-hidden">
flowers (accordion)
</h1><pclass="visually-hidden">
Tablist help: use the accordion controls to toggle the visibility of their respective panels (below the controls).
</p><divclass="header"><inputclass="trigger visually-hidden"id="accordion_flowers_panel_rose"type="checkbox" /><h2><labelfor="accordion_flowers_panel_rose"><spanclass="visually-hidden">Show panel </span>Rose</label></h2></div><divclass="panel"id="accordion_flowers_panel_rose_panel"><p>
A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over three hundred species and tens of thousands of cultivars.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Rosa_rubiginosa_1.jpg/259px-Rosa_rubiginosa_1.jpg"alt="Rosa rubiginosa by Stan Shebs (wikimedia.org)"width="200"><h3>
Botany
</h3><p>
The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four).
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Rose"target="_blank">Rose (wikipedia.org)</a></p><p><labelfor="rose_input"><strong>Name your favorite rose:</strong></label><inputid="rose_input"type="text" /></p></div><divclass="header"><inputclass="trigger visually-hidden"id="accordion_flowers_panel_tulip"type="checkbox" /><h2><labelfor="accordion_flowers_panel_tulip"><spanclass="visually-hidden">Show panel </span>Tulip</label></h2></div><divclass="panel"id="accordion_flowers_panel_tulip_panel"><p>
Tulips are a genus of spring-blooming perennial herbaceous bulbiferous geophytes. The flowers are usually large, showy and brightly colored, generally red, pink, yellow, or white.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/The_Dallas_Arboretum_and_Botanical_Garden.jpg/320px-The_Dallas_Arboretum_and_Botanical_Garden.jpg"alt="Red and pink tulips by Inuyas (wikimedia.org)"width="200"><h3>
Description
</h3><p>
The tulip’s flowers are usually large and are actinomorphic (radially symmetric) and hermaphrodite (contain both male and female characteristics), generally erect, or more rarely pendulous, and are arranged more usually as a single terminal flower. In structure, the flower is generally cup or star shaped.
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Tulip"target="_blank">Tulip (wikipedia.org)</a></p><p><labelfor="tulip_input"><strong>Name your favorite tulip:</strong></label><inputid="tulip_input"type="text" /></p></div><divclass="header"><inputclass="trigger visually-hidden"id="accordion_flowers_panel_sunflower"type="checkbox" /><h2><labelfor="accordion_flowers_panel_sunflower"><spanclass="visually-hidden">Show panel </span>Sunflower</label></h2></div><divclass="panel"id="accordion_flowers_panel_sunflower_panel"><p>
Helianthus annuus, the common sunflower, is a large annual forb of the genus Helianthus grown as a crop for its edible oil and edible fruits. The name sunflower may derive from the flower’s head’s shape, which resembles the sun.
</p><imgsrc="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Sunflower_sky_backdrop.jpg/217px-Sunflower_sky_backdrop.jpg"alt="Sunflower against a blue sky by Fir0002 (wikimedia.org)"width="200"><h3>
Description
</h3><p>
The plant has an erect rough-hairy stem, reaching typical heights of 3 metres. The tallest sunflower on record achieved 9.17 metres. Sunflower leaves are broad, coarsely toothed, rough and mostly alternate. What is often called the "flower" of the sunflower is actually a "flower head" or pseudanthium of numerous small individual five-petaled flowers. The outer flowers, which resemble petals, are called ray flowers.
</p><p>
Learn more: <ahref="https://en.wikipedia.org/wiki/Helianthus_annuus"target="_blank">Sunflower (wikipedia.org)</a></p><p><labelfor="sunflower_input"><strong>Name your favorite sunflower:</strong></label><inputid="sunflower_input"type="text" /></p></div></div><p><button>Focusable element after</button></p>
Checkboxes replace the radio buttons to offer multiple selection.
We waived using a <fieldset>/<legend> structure, as this is no traditional group of checkboxes, and JAWS tends to be very wordy with focusable items nested within those, see Grouping form controls with fieldset and legend.
By default, only the Space key is used to toggle a checkbox (while pressing Enter submits a form).
To make it more intuitive for visual users (who do not know about any checkbox behind the scenes, and thinking they are interacting with a link or button), the Enter key was re-wired to also toggle the checkboxes.
In contrast to the radio button solution above, we omitted a visual .accordion:focus-within .control label state for the accordion items, as checkboxes are individual controls and (thereby accessed by the Tab key, as most users would expect).