css and layoutsanimayions

Animating display: none and height: auto Without JavaScript

The FAQ accordion should expand smoothly. You can't animate height: auto, so you reach for the tried-and-true trick:

css

.panel {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease;
}
.panel.is-open {
max-height: 1000px;
}

Opening looks almost right. Closing looks odd: for a moment nothing happens, and then the panel suddenly collapses in a fraction of a second. The reason is arithmetic. The transition lasts 300 ms, but it's measured from 1000px, while the panel is actually 200px tall. With a linear curve, for the first 240 ms max-height drops from 1000 to 200px – a range you can't see. That leaves 60 ms for the visible part. The bigger the margin you give it, the worse it gets, and too small a margin cuts off longer content.

display: none is even worse. A dropdown should fade out via opacity, but along with opacity: 0 it gets display: none – and vanishes instantly, because display has no intermediate values. Hence the code everyone has written at some point: first add the animation class, wait for transitionend or a setTimeout, and only then hide the element. And when showing it – the reverse: remove display: none, force a style recalculation with a trick like el.offsetHeight or a double requestAnimationFrame, and only then add the class so the browser notices the change.

Since 2024, both problems can be solved in CSS alone. But to understand why the solutions look the way they do, you need to know where these limitations came from.

Why display and auto couldn't be animated

A CSS transition needs two values between which an intermediate state can be computed. opacity: 0.4 lies between 0 and 1, translate: 0 12px lies between 0 and 24px. The browser simply interpolates numbers.

display is a discrete property – there's no "halfway" value between none and block. That's why a display change was always instant and, more importantly, instantly interrupted all other transitions: an element with display: none isn't rendered, so there's nothing to animate.

height: auto is a different problem. auto isn't a number, it's an instruction: "compute the height from the content". The browser only knows the result after layout, and interpolation between 0 and a keyword wasn't defined – so the spec simply said: don't animate. max-height with a margin was a way of swapping the keyword for a number, at the cost of all the arithmetic described above.

allow-discrete: a display that waits for the animation to end

The first missing piece is transition-behavior: allow-discrete. It lets you add discrete properties to a transition – and for display, the browser applies a special rule: the element stays visible for the entire duration of the transition. When hiding, display switches to none only at the very end; when showing, at the very beginning.

css

.dropdown {
opacity: 1;
transition:
opacity 0.2s ease,
display 0.2s allow-discrete;
}
.dropdown.is-hidden {
opacity: 0;
display: none;
}

Adding the .is-hidden class now gives you a smooth fade-out: for 200 ms opacity drops to zero while the element is still in place. Only when the transition ends does display: none actually remove it from layout and from the accessibility tree. No transitionend, no setTimeout.

Hiding works. But when you remove the class, the dropdown appears without animation – and that's the second missing piece.

@starting-style: where to start when the element wasn't there a moment ago

A transition happens when the browser sees a value change between the previous and the new style state. An element that has just stopped being display: none – or that has just been inserted into the DOM – has no previous state. The browser has never rendered it, so there's nothing to start the transition from, and it shows the end state immediately. That's exactly why the old solutions forced a style recalculation with the offsetHeight trick: the point was to make the browser "see" the element with opacity: 0 before it got opacity: 1.

@starting-style gives the browser that missing state directly:

css

.toast {
opacity: 1;
translate: 0 0;
transition:
opacity 0.3s ease,
translate 0.3s ease;
}
@starting-style {
.toast {
opacity: 0;
translate: 0 16px;
}
}

When a new .toast lands in the DOM, the browser treats the rules in @starting-style as its "previous" style and animates from opacity: 0 to opacity: 1. This works on an element's first render: after insertion into the tree, or after switching from display: none to visible. It doesn't apply to a regular class change on an element that's already visible – there the previous state exists, so @starting-style isn't needed.

In practice this means React can simply render a toast conditionally ({visible && <Toast />}), and the entrance animates on its own. The exit is another story – React removes the element from the DOM right away, so an exit animation requires either keeping the element in the tree with display: none and allow-discrete, or View Transitions, which I covered in a separate article.

The pitfall: @starting-style loses to the rule it was meant to override

This bug usually costs a good chunk of time, because the code looks correct and nothing reports an error:

css

@starting-style {
.toast {
opacity: 0;
}
}
.toast {
opacity: 1;
transition: opacity 0.3s ease;
}
/* No entrance animation. */

@starting-style isn't a separate layer and doesn't raise specificity. Its rules take part in the regular cascade – and here both rules have the same specificity, so the later one wins. The starting state computed from the cascade is opacity: 1, the same as the end state, so there's nothing to animate. There are two fixes: put @starting-style after the base rule, or nest it inside it:

css

.toast {
opacity: 1;
transition: opacity 0.3s ease;
@starting-style {
opacity: 0;
}
}

The nested version is safer, because the starting state sits with the element it belongs to, and it can't accidentally be moved in front of the base rule while tidying up the file. (I covered the rules of nesting – and what happens to declarations after a nested rule – in the article on native nesting.)

Popover and dialog: the overlay everyone forgets

Combining both mechanisms gives you a full popover or <dialog> animation – entrance and exit, without a line of JavaScript:

css

[popover] {
opacity: 0;
translate: 0 8px;
transition:
opacity 0.2s ease,
translate 0.2s ease,
display 0.2s allow-discrete,
overlay 0.2s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
translate: 0 0;
@starting-style {
opacity: 0;
translate: 0 8px;
}
}

