Markup and the basics
The <details> element needs no companion markup to work as an accordion, and any HTML you like can live inside it. Adding a <summary> element replaces the default "details" label with custom text.
<details>
Open and close the element to toggle this content.
</details>
<details>
<summary>Toggle content</summary>
Open and close the element to toggle this content.
</details>
<details>
<summary>Toggle content</summary>
<p>Open and close the element to toggle this content.</p>
<img src="path/to/image.svg" alt="">
</details>
In-page search and hidden content
Because collapsed content is hidden by default, there were early accessibility concerns about its invisibility to in-page search (CMD+F). Chrome now opens the <details> element when it finds a matching term inside it.

Firefox (since 139) and Safari skip closed <details> content during in-page searches as of writing. Firefox 134.0.1 matched searches when the element was open; Safari 18.1 skipped it entirely. Searchability is one of the items covered by Interop 2025, so the behavior may change. For now, keep important content out of <details> when it must be findable — an FAQ pattern built on the element may not be the best home for answers that need to be searchable.
Exclusive accordions via the name attribute
Giving each <details> the same name attribute makes them behave like a true accordion, where opening one panel collapses another.
<details name="notes">
<summary>Open Note</summary>
<p> ... </p>
</details>
<details name="notes"> <!-- etc. --> </details>
<details name="notes"> <!-- etc. --> </details>
<details name="notes"> <!-- etc. --> </details>
Styling the marker
The triangle indicating open and closed state can be targeted with ::marker, but only color and font size can be changed — that's the extent of it in fully supporting Chrome and Firefox. Safari supports the pseudo-element for list items (li::marker) but not for summary::marker. A generic ::marker rule hits both the <details> and an unordered list in the supporting browsers; in Safari it applies only to the list.
Scoping that generic selector to the <details> element does not isolate the marker:
/* This doesn't work! */
details::marker {
/* styles */
}
The scope belongs on <summary>, because that's what the marker is attached to.
/* This does work */
summary::marker {
/* styles */
}
The <summary> must be present in the HTML for marker styles to match — the element HTML inserts by default isn't enough. Given a generic ::marker selector, only a <details> with an explicit summary receives the styles.
Swapping the triangle through list-style-type — which works for list items — does not work:
/* Does not work! */
summary::marker {
list-style-type: square;
}
A web.dev article says it does, but no working example could be produced in any browser. The specification isn't explicit about it either way, so the outcome is open.
Removing the marker takes only an empty content value. From there, a custom marker can be built on the <summary> element's ::before pseudo-element — though Safari, lacking ::marker support, paints both the default marker and the custom one.
Styling and animating the content
A background color can be placed on the whole <details> element, or better, gated on the open state with an attribute selector:
details {
background: oklch(95% 0.1812 38.35);
}
details[open] {
background: oklch(95% 0.1812 38.35);
}
To exclude <summary> from that background, you can wrap the content in a <div> and select it — or, without extra markup, use the ::details-content pseudo-element:
details[open] div {
background: oklch(95% 0.1812 38.35);
}
::details-content {
background: oklch(95% 0.1812 38.35);
}
No details prefix is needed: ::details-content is only selectable inside a <details> element. The pseudo-element is still gaining support, so use it cautiously.
Animating open and closed states once required hackery — the Web Animations API in one approach, a CSS animation with explicit sizing and positioning in Robin Rendle's, since the expanded height can't be known in advance. CSS can now transition from zero height to intrinsic (auto) height. Start at zero height and clip the overflow, then target ::details-content directly.
details[open] p {
animation: animateDown 0.2s linear forwards;
}
@keyframes animateDown {
0% {
opacity: 0;
transform: translatey(-15px);
}
100% {
opacity: 1;
transform: translatey(0);
}
}
::details-content {
transition: height 0.5s ease, content-visibility 0.5s ease allow-discrete;
height: 0;
overflow: clip;
}
Opt into auto-height transitions with interpolate-size, placed on :root so it applies everywhere, then set the height to auto on the open <details>:
:root {
interpolate-size: allow-keywords;
}
[open]::details-content {
height: auto;
}
Guard it behind a feature query so it only applies where auto-height transitions are supported, and set the transition on the pseudo-element:
@supports (interpolate-size: allow-keywords) {
:root {
interpolate-size: allow-keywords;
}
[open]::details-content {
height: auto;
}
}
::details-content {
transition: height 0.5s ease;
height: 0;
overflow: clip;
}
/* Browser supports interpolate-size */
@supports (interpolate-size: allow-keywords) {
:root {
interpolate-size: allow-keywords;
}
[open]::details-content {
height: auto;
}
}
Opening animates, but closing snaps. Per Bramus, content-visibility must also be included in the transition: it is implicitly set on the element and maps to a hidden state when the element is closed, which is what causes the snap.
::details-content {
transition: height 0.5s ease, content-visibility 0.5s ease allow-discrete;
height: 0;
overflow: clip;
}
/* Browser supports interpolate-size */
@supports (interpolate-size: allow-keywords) {
:root {
interpolate-size: allow-keywords;
}
[open]::details-content {
height: auto;
}
}
The allow-discrete keyword is required because content-visibility supports only discrete animation.
Tricks worth borrowing
- Chris Coyier's floating-footnote system; adding the
nameattribute to each footnote makes one close when another opens. - John Rhea's "Pop(over) The Balloons" game, which combines
<details>with the Popover API. - Bramus's horizontal accordion, which uses the
<details>element as a flex container. - Chris Coyier's play/pause effect for animated GIFs, which doesn't literally pause the file.
- Ryan Trimble's dropdown menu styling, with anchor positioning setting where the content opens.



