← Tous les articles

CSS: animating an accordion to height: auto (grid, interpolate-size and details) without magic numbers

Why transition doesn't work with height: auto, why the max-height trick gives lopsided animations, and the reliable ways to open an accordion smoothly while keeping it keyboard-accessible.

CSS: animating an accordion to height: auto (grid, interpolate-size and details) without magic numbers

Key points

  • A transition can’t animate to height: auto: the browser can’t interpolate between 0 and a value it doesn’t know yet, so the element just snaps open.
  • The method that works in every current browser: animate grid-template-rows from 0fr to 1fr, with min-height: 0 on the child.
  • interpolate-size and ::details-content animate auto directly but are limited to Chromium-based browsers for now, so treat them as progressive enhancement.

It always goes the same way: an FAQ, a mobile menu or a “Read more” block should open smoothly. You write height: 0 for closed, height: auto for open, add a nice transition… and the block snaps open. A quick search turns up the max-height trick, and now closing feels oddly sluggish. Accordions show up on nearly every project we build (FAQs, product pages, filters), so we’ve settled on a method. Here it is, along with the modern alternatives and the accessibility details that tend to get forgotten.

Why height: auto doesn’t animate

To animate a property, the browser works out in-between values from start to end: from 0 to 240 pixels it goes through 24, 48, 72 and so on. auto isn’t a value, it’s an instruction: “work the height out from the content”. Historically, CSS couldn’t interpolate between 0 and that instruction, so it jumps straight to the end state, with no error and no warning. That silence is what eats the time.

The same limitation applies to width: auto, height: fit-content and every other sizing keyword. It isn’t your code; it’s a gap in the language that the spec has only recently closed (more on that below), and not everywhere yet.

The max-height trick, and why we avoid it

The fix you’ll find most often animates max-height, which takes fixed values, using a “big enough” number for the open state:

.panel {
  max-height: 0;
  overflow: hidden;
  transition: max-height .3s ease;
}
.panel.open {
  max-height: 1000px; /* an arbitrary value: that's the whole problem */
}

It looks fine at first, with three flaws. One: the animation runs across the full range up to 1000 pixels. If the content is 150 pixels tall, opening visually finishes in a fraction of the duration, and closing seems to start late, because the browser first travels from 1000 down to 150 with nothing visible moving. Two: if the content ever exceeds the chosen value (a longer FAQ answer, a translation, a narrow screen wrapping text onto more lines), it gets clipped. Three: the magic number differs from component to component and nobody remembers why it’s 1000 here and 2000 there.

The other classic route, measuring content with scrollHeight in JavaScript and applying a pixel height, animates precisely. But you then have to handle window resizing, switching back to auto once open, and content changing while the panel is open. A lot of code for a visual effect.

The reliable method: animate grid-template-rows from 0fr to 1fr

The trick that changed things: grid-template-rows is animatable, including between 0fr and 1fr. A single-row grid at 1fr takes exactly the height of its content; at 0fr it collapses to nothing. The browser can interpolate between the two without knowing the height up front, and every current browser supports it.

.panel {
  display: grid;
  grid-template-rows: 0fr;
  visibility: hidden;              /* closed content: no longer reachable by keyboard */
  transition: grid-template-rows .3s ease, visibility .3s;
}
.panel.open {
  grid-template-rows: 1fr;
  visibility: visible;
}
.panel > .panel-content {
  min-height: 0;                   /* essential: without it the row won't shrink to 0 */
  overflow: hidden;
}
/* Padding goes on an inner element, never on .panel-content */
.panel-content > .body { padding-block: 1rem; }

@media (prefers-reduced-motion: reduce) {
  .panel { transition: none; }
}

Two lines are essential and almost always missed. min-height: 0 on the child: by default a grid item can’t shrink below its content, so the row refuses to reach zero. overflow: hidden: without it, the content spills visibly out of the closed panel.

