Animating height to auto with interpolate-size
Why the max-height trick feels wrong, and the one line of CSS that lets an accordion transition to height: auto.
By Moshiur Rahman3 min readCSS, Animation
For years, animating an accordion to height: auto meant faking it.
Open and close both panels below. They use the same duration and the same easing. The left one uses the old max-height trick, and the right one transitions to a real height: auto.
Three projects, unlimited viewers and a month of history. Upgrade when you need more seats; your work stays where it is.
Three projects, unlimited viewers and a month of history. Upgrade when you need more seats; your work stays where it is.
Why auto doesn't animate
CSS can only tween between two numbers. auto isn't a number until layout has run, so a transition from height: 0 to height: auto has no midpoint to calculate. The browser jumps to the end.
The max-height trick
The common workaround is to transition max-height to a value bigger than the panel will ever be:
.panel {
max-height: 0;
overflow: hidden;
transition: max-height 800ms ease;
}
.open .panel {
max-height: 1000px;
}It opens, but the timing is wrong. The transition runs from 0 to 1000px, and the panel is only about 70px tall. On open, it reaches full height in the first few percent of the duration and then the rest of the transition does nothing visible. On close, it's worse: the first 930px of the transition change nothing, so the panel sits still and then snaps shut. Push the duration slider up in the demo and it's hard to miss.
The other workaround was JavaScript: read scrollHeight, set that as a pixel height, animate it, and put auto back when the transition ends.
interpolate-size
interpolate-size: allow-keywords tells the browser it's allowed to interpolate between a length and an intrinsic size keyword like auto, min-content or fit-content:
:root {
interpolate-size: allow-keywords;
}// No built-in utility yet, so use an arbitrary property
<html lang="en" className="[interpolate-size:allow-keywords]">The property is inherited, so setting it on :root turns it on for the whole page. After that, the plain version works:
.panel {
height: 0;
overflow: hidden;
transition: height 800ms ease;
}
.open .panel {
height: auto;
}<div
data-open
class="h-0 overflow-hidden transition-[height] duration-800 ease-in-out
data-open:h-auto"
>
…
</div>It's off by default because turning it on everywhere could change how existing sites animate. allow-keywords lets you go between a length and a keyword. It still can't animate from one keyword to another, like auto to min-content.
Just one element: calc-size()
If you'd rather not change the whole page, calc-size() does it for a single value:
.open .panel {
height: calc-size(auto, size);
}<div class="h-0 overflow-hidden transition-[height] data-open:h-[calc-size(auto,size)]">calc-size(auto, size) resolves to the same height as auto, but as a value the browser knows it can interpolate. You can also do math on it, like calc-size(auto, size + 2rem).
Support
At the time of the original post, interpolate-size and calc-size() were in Chrome and Edge 129 and later, and not yet in Firefox or Safari. Check MDN for the current state.
Where it isn't supported, the property is ignored and the panel opens instantly, which is what a plain height: auto toggle always did. Nothing breaks, so you can ship it today as an extra for the browsers that have it.
Need this on your site?
I build interfaces like this for agencies, startups and SaaS teams.
Let's talk