Skip to content
Founding Lifetime: every Pro skillfirst 100 members · $169 onceUpgrade
Lookbk
Sign In
Effect guide

Layout animation: animate elements when their position or size changes

Layout animation makes an element glide from its old place to its new one when the DOM changes, instead of jumping. You measure before, change the layout, measure after, then animate the difference.

Free skills to start. Pro is $79 a year for every skill, or $9.99 a month for 10. See pricing

What is a layout animation?

A normal CSS transition cannot do this. When you reorder a list, add a card or switch a grid to a different layout, each element lands in its new spot on the next frame. There is no value to interpolate, because you never set a starting position. Layout animation fills that gap: it records where each element was, lets the browser apply the new layout, then plays the move from the old box to the new one.

There are two common routes. The first is FLIP (First, Last, Invert, Play), which you write yourself with getBoundingClientRect() and Element.animate(). The second is the View Transition API, where the browser takes the snapshots for you. Both end up animating transforms and sizes, not the layout properties themselves.

The View Transition API has one rule that catches people out. Each element that takes part needs its own view-transition-name. The spec says that if two elements share the same name when the old state is captured, the transition is skipped with an InvalidStateError, and a repeat in the new state makes capture fail too. The update callback still runs, so the DOM changes, but nothing animates. In a list, give every item a unique name, for example one built from its id.

How the effect works

FLIP starts by reading. getBoundingClientRect() returns a DOMRect with the element's size and its position relative to the viewport. You read it for every item before the change and again after. The gap between the two rectangles is how far the item has to travel.

Then you play the move. Element.animate() takes keyframes and a duration in milliseconds, and returns an Animation. Start each item with a transform that places it back where it was, and animate to no transform. The element is already in its final layout, so the animation is only a visual replay.

The View Transition API does the same capture-and-replay for you. document.startViewTransition() takes a snapshot of the current page, calls your update callback, and when the promise it returns fulfills, the transition begins in the next frame. For each named element the spec's default group animation moves from the old size and position to the new, and the default animation-duration set by the browser's stylesheet is 0.25s.

  1. 01

    Measure the first state

    Cancel any running animations on the items, then store each item's getBoundingClientRect() in a Map keyed by element.

  2. 02

    Change the layout

    Reorder, add or remove elements, or switch a class. Do it in plain DOM code, with no animation yet.

  3. 03

    Measure the last state and invert

    Read each rectangle again. Compute the difference in left and top, and in width and height if the size changes.

  4. 04

    Play

    Call element.animate() from the inverted transform to none. Skip the whole step when prefers-reduced-motion is set to reduce.

HTML

FLIP layout animation for a shuffling grid

Plain JavaScript, no library. Shuffle reorders the tiles, Add inserts one, Remove drops the first. Each tile slides from its old box to its new one. With reduced motion turned on, tiles jump with no animation.

