手风琴使用原生 HTML <details> 和 <summary> 元素,利用 name 属性创建互斥手风琴,一次只能打开一个项目。这种方法的核心功能不需要 JavaScript,且默认完全无障碍。
示例
点击下面的手风琴以展开/折叠手风琴内容。每个 <details> 元素共享相同的 name 属性值,创建互斥手风琴,打开一个项目会自动关闭其他项目。
要渲染默认展开的手风琴,请为 <details> 元素添加 open 属性。
Accordion Item #1
open attribute is present. The native <details> element handles all the show/hide logic without any JavaScript. You can put any HTML content within the .accordion-body.
Accordion Item #2
name attribute groups this with other accordion items. When you open this item, any other open item in the same group will close automatically.
Accordion Item #3
name attributes on your <details> elements.
<div class="accordion">
<details class="accordion-item" name="accordionExample" 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 is shown by default because the <code>open</code> attribute is present. The native <code><details></code> element handles all the show/hide logic without any JavaScript. You can put any HTML content within the <code>.accordion-body</code>.
</div>
</details>
<details class="accordion-item" name="accordionExample">
<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> The <code>name</code> attribute groups this with other accordion items. When you open this item, any other open item in the same group will close automatically.
</div>
</details>
<details class="accordion-item" name="accordionExample">
<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> This exclusive accordion behavior is built into the browser—no Bootstrap JavaScript required. Just use matching <code>name</code> attributes on your <code><details></code> elements.
</div>
</details>
</div>面板高度动画需要 interpolate-size。不支持它的浏览器会立即打开和关闭面板。请参阅过渡以更改时序或移除它。
此组件的动画效果取决于 prefers-reduced-motion 媒体查询。请参阅无障碍文档中的减少动态效果章节。
变体
使用上下文 .theme-{color} 类为打开的手风琴项应用语义主题颜色。
Accordion Item #1
open attribute is present. The native <details> element handles all the show/hide logic without any JavaScript. You can put any HTML content within the .accordion-body.
Accordion Item #2
name attribute groups this with other accordion items. When you open this item, any other open item in the same group will close automatically.
Accordion Item #3
name attributes on your <details> elements.
<div class="accordion theme-primary">
<!-- Accordion items here -->
</div>小号
为 .accordion 添加 .accordion-sm,以获得更紧凑的手风琴,具有更紧凑的内边距、更小的边框半径和更小的字体大小。
Accordion Item #1
.accordion-sm class tightens up the padding, border radius, and font size for a more compact presentation.
Accordion Item #2
Accordion Item #3
.accordion-sm with other variants like .accordion-flush as needed.
<div class="accordion accordion-sm">
<!-- Accordion items here -->
</div>无边框
添加 .accordion-flush 以移除一些边框和圆角,使手风琴与其父容器边缘对齐。
Accordion Item #1
.accordion-flush class. This is the first item’s accordion body.Accordion Item #2
.accordion-flush class. This is the second item’s accordion body. Let’s imagine this being filled with some actual content.Accordion Item #3
.accordion-flush class. This is the third item’s accordion body. Nothing more exciting happening here in terms of content, but just filling up the space to make it look, at least at first glance, a bit more representative of how this would look in a real-world application.<div class="accordion accordion-flush">
<!-- Accordion items here -->
</div>间距
添加 .accordion-gap 在手风琴项之间添加间距。每个项目都是一个独立的圆角卡片。
间距存在于手风琴上,而不是面板上。面板仍然只对其 block-size 进行动画,因此打开和关闭过渡保持平滑。
Accordion Item #1
.accordion-gap class spaces items apart. Each item keeps a full border radius while the panel opens and closes.
Accordion Item #2
Accordion Item #3
--accordion-gap to change the space between items.
<div class="accordion accordion-gap">
<!-- Accordion items here -->
</div>在 $accordion-tokens 中设置 --accordion-gap 以更改默认间距。你也可以在手风琴上覆盖 CSS 自定义属性。
始终打开
省略每个 <details> 元素上的 name 属性,以允许多个手风琴项同时打开。
Accordion Item #1
name attribute, each accordion item operates independently. You can open multiple items at the same time.
Accordion Item #2
name attribute linking them together.
Accordion Item #3
<div class="accordion">
<details class="accordion-item" open>
<!-- No name attribute, so items open independently -->
</details>
<!-- More accordion items here -->
</div>展开并折叠全部
要启用一次性展开和折叠所有手风琴项,请使用 JavaScript 移除每个 <details> 元素上的 name 属性。这允许多个手风琴项同时打开。当你再次折叠项目时,如果你希望恢复互斥行为,请恢复 name 属性。
Accordion Item #1
name.
Accordion Item #2
name grouping.
Accordion Item #3
name, you only need to set open on each item.
<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>我们为此演示使用以下 JavaScript:
const accordion = document.getElementById('accordionExpandCollapse')
const toggleBtn = document.getElementById('btnAccordionToggleAll')
if (accordion && toggleBtn) {
const items = accordion.querySelectorAll('.accordion-item')
const groupName = 'accordionExpandCollapse'
toggleBtn.addEventListener('click', () => {
const expand = toggleBtn.getAttribute('aria-expanded') !== 'true'
for (const item of items) {
if (expand) {
item.removeAttribute('name')
item.open = true
} else {
item.open = false
item.setAttribute('name', groupName)
}
}
toggleBtn.setAttribute('aria-expanded', String(expand))
toggleBtn.textContent = expand ? 'Collapse all' : 'Expand all'
})
}
嵌套
手风琴可以通过在 .accordion-body 内放置完整的 .accordion 来相互嵌套。为每个层级使用唯一的 name 属性值,以便父级和子级手风琴独立操作。
Accordion Item #1
Nested Item #1
Nested Item #2
Nested Item #3
Accordion Item #2
Accordion Item #3
<div class="accordion">
<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">
这是第一个嵌套手风琴项的内容。
</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">
这是第二个嵌套手风琴项的内容。
</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">
这是第三个嵌套手风琴项的内容。
</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>无障碍
原生 <details> 和 <summary> 元素提供内置无障碍功能:
- 屏幕阅读器会自动朗读展开/折叠状态
- 键盘导航开箱即用(Enter/空格键切换)
<summary>元素默认可聚焦- 无需 ARIA 属性,因为语义已内置于元素中
CSS
变量
Accordions use local CSS variables on .accordion for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.
// stylelint-disable-next-line scss/dollar-variable-default
$accordion-tokens: defaults(
(
--accordion-padding-x: 1.25rem,
--accordion-padding-y: 1rem,
--accordion-color: var(--fg-body),
--accordion-bg: var(--bg-body),
--accordion-transition-property: "color, background-color, border-radius",
--accordion-transition-duration: .15s,
--accordion-transition-timing: ease-in-out,
--accordion-panel-transition-property: "content-visibility, block-size",
--accordion-panel-transition-duration: .2s,
--accordion-panel-transition-timing: ease,
--accordion-border-color: var(--border-color),
--accordion-border-width: var(--border-width),
--accordion-border-radius: var(--accordion-radius, var(--radius-7)),
--accordion-gap: var(--spacer-2),
--accordion-btn-color: var(--fg-2),
--accordion-btn-bg: var(--bg-body),
--accordion-btn-icon-width: 1rem,
--accordion-btn-icon-transform: rotate(-180deg),
--accordion-btn-icon-transition: transform .2s ease-in-out,
--accordion-active-color: var(--fg-body),
--accordion-active-bg: var(--bg-2),
),
$accordion-tokens
);
自定义
自定义图标
手风琴使用内联 SVG 作为尖括号图标。你可以通过替换 .accordion-header 内的 SVG 来自定义它:
<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>
.accordion-icon 类负责定位以及手风琴打开时的旋转动画。
事件
你可以监听 <details> 元素上的原生 toggle 事件:
const accordionItem = document.querySelector('.accordion-item')
accordionItem.addEventListener('toggle', event => {
if (event.target.open) {
console.log('Accordion opened')
} else {
console.log('Accordion closed')
}
})
编程方式控制
使用 open 属性以编程方式切换手风琴项:
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