# Accordions without the jump

Source: https://websgeek.com/accordions-without-the-jump
Publisher: WebsGeek (https://websgeek.com), updated 2026-11-19

Animating height so nothing around it lurches.

## The jump

An accordion hides content until someone asks for it. The simplest version toggles `display`, which works perfectly, and moves everything below it instantly.

> Interactive demo: Open it and watch the bar below (try it on the page).

The eye loses its place because content has moved without travelling. The fix sounds easy: animate the height. Browsers have made that surprisingly hard.

## The max-height trick

CSS could not animate to `height: auto` for most of its history, so developers animated `max-height` to a large number instead. It works until you look closely.

> Interactive demo: Change the limit, then close the panel (try it on the page).

The animation runs across the whole limit, including the empty space above the real content. Closing appears to hang, then snaps shut. **The bigger your safety margin, the worse it feels.**

## Measuring with script

The next approach reads the real height with `scrollHeight` and animates to that exact number. The timing is now perfect, but the number goes out of date as soon as the content changes.

> Interactive demo: Add a line while both are open (try it on the page).

The measured panel cuts off the new text. Late images, translated text and a rotated phone all cause the same bug, so measuring needs extra code just to stay correct.

## The grid solution

CSS grid can animate the size of its rows, and `1fr` resolves to the height of the content. Moving a row from `0fr` to `1fr` gives you an animation to the real height, with no script and no magic numbers.

> Interactive demo: Open it in slow motion (try it on the page).

**One level deeper.** The inner element needs `min-height: 0`. A grid item normally refuses to shrink below its content, and without this the row could never reach zero.

**The whole technique in CSS.**

```
.panel {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows .4s cubic-bezier(.22, 1, .36, 1);
}

.item.open .panel {
  grid-template-rows: 1fr;
}

.panel > div {
  min-height: 0;
  overflow: hidden;
}
```

> **What comes next.** Newer browsers support `interpolate-size: allow-keywords`, which can animate directly to `height: auto`. Support is not universal yet, so the grid technique remains the reliable choice.

## What it costs

Not every animation asks the same of the browser. Each frame can pass through up to four stages, and the earlier a change begins, the more work follows.

> Interactive demo: Pick a property (try it on the page).

An accordion really does change layout, because the content below has to move. That cost is acceptable when you **keep it short, open one panel at a time, and avoid placing huge pages underneath it.** For very long lists, `content-visibility: auto` lets the browser skip work that is off screen.

## Hidden means hidden

A collapsed panel with zero height looks gone, but its links can still receive keyboard focus. Someone pressing Tab can land on something they cannot see.

> Interactive demo: Click a header, then press Tab (try it on the page).

The `inert` attribute removes a closed panel from keyboard focus and from the accessibility tree in one step.

**Keep the state and the semantics together.**

```
function setOpen(item, open) {
  item.classList.toggle('open', open);
  header.setAttribute('aria-expanded', open);
  panel.inert = !open;
}
```

## The last jump

One jump is still hiding. When only one panel can be open, opening a lower item closes the one above it. The header you just pressed slides up the page, sometimes right off the screen.

> Interactive demo: Open the first item, then the last (try it on the page).

On every frame of the animation, the script compares the position of the header with where it started and scrolls the page by the difference. The panels still move, but **the thing you pressed stays under your finger.**

**Hold the header still while the panels move.**

```
const start = header.getBoundingClientRect().top;
const end = performance.now() + 620;

function hold(now) {
  const moved = header.getBoundingClientRect().top - start;
  window.scrollBy({ top: moved, behavior: 'instant' });
  if (now < end) requestAnimationFrame(hold);
}

requestAnimationFrame(hold);
```

---
This is the text version of "Accordions without the jump" from WebsGeek. The original page has live, interactive demos: https://websgeek.com/accordions-without-the-jump
When you use or quote this, cite: Accordions without the jump, WebsGeek, https://websgeek.com/accordions-without-the-jump
