For a panel of unknown content height, measure the content and transition the panel between zero and that measured size. A height transition in CSS needs endpoints the browser can interpolate; an intrinsic size such as auto needs additional browser support. Start with the measured approach when consistent animation across your supported browsers matters, then consider a CSS-only option where its tradeoffs fit.
CSS Height Transition With a Measured Endpoint
Imagine a button revealing a short help panel. The outer panel has no padding or border, so its CSS transition height value matches the inner content’s measured height. Padding belongs on the inner element. The closed panel is also inert, keeping its controls out of keyboard navigation until it opens.
<button id="toggle" aria-expanded="false" aria-controls="panel">
Show help
</button>
<div id="panel" class="panel" inert>
<div id="panel-content" class="panel-content">
<p>Here is the help content.</p>
</div>
</div>
.panel {
height: 0;
overflow: hidden;
transition: height 300ms ease;
}
.panel-content { padding: 1rem; }
.panel-content p { margin: 0; }
@media (prefers-reduced-motion: reduce) {
.panel { transition: none; }
}
const button = document.querySelector("#toggle");
const panel = document.querySelector("#panel");
const content = document.querySelector("#panel-content");
let open = false;
function sizePanel() {
if (open) panel.style.height = `${content.offsetHeight}px`;
}
button.addEventListener("click", () => {
open = !open;
button.setAttribute("aria-expanded", String(open));
button.textContent = open ? "Hide help" : "Show help";
if (!open && panel.contains(document.activeElement)) button.focus();
panel.inert = !open;
panel.style.height = open ? `${content.offsetHeight}px` : "0px";
});
new ResizeObserver(sizePanel).observe(content);
When the button opens the panel, the script reads the inner element’s rendered height and assigns a pixel value to the outer element. The CSS height transition then runs from 0 to that value. On close, it returns to zero. A ResizeObserver updates the target if text wraps, an image loads, or content changes while the panel is open.
A CSS transition height measurement is a snapshot, so the observer matters when the content can grow. Here offsetHeight includes the inner element’s padding and border; the outer element has neither. Keep margins inside the measured box, as the paragraph rule does, so they do not escape the measurement. Test rapid toggles and content changes in the browsers your audience uses. The observer watches the content rather than the animated outer box, avoiding a feedback loop on each animation frame. It continues to observe when the panel is closed, but its callback updates the target only while open. In an application that removes the component, disconnect the observer during teardown so it no longer watches an abandoned element.
Why auto needs special treatment
A transition usually calculates intermediate values between two endpoints. A fixed length such as 0px and an intrinsic height such as auto have historically lacked an interpolated path in many browsers. Without support for intrinsic-size interpolation, changing directly from height: 0 to height: auto often produces a jump even if a transition is declared.
A CSS transition height target expressed in pixels gives the browser a number at each end. The measured example keeps the panel at a pixel height while open and updates that height when the inner box changes. Another design can switch to auto after opening, but must measure the current size and establish a pixel starting point before closing; interruption and reduced-motion cases need care.
The CSS height transition also changes layout as it runs, moving the content below the panel. That is often the intended effect for an accordion. For a large or complex page, check performance in DevTools: repeated layout during animation can become expensive. A transform can be smoother for an overlay, but scaling does not move neighboring content in the same way. Inspect both the visual motion and the surrounding layout: a reveal that looks smooth but overlaps the next control may not suit an in-flow panel. For a CSS transition of height, also test the longest content on a narrow viewport, where wrapping can substantially increase the measured size.
The max-height shortcut
For a small panel with a known upper bound, transitioning max-height is simple. Give the closed panel max-height: 0; overflow: hidden, then change the maximum to a value large enough for the open content. The real height remains content-sized until it reaches that limit. If content exceeds the cap, it will be clipped, so test translated text, narrow screens, and larger text settings.
The apparent duration depends on the relationship between the cap and the content. With a linear transition from 0 to 1000px over one second, content that is 200px tall becomes fully visible after roughly 200 milliseconds, although the property continues transitioning for the full second. Easing changes that timing further. On closing, the invisible part of the range runs first, so the panel may appear to wait before it starts to shrink. Choosing a cap closer to the expected content size reduces the mismatch, but increases the risk of clipping if the content grows. A CSS transition height cap much larger than the content can therefore make opening feel abrupt and closing appear delayed.
Animate an intrinsic size where supported
Browsers that support interpolate-size: allow-keywords can interpolate between a length and an intrinsic keyword such as auto. Use feature detection and provide a usable fallback; support is still limited across browsers, so check compatibility against your actual browser requirements. One endpoint must be a length or percentage: the property does not enable interpolation between two intrinsic keywords.
.panel { height: 0; overflow: hidden; }
.panel.is-open { height: auto; }
@supports (interpolate-size: allow-keywords) {
.panel { interpolate-size: allow-keywords; transition: height 300ms ease; }
}
@media (prefers-reduced-motion: reduce) {
.panel { transition: none; }
}
This is an alternative to the measured script, not an addition to it: toggle is-open, aria-expanded, and inert together without setting an inline height. Unsupported browsers still open the panel, but without this animation. The intrinsic CSS height animation follows the content’s natural open size without a hard-coded cap. Verify dynamic changes and interruption behavior in your target browsers; support for the feature does not make every interaction identical. A CSS transition between heights also needs a real state change after styles have been applied. If the panel is inserted already open, there may be no previous closed height to animate from; plan its initial state accordingly.
Clipping keeps content from spilling during a CSS height change, but persistent overflow: hidden can also clip focus outlines, shadows, and menus. Design space for these elements or adjust overflow after opening if the component needs it. A height animation in CSS should respect reduced-motion preferences, and the button’s state must stay synchronized with what users can reach.
Choose measurement when broad support and a precise endpoint matter, max-height for a bounded panel where timing differences are acceptable, and intrinsic-size interpolation when the target browsers support it or an immediate fallback is fine. For each option, test the longest realistic content and a fast open-close interaction before shipping.
