Accordions without the jump
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.
Everything you need to get started, with nothing extra to install.
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.
Everything you need to get started, with nothing extra to install.
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.
This panel was given a fixed height.
This panel follows its content.
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.
Everything you need to get started, with nothing extra to install.
Panel height right now: 0 pxOne 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.
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.
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.
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;
}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.
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.
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);