<!doctype html>
<html><head><meta charset="utf-8"><title>Layout animation</title>
<style>
  body { margin: 0; font: 16px system-ui, sans-serif; background: #12141a; color: #f3f4f6; padding: 32px; }
  .bar { display: flex; gap: 8px; margin-bottom: 24px; }
  button { font: inherit; padding: 8px 16px; border: 0; border-radius: 8px; background: #6ee7b7; color: #0b0d11; cursor: pointer; }
  #grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; max-width: 800px; }
  .tile { height: 110px; border-radius: 14px; display: grid; place-items: center; font-size: 28px; font-weight: 700; color: #0b0d11; }
</style></head>
<body>
<div class="bar">
  <button id="shuffle">Shuffle</button>
  <button id="add">Add</button>
  <button id="remove">Remove</button>
</div>
<div id="grid"></div>
<script>
const grid = document.getElementById('grid');
const reduce = matchMedia('(prefers-reduced-motion: reduce)');
let n = 0;

function addTile() {
  const t = document.createElement('div');
  t.className = 'tile';
  t.textContent = ++n;
  t.style.background = 'hsl(' + (n * 47 % 360) + ' 75% 70%)';
  grid.appendChild(t);
}
for (let i = 0; i < 8; i++) addTile();

// First, Last, Invert, Play around any DOM change
function flip(change) {
  const tiles = () => [...grid.children];
  tiles().forEach(t => t.getAnimations().forEach(a => a.cancel()));
  const first = new Map(tiles().map(t => [t, t.getBoundingClientRect()]));
  change();
  if (reduce.matches) return;
  tiles().forEach(t => {
    const a = first.get(t);
    const b = t.getBoundingClientRect();
    if (!a) {
      t.animate([{ opacity: 0, transform: 'scale(0.6)' }, { opacity: 1, transform: 'none' }], { duration: 300, easing: 'ease-out' });
      return;
    }
    const dx = a.left - b.left, dy = a.top - b.top;
    const sx = a.width / b.width, sy = a.height / b.height;
    if (!dx && !dy && sx === 1 && sy === 1) return;
    t.animate([
      { transformOrigin: 'top left', transform: 'translate(' + dx + 'px,' + dy + 'px) scale(' + sx + ',' + sy + ')' },
      { transformOrigin: 'top left', transform: 'none' }
    ], { duration: 400, easing: 'cubic-bezier(.65,0,.25,1)' });
  });
}

document.getElementById('shuffle').onclick = () => flip(() => {
  const list = [...grid.children];
  for (let i = list.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [list[i], list[j]] = [list[j], list[i]];
  }
  list.forEach(t => grid.appendChild(t));
});
document.getElementById('add').onclick = () => flip(() => {
  addTile();
  grid.insertBefore(grid.lastChild, grid.children[1]);
});
document.getElementById('remove').onclick = () => flip(() => {
  if (grid.firstChild) grid.removeChild(grid.firstChild);
});
</script>
</body></html>

A starting point written for this page, free to use in your own projects. The skills below build the finished version with checks, fallbacks and a recorded video.

What to get right on a real page

  1. 01

    Give every View Transition element a unique name

    A repeated view-transition-name makes the transition skip, while your DOM update still runs. When you render a list, build the name from each item's id, never from a shared class.

  2. 02

    Cancel running animations before you measure

    Element.getAnimations() lists the animations on an element. Cancel them first so the first rectangle is the true resting position and not a mid-move frame.

  3. 03

    Read all rectangles, then write

    Collect every getBoundingClientRect() before you touch the DOM, and every one after. Mixing reads and writes per item makes the browser recompute layout repeatedly.

  4. 04

    Honor reduced motion

    The reduce value of prefers-reduced-motion means the user turned on a reduced-motion setting on their device. Skip the move, or swap it for a short fade.

  5. 05

    Scale the box only when the size changes

    Text inside a scaled element is stretched during the move. If only position changes, use translate alone. If size changes, expect distortion and consider a crossfade for the content.

  6. 06

    Keep the animation short

    The browser's default group animation lasts 0.25s. A layout move longer than a half second starts to feel like waiting, so tune from there.

Skills that build a layout animation

These skills use the same idea, moving shared objects from one arrangement to another, in finished sections you can drop into a site.

  1. Feature Tabs: an example of what it makes
    Free
    web
    Feature TabsClaude CodeCodexCursor+3 more agents

    Page Kits · Free

    Feature Tabs

    A features section built from one JSON config: 3 to 6 tabs beside one stage drawn as real UI, where switching tabs moves the same objects into a new view

    It builds a features section where a tab switch keeps what two views have in common and moves it into place, so each switch looks caused. Shared objects travel in 560 ms with cubic-bezier(.65,0,.25,1), staggered 22 ms in reading order, and bars grow out of the amounts they came from.

    Worth knowing: With reduced motion, nothing travels: a switch replaces the view with a 120 ms fade.

    Needs: Node.js 18+, Chrome (or Chromium/Edge), and ffmpeg for the video

    See what Feature Tabs makes
  2. Phone Screen Showcase: an example of what it makes
    Free
    web
    Phone Screen ShowcaseClaude CodeCodexCursor+3 more agents

    Page Kits · Free

    Phone Screen Showcase

    One phone, 3 to 5 app screens, tap by tap, scroll or timed, HTML and CSS phone, light and dark examples

    It builds an app landing hero around one phone that walks through 3 to 5 screens, where the next screen arrives as a push, a rising sheet or a tab change. The motion uses transforms and opacity only, so it can be scrubbed both ways with the scroll.

    Worth knowing: It tells one task in 3 to 5 screens, so it is a walkthrough, not a general list reorder tool.

    Needs: Node.js 18+, Chrome (or Chromium/Edge), and ffmpeg for the video

    See what Phone Screen Showcase makes

Install

Build a layout animation with your agent

Each skill is a SKILL.md folder, so it installs the same way in Claude Code, Codex, Cursor and other agents. The example below uses Feature Tabs, which is free; each skill page has its own prompt.

1. Paste the install prompt

Your agent runs the installer. The first time, it shows a sign-in link so you can approve it in the browser.

Install the "Feature Tabs" skill from Lookbk (https://lookbk.dev/skills/feature-tabs-kit).
1. Run: curl -fsSL https://lookbk.dev/cli.js | node - add feature-tabs-kit
2. When the install finishes, read the SKILL.md it installed and tell me what you need from me to make the first result.

Or run it in a terminal

curl -fsSL https://lookbk.dev/cli.js | node - add feature-tabs-kit

2. Start a new session

Agents load skills when a session starts. The installer writes the folder where your agent looks:

Claude Code
~/.claude/skills/feature-tabs-kit
Codex
~/.agents/skills/feature-tabs-kit
Cursor
~/.agents/skills/feature-tabs-kit

3. Ask for the result

Describe it in plain words, for example:

Build our how it works section with the feature tabs kit: four features, each with a claim, a proof number and a stage view of our product made from the same records, light theme

Another agent? Install a skill in any agent

FAQ

Questions

Still stuck? The guides cover installing, agents and licensing in more detail.

Read the guides
What is the difference between FLIP and the View Transition API?

With FLIP you measure the elements and write the animation yourself using getBoundingClientRect() and Element.animate(). With document.startViewTransition() the browser captures the old state, runs your update callback, captures the new state and animates the named elements for you.

Why does my view transition do nothing?

Check for duplicate view-transition-name values. The spec says a name repeated in the old state skips the transition with an InvalidStateError, and a repeat in the new state makes capture fail. The update callback still runs.

What does FLIP stand for?

First, Last, Invert, Play. You record the first rectangle, apply the change and record the last, invert the difference with a transform, then play the animation back to no transform.

What does startViewTransition do with my callback?

MDN says the callback is invoked once the API has taken a snapshot of the current page. When the promise it returns fulfills, the transition begins in the next frame. If the promise rejects, the transition is abandoned.

How do I respect users who want less motion?

Check the prefers-reduced-motion media query. Its reduce value means the user enabled a reduced-motion setting on their device. In that case apply the change with no animation or a short fade.