Animate <details> with ::details-content and interpolate-size
Animating details open and close without measuring height
<details> has always snapped open and closed. Animating it meant reading scrollHeight in JS, setting a max-height, transitioning, and unsetting on close. ::details-content plus interpolate-size animates the real height natively.
The old hack vs the modern fix
View demo// .content needs overflow: hidden
document.querySelectorAll('details').forEach((d) => {
const summary = d.querySelector('summary');
const content = d.querySelector('.content');
summary.addEventListener('click', (e) => {
e.preventDefault();
if (d.open) {
const anim = content.animate(
{ height: [content.offsetHeight + 'px', '0px'] },
{ duration: 300, easing: 'ease' }
);
anim.onfinish = () => { d.open = false; };
} else {
d.open = true;
content.animate(
{ height: ['0px', content.offsetHeight + 'px'] },
{ duration: 300, easing: 'ease' }
);
}
});
});:root {
interpolate-size: allow-keywords;
}
details::details-content {
height: 0;
overflow: clip;
transition: height .3s ease, content-visibility .3s ease allow-discrete;
}
details[open]::details-content {
height: auto;
}
/* native details, animated open and close */<details class="[interpolate-size:allow-keywords] [&::details-content]:h-0 [&::details-content]:overflow-clip [&::details-content]:transition-[height,content-visibility] [&::details-content]:[transition-behavior:allow-discrete] [&[open]::details-content]:h-auto"><summary>Title</summary><div>Body</div></details>Details content browser support
::details-content is Baseline 2025. The height animation also needs interpolate-size, which only Chrome and Edge support so far (129+). In Firefox and Safari the details element still works but opens and closes instantly.
Live demo
Why the modern way wins
Real height, animated
interpolate-size: allow-keywords turns height: auto into something the browser can interpolate. No more fake max-height ladders or hidden measuring divs.
Native semantics intact
<details> still toggles, screen readers still announce, keyboard still works. The animation is layered on top, not replacing native behavior.
Targets the content directly
::details-content wraps everything inside <details> except the summary. Style it like any element: padding, transitions, transforms.
- Key Change
- Animate auto
- interpolate-size unlocks it
- Old Approach
- JS height measurement
- Read scrollHeight, transition, unset
- Modern Approach
- ::details-content + interpolate-size
- CSS only
How it works
Two pieces have to line up. First, interpolate-size: allow-keywords on the root tells the browser to interpolate between fixed values and intrinsic keywords like auto. Without it, height: auto is still a hard switch.
Second, ::details-content selects everything inside <details> except the <summary>. Set height: 0 in the closed state and height: auto in the open state, then transition between them. The allow-discrete keyword on the content-visibility transition keeps the content visible during the closing animation.
The same trick works for any container that needs to grow or shrink to fit its contents. Wrap the toggle in <details> and you get native keyboard handling, ARIA roles, and animated open and close, all from the platform.