Accordion·续
Always open
Omit the name attribute on each <details> element to allow multiple accordion items to be open simultaneously.
<details class="accordion-item" open>
<summary class="accordion-header">
Accordion Item #1
<svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg>
</summary>
<div class="accordion-body">
<strong>This is the first item’s accordion body.</strong> Without the <code>name</code> attribute, each accordion item operates independently. You can open multiple items at the same time.
</div>
</details>
<details class="accordion-item">
<summary class="accordion-header">
Accordion Item #2
<svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg>
</summary>
<div class="accordion-body">
<strong>This is the second item’s accordion body.</strong> Opening this won’t close the first item because there’s no <code>name</code> attribute linking them together.
</div>
</details>
<details class="accordion-item">
<summary class="accordion-header">
Accordion Item #3
<svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg>
</summary>
<div class="accordion-body">
<strong>This is the third item’s accordion body.</strong> Each item is completely independent—open or close them in any combination you like.
</div>
</details>
</div>} customMarkup={<div class="accordion">
<details class="accordion-item" open><!--[!code highlight]-->
<!-- No name attribute, so items open independently -->
</details>
<!-- More accordion items here -->
</div>`} />
Expand & Collapse all
To enable expanding and collapsing of all accordion items at once, remove the name attribute from each <details> element using JavaScript. This allows multiple accordion items to be open simultaneously. Restore the name attribute when you collapse the items again if you want exclusive behavior back.
<Example code={`<button type="button" class="btn-subtle btn-sm theme-secondary mb-3" id="btnAccordionToggleAll" aria-expanded="false">Expand all</button>
<div class="accordion accordion-sm" id="accordionExpandCollapse"> <details class="accordion-item" name="accordionExpandCollapse" open> <summary class="accordion-header"> Accordion Item #1 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> <strong>This is the first item’s accordion body.</strong> Use Expand all to open every item, even when they share a <code>name</code>. </div> </details> <details class="accordion-item" name="accordionExpandCollapse"> <summary class="accordion-header"> Accordion Item #2 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> <strong>This is the second item’s accordion body.</strong> Collapse all closes every item and restores exclusive <code>name</code> grouping. </div> </details> <details class="accordion-item" name="accordionExpandCollapse"> <summary class="accordion-header"> Accordion Item #3 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> <strong>This is the third item’s accordion body.</strong> If your accordion has no <code>name</code>, you only need to set <code>open</code> on each item. </div> </details> </div>`} />
We use the following JavaScript for this demo:
Nested
Accordions can be nested inside each other by placing a full .accordion within an .accordion-body. Use unique name attribute values for each level so the parent and child accordions operate independently.
<details class="accordion-item" name="accordionNested" open> <summary class="accordion-header"> Accordion Item #1 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> <strong>This is the first item’s accordion body.</strong> It contains a nested accordion below. <div class="accordion mt-3"> <details class="accordion-item" name="accordionNestedInner" open> <summary class="accordion-header"> Nested Item #1 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> This is the body of the first nested accordion item. </div> </details> <details class="accordion-item" name="accordionNestedInner"> <summary class="accordion-header"> Nested Item #2 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> This is the body of the second nested accordion item. </div> </details> <details class="accordion-item" name="accordionNestedInner"> <summary class="accordion-header"> Nested Item #3 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> This is the body of the third nested accordion item. </div> </details> </div> </div> </details> <details class="accordion-item" name="accordionNested"> <summary class="accordion-header"> Accordion Item #2 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> <strong>This is the second item’s accordion body.</strong> It does not contain a nested accordion. </div> </details> <details class="accordion-item" name="accordionNested"> <summary class="accordion-header"> Accordion Item #3 <svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg> </summary> <div class="accordion-body"> <strong>This is the third item’s accordion body.</strong> It does not contain a nested accordion. </div> </details> </div>`} />
Accessibility
The native <details> and <summary> elements provide built-in accessibility:
- Screen readers announce the expanded/collapsed state automatically
- Keyboard navigation works out of the box (Enter/Space to toggle)
- The
<summary>element is focusable by default - No ARIA attributes are required as the semantics are built into the elements
CSS
Variables
Customize
Custom icons
The accordion uses an inline SVG for the chevron icon. You can customize this by replacing the SVG within the .accordion-header:
<summary class="accordion-header">
Accordion Title
<svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round">
<path d="m2 5 6 6 6-6"/>
</svg>
</summary>
The .accordion-icon class handles positioning and the rotation animation when the accordion opens.
Events
You can listen for the native toggle event on <details> elements:
const accordionItem = document.querySelector('.accordion-item')
accordionItem.addEventListener('toggle', event => {
if (event.target.open) {
console.log('Accordion opened')
} else {
console.log('Accordion closed')
}
})
Programmatic control
Toggle accordion items programmatically using the open property:
const accordionItem = document.querySelector('.accordion-item')
// Open the accordion
accordionItem.open = true
// Close the accordion
accordionItem.open = false
// Toggle the accordion
accordionItem.open = !accordionItem.open
