Web Front-end
H5 Tutorial
How to use the details and summary elements for an accordion in HTML5?
How to use the details and summary elements for an accordion in HTML5?
Yes, HTML5's <details> and <summary> elements can create accessible folded panels without JavaScript; 1. <details> contains expandable/collapsed content, <summary> as the title; 2. The default content is hidden, click <summary> to switch display; 3. Use the open attribute to expand by default; 4. Multiple <details> can form an accordion menu, supporting multiple openings; 5. You can customize styles through CSS, such as hiding the default arrows, adding icons and borders; 6. Native semantics and supports keyboard navigation, without additional ARIA attributes; 7. If you need a single opening effect, you need to add JavaScript to listen to toggle events and close other panels, thereby realizing the function of expanding only one panel.

The <details></details> and <summary></summary> elements in HTML5 provide a native way to create an according-style widget without JavaScript. They're semantic, accessible, and easy to use.

How <details></details> and <summary></summary> Work
The <details></details> element acts as a container for content that can be shown or hidden. The <summary></summary> element is used inside <details></details> and serves as the clickable label or header that toggles the visibility of the content.
By default, the content inside <details></details> is hidden. When a user clicks the <summary></summary> , the content expands or collapses.

Basic Syntax
<details> <summary>Click to expand</summary> <p>This is the hidden content that appears when expanded.</p> </details>
- The
<summary>is required for<details>to function properly. - Any content can go inside
<details>after the<summary>. - Only one
<summary>is allowed per<details>.
Creating a Multi-Item Accordion
You can stack multiple <details> elements to create a full according. Each item behaves independently.
<details> <summary>Section 1</summary> <p>Content for section 1.</p> </details> <details> <summary>Section 2</summary> <p>Content for section 2.</p> </details> <details> <summary>Section 3</summary> <p>Content for section 3.</p> </details>
This creates an according where each section can be opened or closed individually. Unlike some JavaScript-based according toons, multiple items can be open at once — which is often desirable.

Open by Default
To make a section expanded when the page loads, use the open attribute:
<details open> <summary>Section 1 (Open by default)</summary> <p>This content is visible right away.</p> </details>
Styling the Accordion with CSS
You can style <details> and <summary> to match your design.
Remove Default Arrow (Triangle)
The <summary> usually shows a default triangle (disclosure widget). You can hide it and add a custom one:
summary {
list-style: none; /* Removes default arrow */
cursor: pointer;
}
summary::-webkit-details-marker {
display: none; /* For Chrome/Safari */
}
summary::marker {
content: ''; /* For Firefox */
}Add Custom Icon
Use CSS to add a plus/minus or chevron:
summary::after {
content: " ";
float: right;
}
details[open] summary::after {
content: "−";
}Add Spacing and Borders
Make it looks like a proper according:
details {
border: 1px solid #ddd;
margin-bottom: -1px;
border-radius: 4px;
}
summary {
padding: 10px;
background-color: #f7f7f7;
font-weight: bold;
list-style: none;
}
details > *:not(summary) {
padding: 10px;
margin: 0;
}Accessibility and Semantics
-
<details>and<summary>are natively accessible . Screen readers understand the toggle behavior. - No need for
role,aria-expanded, ortabindex— the browser handles it. - Keyboard navigation works: users can press Enter or Space to toggle.
Limitations
- No built-in single-open-panel behavior : All panels can be open at once. If you need “only one open at a time,” you'll need a small amount of JavaScript.
- Stylening limitations : The disclosure triangle can be tricky to style consistently across browsers.
Optional: JavaScript for Single Open Panel
If you want only one section open at a time (like a traditional according), use a little JavaScript:
<details open class="accordion-item"> <summary>Section 1</summary> <p>Content 1</p> </details> <details class="accordion-item"> <summary>Section 2</summary> <p>Content 2</p> </details>
document.querySelectorAll('details.accordion-item').forEach(item => {
item.addEventListener('toggle', () => {
if (item.open) {
document.querySelectorAll('details.accordion-item').forEach(other => {
if (other !== item) other.open = false;
});
}
});
}); This listens for the toggle event and closes other items when one opens.
Basically, <details></details> and <summary></summary> give you a solid, accessible according to zero JavaScript needed — and a small enhancement can make it behave like a single-open according to if required.
The above is the detailed content of How to use the details and summary elements for an accordion in HTML5?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20516
7
13630
4
How to detect if a browser supports HTML5 features? (Modernizr)
Mar 04, 2026 am 03:11 AM
The main reason for the failure of Modernizr detection is that the script is not successfully loaded or executed at an improper time. It is necessary to ensure that it is loaded synchronously, avoids CSP interception, and is executed before DOM construction. As an alternative, it is preferable to use CSS@supports and native API to detect empty scripts.
How to use the template tag for dynamic content in HTML5? (Cloning nodes)
Mar 05, 2026 am 02:15 AM
The template tag itself does not render and must be manually cloned and inserted. Template is a lazy container of HTML5. The browser will parse it but completely skip rendering and script execution. If you write Hello directly, nothing will appear on the page - this is not a bug, it is the design. To make it "alive", you must use JavaScript to extract the content, clone it, and then hang it on the DOM. A common mistake is to directly obtain document.querySelector('template').content and then try to appendChild. The result is an error or no response: because the content is a Docu
How to make a phone number clickable in HTML5? (Tel link)
Mar 05, 2026 am 02:29 AM
The correct way to write it is href="tel: 8613812345678". All non-numeric characters need to be cleared (only and numbers are retained). Mainland China numbers must be prefixed with 86. Extension numbers use;ext= format, and target="_blank" is disabled.
How to disable autocomplete on input fields in HTML5? (Form attributes)
Mar 05, 2026 am 02:31 AM
Autocomplete="off" sometimes does not take effect because modern browsers (such as Chrome ≥ 80) actively ignore it to ensure the password manager experience; to be truly effective, it needs to be combined with strategies such as semantic values (such as new-password), avoiding sensitive names, and dynamically generated attributes.
How to create a progress bar for file uploads in HTML5? (Progress tag)
Mar 06, 2026 am 02:22 AM
Why can't the tag directly display the upload progress? It is a read-only visual component. It does not listen to network requests and is not automatically bound to the upload process of XMLHttpRequest or fetch. If you put it in and don't update the value manually, it will always stop at 0%. What really drives it is the event monitoring in the upload logic you write yourself. A common mistake is to only monitor onload (upload completed) but miss upload.onprogress. XMLHttpRequest (not fetch) must be used to obtain real-time upload progress, because fetch does not expose the max attribute of the event in the upload phase and must be set to the file size (file.size
How to create a tooltip using only HTML5? (Title attribute)
Mar 06, 2026 am 12:23 AM
The title attribute is not a tooltip component, but an accessibility prompt mechanism implemented by the browser. The behavior, style, and interaction are uncontrollable and are only suitable for simple scenarios such as pure information supplement.
How to center an image vertically in HTML5? (Layout techniques)
Mar 07, 2026 am 02:05 AM
Flexbox is the most stable for centered images. The key is to set display:flex and align-items:center in the parent container and specify the height; using place-items:center for Grid is more concise; absolute positioning requires top:50% with transform:translateY(-50%); vertical-align is invalid for block-level centering.
How to insert a copyright symbol in HTML5? (Character entities)
Mar 05, 2026 am 02:57 AM
© and © have the same effect. The former is a named entity and is easy to read, while the latter is a decimal digital entity and has more stable compatibility. It is necessary to avoid the problems of CSS hiding, JS unescapement and missing semantics.