The trap we’ve hit most on inherited sites: padding applied straight to that child. Even at 0fr the padding remains, and the “closed” panel keeps a 2rem strip. Put the spacing on an inner element, as in the example. Same for borders: put them on the outer container or further in, never on the element that collapses.

The HTML and JavaScript stay tiny: a real <button>, an aria-expanded attribute reflecting state, and a class that triggers the animation:

<button type="button" class="trigger" aria-expanded="false" aria-controls="faq-1">
  What are your lead times?
</button>
<div id="faq-1" class="panel">
  <div class="panel-content">
    <div class="body"><p>…</p></div>
  </div>
</div>

<script>
document.querySelectorAll('.trigger').forEach((button) => {
  const panel = document.getElementById(button.getAttribute('aria-controls'));
  button.addEventListener('click', () => {
    const open = button.getAttribute('aria-expanded') === 'true';
    button.setAttribute('aria-expanded', String(!open));
    panel.classList.toggle('open', !open);
  });
});
</script>

Accessibility: what the animation mustn’t break

A panel collapsed to 0 pixels is still there for keyboards and screen readers. Links and buttons inside it remain reachable with Tab, so keyboard users end up with an invisible focus inside content they can’t see. That’s the job of visibility: hidden in the example: an element hidden that way can’t be focused or read. And visibility plays nicely with the transition: when opening, content becomes visible at the start of the animation; when closing, it’s hidden only at the end. Adding the inert attribute to the closed panel from JavaScript is an equally valid alternative.

The trigger must be a <button>, not a clickable <div>: it’s focusable, responds to Enter and Space, and is announced as a button. aria-expanded tells screen readers whether it’s open, and aria-controls links the button to its panel.

Then there’s prefers-reduced-motion: some people set their system to reduce animation because motion genuinely bothers them. Dropping the transition for them costs one rule, and the accordion works just as well.

If all you need is a disclosure block, the native <details> element with its <summary> already handles keyboard, state and screen reader announcements, with no JavaScript. In WordPress, that’s what the Details block outputs. Its one long-standing weakness was animation, and that’s exactly what’s changing.

The modern options: interpolate-size and ::details-content

CSS finally addressed the problem with the interpolate-size property (and its sibling, the calc-size() function). Declared once on the root as allow-keywords, it allows transitions to and from auto. The ::details-content pseudo-element targets the content of a <details>, which at last lets you animate it opening:

/* 1. Allow animating to and from auto (Chromium for now) */
:root { interpolate-size: allow-keywords; }

.panel {
  height: 0;
  overflow: hidden;
  transition: height .3s ease;
}
.panel.open { height: auto; } /* animated where supported, instant elsewhere */

/* 2. Same idea applied to <details>, no JavaScript at all */
details::details-content {
  block-size: 0;
  overflow: hidden;
  transition: block-size .3s ease, content-visibility .3s allow-discrete;
}
details[open]::details-content { block-size: auto; }

At the time of writing these features ship in Chromium-based browsers (Chrome, Edge, Opera, Samsung Internet), not yet everywhere. Check current support on caniuse.com when you start your project. The good news is that they’re perfect for progressive enhancement: where they aren’t recognised, the panel simply opens without animation. Nothing breaks.

What we use today: the grid-template-rows technique for custom components, because it animates identically in every browser; <details> with ::details-content for simple FAQs, accepting an unanimated open in browsers that don’t support it yet. Either way, no more magic numbers to maintain.

FAQ

Why won’t my grid accordion close completely?

In order of likelihood: min-height: 0 is missing on the direct child, the direct child has padding or a border, or the panel has more than one direct child (so the grid creates several rows). Wrap all content in a single element and move spacing inside it.

Is animating height bad for performance?

Animating a dimension forces the browser to recalculate layout every frame, which costs more than animating transform or opacity. For a reasonably sized accordion it’s negligible. Just avoid animating large areas of the page at once, and keep durations short, around 200 to 300 milliseconds.

Need interface components that are smooth, accessible and easy to maintain? We design and build custom accordions, menus and WordPress blocks, tested with a keyboard and on mobile.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →