← Back to HTML & CSS, page 9

html-css-0166 · Medium mid

How do you animate an element open from height 0 to its natural height when height: auto cannot be animated?

height: auto cannot be interpolated. A grid with grid-template-rows going from 0fr to 1fr animates to the content height with no script, if the child has overflow: hidden and min-height: 0. Newer CSS can animate to auto directly, but check support and hide closed content from the keyboard.

Full answer

A transition needs two numbers to move between. height: 0 is a number, but height: auto is not, so transition: height jumps at the end and does not animate. This is a common need for accordions and expanding panels.

Options:

  1. Animate max-height to a large value such as 500px. It works, but the speed is wrong when the content is shorter than the guess, and the panel can be cut when the content is taller. It is a rough fix.
  2. Measure with JavaScript. Read scrollHeight and animate the height to that number, then set auto at the end. It is exact, but needs script and care when content changes size.
  3. Animate a grid row. A grid container with grid-template-rows: 0fr becomes 1fr when open. The track size fr is a number, so it interpolates, and the track ends up as tall as the content. The child needs overflow: hidden and min-height: 0, so it can shrink to zero. This needs no script apart from toggling a class.
  4. Newer CSS can animate to auto directly with interpolate-size: allow-keywords or with calc-size(). Check the support table before relying on it, and keep option 3 as the fallback.

Also remember accessibility: a closed panel should be hidden from the keyboard and from screen readers, for example with visibility: hidden after the transition, inert, or the hidden attribute, so its links are not focusable when it is closed. Respect reduced motion by shortening or removing the transition.

Closed, the panel has zero height. After the click, it grows over 0.3 seconds to the exact height of the three lines, with no number written in the CSS.

Add inert or visibility: hidden to the closed content when it holds links or buttons.

html
<style>
  .wrap { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.3s ease; }
  .wrap.open { grid-template-rows: 1fr; }
  .inner { overflow: hidden; min-height: 0; }
  @media (prefers-reduced-motion: reduce) { .wrap { transition: none; } }
</style>
<button id="b" type="button" aria-expanded="false" aria-controls="w">Toggle</button>
<div class="wrap" id="w"><div class="inner" id="inner"><p style="margin:0">Line one<br>Line two<br>Line three</p></div></div>
<script>
  b.addEventListener("click", () => {
    const open = w.classList.toggle("open");
    b.setAttribute("aria-expanded", open);
  });
</script>

Checked in Chrome 156 only, by clicking and reading the height of the inner box after the change. The animation was not watched by eye. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Auto is not a number, so it cannot be transitioned.
  • max-height is a rough workaround.
  • grid-template-rows 0fr to 1fr animates to the content height.
  • The child needs overflow: hidden and min-height: 0.
  • Hide closed content from keyboard and screen readers.

Common mistakes

  • Using max-height with a guessed value and getting wrong timing or cut content.
  • Forgetting min-height: 0 and overflow: hidden on the grid child.
  • Leaving links in a closed panel focusable.

Follow-ups

  • Why does the child need min-height: 0?
  • How would you hide the closed content from keyboard users?

Tips

  • Explain why auto cannot be animated, then list the options in order of quality.
  • Describe the 0fr to 1fr trick and its two required properties.