← Back to HTML & CSS, page 9

html-css-0167 · Hard senior

What is the FLIP technique, and how does it animate a layout change cheaply?

FLIP measures the first position, makes the change, measures the last position, inverts with a transform so nothing seems to move, and plays the transform back to none. Layout runs once, and the animation changes only transform.

Full answer

Some changes cannot be animated with CSS because they are layout changes: reordering a list, moving an item to another container, or changing a grid. If you animate top, left or width, the browser must redo layout on every frame, which is slow. FLIP lets you animate such changes with transform, which is cheap.

FLIP stands for First, Last, Invert and Play.

  1. First: measure the element's starting box with getBoundingClientRect().
  2. Last: make the change in the DOM or in the classes, so the element is in its final place. Measure it again.
  3. Invert: compute the difference between the two boxes, and apply a transform that moves the element back to where it was. At this point it looks as if nothing has moved.
  4. Play: animate the transform to none. The element appears to travel from the old place to the new one.

The real layout has already changed at the start, so the work for layout is done once. The animation only changes transform, which the browser can draw on the compositor, often without redoing layout or paint on each frame.

Tips:

  • Use the Web Animations API (el.animate()), which gives a promise when the animation finishes, and avoids a forced style flush.
  • Read all positions first and write after, to avoid layout thrashing.
  • Scale changes can distort content, so animate size with care or use a separate approach.
  • Keep the time short and respect prefers-reduced-motion.
  • The View Transitions API does something similar for the browser, with less code, but it is newer, so check the support table.

After the call, the DOM order is B, C, A at once. Item A starts at the top and moves to the bottom, and B and C move up, all by transform. Each moving item has one running animation, and when it ends, nothing is left in the style.

Measure in the performance panel to see that layout runs once, and not once per frame.

html
<style>
  ul { list-style: none; margin: 0; padding: 0; width: 160px; font: 16px sans-serif; }
  li { padding: 8px; margin-bottom: 4px; background: #cde; }
</style>
<ul id="list"><li id="a">A</li><li id="b">B</li><li id="c">C</li></ul>
<script>
  function flip(change) {
    const items = [...list.children];
    const first = new Map(items.map(el => [el, el.getBoundingClientRect()]));
    change();
    items.forEach(el => {
      const last = el.getBoundingClientRect();
      const dy = first.get(el).top - last.top;
      if (dy) el.animate([{ transform: `translateY(${dy}px)` }, { transform: "none" }], { duration: 200, easing: "ease-out" });
    });
  }
  window.moveFirstToEnd = () => flip(() => list.append(list.firstElementChild));
</script>

Checked in Chrome 156 only. The check counted the running animations and the final order, not how smooth the motion looks. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • First, Last, Invert, Play.
  • Layout changes once; the animation uses transform.
  • Web Animations API gives control and a finished promise.
  • Read all positions before writing.
  • View Transitions can do similar work with less code.

Common mistakes

  • Animating top, left or width for a reorder and causing layout on every frame.
  • Reading and writing positions in turns, which forces layout many times.
  • Skipping a reduced motion rule.

Follow-ups

  • Why use getBoundingClientRect before and after the change?
  • How does the View Transitions API relate to FLIP?

Tips

  • Spell out First, Last, Invert, Play in one sentence each.
  • Say why transform is cheaper and that layout is done once.