Why Won't My Accordion Animate? The height: auto Transition Fix
October 5, 2026 | 8 min read | CSS
By Syed · Frontend Engineer & Creator of UITweak
You built an accordion. The button toggles a class, the panel gets height: auto, and you added transition: height 0.4s ease. Then you click it and the panel snaps open instantly, no animation at all. The transition is not broken; it never had anything to animate with. CSS can interpolate between two numbers, and auto is not a number, it is a layout instruction the browser resolves only after layout finishes. The fix is never to force a transition on auto; it is to give the browser two values it can actually count between. Five ways below, from the one you should reach for first.
30-second diagnosis: in DevTools, watch the panel's computed height while you toggle it. If it jumps straight from 0px to the full pixel height with no intermediate values, you are transitioning to or from auto, min-content, or another keyword. Any transition touching a keyword does nothing; the fixes below replace the keyword with something measurable.
Why This Happens
A CSS transition works like a ruler: at each frame it picks a value between the start and the end. Between 0px and 240px there are infinitely many valid midpoints. But between 0px and auto there is no midpoint at all, because auto does not become a number until the browser has already laid the page out, which happens after the transition would have needed it. So the browser does the only thing it can: it flips to the end state instantly. The same applies to min-content, max-content, and fit-content. This is not a browser bug; every engine behaves this way.
Fix 1: The Grid 0fr to 1fr Trick (Use This One)
The most reliable pure-CSS fix needs no pixel values at all. Make the panel a grid with one row, and animate the row between 0fr and 1fr. Fraction units are interpolatable, so the transition runs smoothly, and the row naturally sizes to whatever the content needs when open:
.panel {
display: grid;
grid-template-rows: 0fr; /* collapsed */
transition: grid-template-rows 0.4s ease;
}
.panel.open {
grid-template-rows: 1fr; /* open: fits content exactly */
}
.panel > .panel-inner {
overflow: hidden; /* required: lets the 0fr row fully collapse */
min-height: 0; /* required: keeps fr minimums from holding it open */
}The two inner declarations are the ones everyone forgets and then blames the trick for. Without overflow: hidden the content refuses to clip during the animation; without min-height: 0 some browsers will not let the row collapse below the content's automatic minimum size. With all three pieces this works in every modern browser (Chrome 107+, Safari 16+, Firefox 66+), and content that changes while the panel is open just works, because nothing was ever measured in pixels.
One practical step remains: picking the easing and duration. Build the expand motion in our free CSS Animation Generator, preview it live, and copy the exact cubic-bezier() value into the transition line above. A snappy accordion usually wants something like cubic-bezier(0.4, 0, 0.2, 1) over 0.35-0.45s; design it by feel in the tool instead of guessing the numbers.
Fix 2: interpolate-size: allow-keywords (the Native Fix)
Modern CSS finally has a first-class answer: opt in to interpolating size keywords, and then height: auto transitions directly, exactly the way you originally tried:
:root {
interpolate-size: allow-keywords; /* opt-in, inherited by everything */
}
.panel {
height: 0;
overflow: hidden;
transition: height 0.4s ease;
}
.panel.open {
height: auto; /* now animates smoothly */
}The honest catch: as of 2026 this is effectively Chromium-only (Chrome 129+). Firefox and Safari have not shipped it yet, and MDN still marks it experimental. Use it as progressive enhancement on top of Fix 1, never as the only fix: browsers without support ignore the declaration and the grid trick carries the animation. The companion function calc-size() lets you mix keywords with math, like height: calc-size(auto, size + 24px) for a panel that always animates with 24px of breathing room, but it shares the same Chromium-only support.
Fix 3: max-height, But Done Right
The classic StackOverflow answer is to transition max-height from 0 to a big number like 999px. It technically animates, and it technically feels wrong. Here is why: the transition runs over the entire 999px range, so a 180px panel spends most of the animation time animating empty space. The visible motion finishes early and the panel appears to sit still at the end; the delay makes it feel broken even though it "works".
.panel {
max-height: 0;
overflow: hidden;
transition: max-height 0.4s ease;
}
.panel.open {
/* keep the range TIGHT: ~1.5x your tallest content, not 9999px */
max-height: 320px;
}There is one place this technique is genuinely fine: when you know the content's maximum size, like a dropdown menu or a notification tray that never exceeds ~300px. Set the max just above that and the timing distortion is small enough to ignore. For unknown-length accordion content, prefer Fix 1, which has no distortion at all.
Fix 4: The scrollHeight Fallback (JavaScript)
When you need the animation in browsers too old for the grid trick, or the content height changes while open and you want exact control, measure the content with scrollHeight and hand the browser two real numbers:
function togglePanel(panel) {
const open = panel.classList.contains("open");
if (!open) {
panel.classList.add("open");
// 1. give the browser a real start AND end value
panel.style.height = panel.scrollHeight + "px";
// 2. after the animation, clear it so content stays flexible
panel.addEventListener("transitionend", () => {
panel.style.height = "";
}, { once: true });
} else {
// 1. freeze the current height (auto -> px)
panel.style.height = panel.scrollHeight + "px";
// 2. force a reflow so the browser registers the freeze...
panel.offsetHeight;
// 3. ...then animate down to zero
panel.style.height = "0px";
panel.classList.remove("open");
}
}The two details that make this version correct: on open, clearing the inline height after the transition means the panel goes back to natural sizing (content that grows later does not get stuck at the old pixel height), and on close, the forced reflow between the two assignments is what actually lets the browser animate instead of jumping. Skip the reflow and you are back to a snap.
Fix 5: Animating Panels Hidden With display: none
Accordions built on display: none have a second problem: display is discrete, it cannot transition at all, so the panel vanishes before the collapse even starts. Modern CSS handles this with transition-behavior: allow-discrete paired with @starting-style for the entrance, combined with the grid trick for the height:
.panel {
display: none;
grid-template-rows: 0fr;
transition:
grid-template-rows 0.4s ease,
display 0.4s ease allow-discrete;
}
.panel.open {
display: grid;
grid-template-rows: 1fr;
}
/* entrance starting point: begins collapsed, animates open */
@starting-style {
.panel.open { grid-template-rows: 0fr; }
}
.panel > .panel-inner {
overflow: hidden;
min-height: 0;
}Support for allow-discrete and @starting-style is broad in current browsers (Chrome 117+, Safari 17.4+, Firefox 129+), and older browsers simply fall back to instant show/hide, which is a safe degradation. Our @starting-style entry/exit guide covers the full pattern for dialogs, toasts, and popovers if you use it beyond accordions.
Quick Recap
- Use the grid
0fr to 1frtrick as your default: pure CSS, works everywhere modern, never measures pixels. - Remember the two inner rules:
overflow: hiddenandmin-height: 0on the content wrapper, or the row will not collapse. - Layer
interpolate-size: allow-keywordson top for the native fix where Chromium supports it. - Only use
max-heightwhen you know the content's maximum size, and keep the range tight. - Reach for
scrollHeightJavaScript only for legacy browsers or content that resizes mid-animation. - For
display: nonepanels, addtransition-behavior: allow-discreteand@starting-style.
Frequently Asked Questions
Why does my max-height accordion feel delayed?
Because the transition covers the whole range up to your large max value, not just the visible content. A 180px panel inside a max-height: 999px transition spends roughly the last two-thirds of the duration animating empty space. Shrink the max to about 1.5x your tallest content, or switch to the grid trick, which animates exactly the content's height.
Does animating grid-template-rows hurt performance?
Height-based animations trigger layout, unlike transform and opacity, which is why you should not use them for things that animate constantly (parallax, scroll effects). For an accordion that opens and closes on click, the cost is one short layout animation, which every modern browser handles without breaking a sweat.
Can I do the grid trick with Tailwind?
Yes, with arbitrary values: grid grid-rows-[0fr] transition-[grid-template-rows] duration-300 on the panel and grid-rows-[1fr] when open, plus overflow-hidden min-h-0 on the inner wrapper. In Tailwind v4 you can also define the open state with adata- attribute variant instead of toggling classes in JavaScript.
What about the native details and summary elements?
Native <details> is great for accessibility, but its open/close is instant by default. In Chromium you can animate it with ::details-content plus interpolate-size; cross-browser, wrap your summary content in the grid-trick panel from Fix 1 inside the <details> and toggle the open class from its toggle event.
Why does my panel flicker at the end of the animation?
Almost always padding or margin on the panel itself. The grid trick animates the row, but padding on the outer panel is outside the animation and appears/disappears instantly. Move padding to the inner wrapper so the whole visual box is inside the animated region.
Try it now: open the free CSS Animation Generator, dial in the expand motion for your accordion, and drop the copied cubic-bezier() into the transition line from Fix 1 above. Your panels will animate with zero pixel measurements and zero JavaScript.