Accordions without the jump

Animating height so nothing around it lurches.

GuideLayout
01

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.

Open it and watch the bar below Interactive

Everything you need to get started, with nothing extra to install.

Everything below teleports

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

02

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.

Change the limit, then close the panel Interactive

Everything you need to get started, with nothing extra to install.

Content below

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.

03

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.

Add a line while both are open Interactive

This panel was given a fixed height.

fixed pixel height

This panel follows its content.

grid rows

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.

04

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.

Open it in slow motion Interactive

Everything you need to get started, with nothing extra to install.

Everything below travels with it
Slow motion
Panel height right now: 0 px

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.Show code
.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.

05

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.

Pick a property Interactive
Style
Layout
Paint
Composite

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.

06

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.

Click a header, then press Tab Interactive

focus can get lost

focus skips the panel
Press Tab to move through each accordion.

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.Show code
function setOpen(item, open) {
  item.classList.toggle('open', open);
  header.setAttribute('aria-expanded', open);
  panel.inert = !open;
}
07

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.

Open the first item, then the last Interactive

Pick any post, press the previews, and read at your own pace. Each article builds one idea from the ground up, so you can stop whenever you have what you need. There is nothing to install and nothing to sign up for.

Short code examples are there for you to learn from and use in your own projects. Test them carefully before you ship them.

Yes. Every demo responds to touch, mouse and keyboard, and each one adapts to the size of your screen.

While the panels animate, a small script measures how far the header has moved and scrolls the page by the same amount.

Only one open
Keep the header in place

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.Show code
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);

New deep dives, in your inbox.