The most often overlooked line of the transition is the last one: overlay. In the Popover API article I wrote about the top layer – the layer above the entire document that the browser moves an open popover and a modal dialog into. On close, the element is removed from that layer immediately, and overlay is the property that describes whether the element is in it. Without adding it to the transition, the popover drops out of the top layer on the first frame of closing – and its exit animation plays beneath the rest of the page, clipped by ancestors with overflow: hidden, or simply invisible. With overlay … allow-discrete, the element stays in the top layer until the animation ends.

For a modal <dialog>, the same applies to ::backdrop – the backdrop also needs its own transition and its own @starting-style, otherwise the dimming appears and disappears abruptly. It's also worth knowing that support for overlay isn't as broad as for allow-discrete and @starting-style – check the current state before you base an entire animation on it. In a browser without support, the exit simply won't animate, and the dialog will still close correctly.

height: auto: interpolate-size and calc-size()

The second problem is solved by interpolate-size. Set to allow-keywords, it lets you animate between a length and the sizing keywords: auto, min-content, max-content, fit-content.

css

:root {
interpolate-size: allow-keywords;
}
.panel {
height: 0;
overflow: clip;
transition: height 0.3s ease;
}
.panel.is-open {
height: auto;
}

This is exactly the code everyone has always wanted to write, and now it works. The transition takes 300 ms from 0 to the content's actual height, with no margin and no odd delay on close. The property is inherited, so the simplest approach is to enable it once on :root. It isn't the default for a simple reason: enabling it globally could change the look of existing sites that – knowingly or not – relied on auto not animating.

If you need more than plain auto, there's calc-size(): height: calc-size(auto, size + 2rem) means "the content height plus 2rem", and that value can be animated too. Without calc-size(), calc(auto + 2rem) would simply be invalid.

The catch: at the time of writing, interpolate-size and calc-size() are supported in Chromium (since version 129), but not yet in Safari or Firefox. Fortunately, the lack of support degrades gracefully here – in other browsers the panel opens instantly, without animation, but correctly.

An accordion on details without JavaScript

It gets most interesting when you combine this with native <details>. The browser handles the keyboard, the open state and screen reader semantics on its own – and recently also exclusivity: several <details> elements with the same name attribute work as an accordion in which only one can be open.

html

<details name="faq">
<summary>How long does delivery take?</summary>
<p>Usually 1–2 business days.</p>
</details>
<details name="faq">
<summary>Can I return a product?</summary>
<p>Yes, within 30 days of purchase.</p>
</details>

For animation, one thing was missing: access to the content container. The browser hides the contents of a closed <details> with content-visibility: hidden on an internal element you had no way to reach. Now there's the ::details-content pseudo-element:

css

details {
interpolate-size: allow-keywords;
}
details::details-content {
block-size: 0;
overflow: clip;
transition:
block-size 0.3s ease,
content-visibility 0.3s allow-discrete;
}
details[open]::details-content {
block-size: auto;
}

allow-discrete shows up again, this time for content-visibility – for the same reason as with display. Without it, the browser would hide the content on the first frame of closing, and the height animation would play out on an empty container. The result: an accordion with animation, exclusivity, keyboard support and correct semantics – without a line of JavaScript and without the ARIA that had to be added by hand in the accessible components article. As with interpolate-size itself, a browser without support shows an accordion that works, just without animation.

A trick that works everywhere today

If the height animation has to work in every browser right now, there's a solution that doesn't need interpolate-size – and doesn't have the drawbacks of max-height. It relies on the fact that since 2023 every browser can animate grid-template-rows:

css

.panel {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.3s ease;
}
.panel.is-open {
grid-template-rows: 1fr;
}
.panel > .panel__inner {
overflow: hidden;
}

0fr is a grid track with zero height, 1fr is a track taking up the content's full height – and the browser interpolates fr fractions like numbers. The inner element with overflow: hidden is necessary so the content doesn't spill out of the zero-height track. This trick has one drawback: it requires an extra element in the HTML. But the timing is correct in both directions, because the animation always runs to the content's actual height, not to an invented margin.

Pitfalls and best practices

  • Height isn't transform. In the microinteractions article I wrote that transform and opacity animate on the compositor thread. height doesn't: every frame means a new layout, for everything below too. For an FAQ accordion it doesn't matter, but animating the height of a long list on a weak phone will show. Keep durations short (200–300 ms).
  • @starting-style after the base rule, or inside it. Placed earlier, it loses the cascade and the entrance animation disappears without any error.
  • @starting-style only works on first render. On insertion into the DOM and on leaving display: none. Toggling classes on a visible element animates with a regular transition.
  • For popovers and dialogs, add overlay to the transition – plus a separate transition for ::backdrop. Without it, the exit plays outside the top layer.
  • Respect prefers-reduced-motion. Expanding panels and sliding elements are motion that's a real problem for some users. The simplest approach is to set transition-duration to zero in @media (prefers-reduced-motion: reduce) – the element still appears, just without animation.
  • Browser support. @starting-style and transition-behavior: allow-discrete work in Chrome 117, Safari 17.4–17.5 and Firefox 129 (2024). interpolate-size and calc-size() – at the time of writing, Chromium only; for other browsers the grid-template-rows trick is the best choice.

Summary

For twenty years, display: none and height: auto were two things CSS couldn't animate, because the first is discrete and the second isn't a number. transition-behavior: allow-discrete makes display wait until the animation ends before switching, @starting-style gives an element that didn't exist a moment ago a starting state, and interpolate-size: allow-keywords finally lets you write height: auto in a transition. Together with ::details-content and the name attribute they give you an animated accordion without JavaScript, and with overlay – a smoothly closing popover. Remember the two things that break this silently: @starting-style has to come after the base rule, and a popover without overlay in its transition loses the top layer on the first frame of closing. When you need full browser support today, grid-template-rows from 0fr to 1fr does the same as interpolate-size, with one extra element in the HTML.