Sort Mrs. Sri's room list cheapest first, and the Attic Room jumps to the first row in a single frame. The browser calculates the position and size of every element in its layout step. A change of position or size that comes from layout like this needs a layout animation. Examples are a card that moves to another row because the list was sorted, or a card that grows because its details were opened.
The trick is like swapping the guests' name cards on a wedding reception table:
- Note where the card was (First).
- Move the card straight to its new place, then note its new position (Last).
- Push the card back by hand until it looks like it is still in the old spot (Invert).
- Let it go slowly into its new place (Play).
The guests see the card glide, although the move itself was instant. That is the FLIP technique: First, Last, Invert, Play. On the page, the name card is a room card, and the hand that pushes it is a transform.
Why a transition can't do it
transition only animates CSS values that change. When the cards are re-sorted, no property changes. The element only moves in the DOM order, and layout calculates its new place. There is nothing to interpolate, so the card jumps in a single frame.
Animating top or height on every frame does work, but it is heavy. In lesson 5 (Transform & Composite), a card rising through top made layout compute 35 to 37 times in 300 ms. Through transform, only once. FLIP takes the second way: let layout jump once, then cover that jump with transform.
FLIP by hand
The three cards in the Lab are 56 px tall with 8 px between them, so the rows are 64 px apart. The original order is Terrace ($35), Attic ($27.50), Garden ($42.50). The flip function below takes the list of cards and a function that changes the DOM, here sortCheapestFirst:
function flip(cards, changeDom) {
const first = new Map(cards.map((el) => [el, el.getBoundingClientRect()])); // F: read everything first
changeDom(); // move them in the DOM
for (const el of cards) {
const f = first.get(el);
const l = el.getBoundingClientRect(); // L: the new position
const dy = f.top - l.top; // I: First - Last
if (dy === 0) continue; // did not move
el.animate(
[{ transform: `translateY(${dy}px)` }, { transform: 'none' }], // P: let go
{ duration: 300, easing: 'ease-in-out' }
);
}
}
flip([...list.children], () => sortCheapestFirst());
- First:
getBoundingClientRect()reads each card's box (its position and size) before anything changes. Read them all first, then change the DOM. Switching between reading and writing forces layout again and again (covered in the Performance & Best Practices lesson). - Last: after the DOM changes, read again. The browser computes layout once here, but draws nothing yet.
- Invert: the difference, First minus Last, is set as a
transformso the card looks like it is still in its old place. - Play:
el.animate()releases that transform tonone, so the card glides to its new place. Because it is onlytransform, the animation can run on the compositor thread.
Before you look at the table, take a guess: the Attic Room ($27.50) moves up 64 px from the second row to the first. Right after sorting, before the first frame is drawn, which transform does FLIP put on the Attic card: translateY(-64px), translateY(64px), or none? This table is what the Lab measures when sorting cheapest first. First and Last are the distance of the card's top edge (top) from the top of the list, in px:
| Card | First (top) | Last (top) | Invert |
|---|---|---|---|
| Attic | 64 | 0 | translateY(64px) |
| Terrace | 0 | 64 | translateY(-64px) |
| Garden | 128 | 128 | none, not animated |
The Attic card gets translateY(64px), a positive one: First minus Last, 64 - 0 = 64. The Attic card is already in the first row, and a positive transform pushes it back down to its old place, then lets go.
Size: scale, and children that get squashed
Now the Terrace Room details open: the card grows from 56 to 112 px, and the two cards below move down 56 px. For size, the Invert uses scale: the old height divided by the new one, 56 / 112 = scaleY(0.5). transform-origin (the point the transform starts from) is set to the top left, so the card's top edge stays in place. The cards below only need translateY(-56px).
The problem is that scale also affects the card's content. On the first frame the text "Terrace Room" is squashed to half its height, then slowly returns to normal. The 12 px rounded corners get squashed too. The fix: give the content the inverse scale, 1 / 0.5 = scaleY(2). The result is 0.5 × 2 = 1, so the content looks normal.
But that inverse scale must be recomputed on every frame. If the content gets just two keyframes (scale 2 to 1), then halfway through, the card's scale is 0.75 and the content's is 1.5. The result is 0.75 × 1.5 = 1.125, so the text is 12.5% too tall. The Lab splits the animation into 20 steps and computes the correction at each step. Motion computes it on every frame.
For an accordion (a panel that opens and closes), CSS can animate the height without FLIP. height: 0 to auto usually jumps, because auto is not a number. The three ways in this table don't jump: in Chromium 156, all three were still halfway at 150 ms of 300 ms.
| Way | When to pick it |
|---|---|
A display: grid wrapper, grid-template-rows: 0fr to 1fr, its child overflow: hidden |
the safe default, in every modern browser |
interpolate-size: allow-keywords, then height: 0 to auto |
the shortest code, but only Chrome 129 so far. Other browsers jump |
details::details-content plus interpolate-size |
an accordion made from <details>. Outside Chrome, its content still jumps |
All three compute layout on every frame, so they are fine for one panel, not for a long list.
Motion: the layout prop
In React with Motion (the previous lesson), one prop, layout, does all of those FLIP steps:
<motion.li
layout // automatic FLIP on every render
transition={{ layout: { duration: 0.3, ease: 'easeInOut' } }}
style={{ borderRadius: 12 }} // written in style so it gets corrected too
className="room"
>
<motion.div layout className="content"> {/* the child joins layout: its scale is corrected */}
<b>Terrace Room</b> <span>$35 / night</span>
{isOpen && <p>Free cancellation up to 3 days before check-in.</p>}
</motion.div>
</motion.li>
layoutmakes Motion measure the element's position and size before and after every render (when React rebuilds a component's view, for example after its React state changes). Then Motion animates the difference withtransform. So a layout animation is an automatic animation when an element's position or size changes because layout was recomputed (a reflow).- In
<motion.div layout>{isExpanded ? <FullContent /> : <Preview />}</motion.div>, the div shows either<Preview />(a summary) or<FullContent />(the full content). WhenisExpandedturnstrue, the div animates its change of size smoothly, with noinitialoranimate. - A child that also has
layoutgets its scale corrected by Motion. Withoutlayouton the child, the child is squashed just like FLIP without the correction.borderRadiusandboxShadoware corrected when written instyle. layout="position"only animates position. The size jumps, so the text is never squashed.layout="size"is the opposite.
layoutId: one photo, two places
The "Morning terrace" thumbnail (a small photo) on the card and the large photo above the list are two different elements. Give both the same layoutId, and show only one of them:
{!photo && <motion.button layoutId="terrace-photo" className="thumb" onClick={() => setPhoto(true)} />}
{photo && <motion.button layoutId="terrace-photo" className="photo" onClick={() => setPhoto(false)}>Morning terrace</motion.button>}
A click on the thumbnail sets photo to true: the thumbnail disappears and the large photo appears in the same render. Motion then animates the large photo from the thumbnail's box to its new one. If several components move each other's layout but don't re-render together, wrap them in <LayoutGroup> so Motion measures them all together.
Without a library: view transitions
The browser has its own version: the view transition from lesson 9 (View Transitions API). Give each card its own view-transition-name, then change the DOM inside startViewTransition:
.room[data-id="terrace"] { view-transition-name: terrace-room; }
.room[data-id="attic"] { view-transition-name: attic-room; }
.room[data-id="garden"] { view-transition-name: garden-room; }
.thumb, .photo { view-transition-name: terrace-photo; } /* never present at the same time */
if (document.startViewTransition) document.startViewTransition(() => sortCheapestFirst());
else sortCheapestFirst();
The browser captures the page before and after, then moves and resizes each name's group. Names must be unique. Two cards with the same name cancel the whole transition. Supported in Chrome 111, Safari 18, Firefox 144. In React, change the state with flushSync (a React function that applies a state change to the DOM right away) inside the callback.
Since React 19.3 (September 2026), there is a <ViewTransition> component. Change the state inside startTransition, and React itself calls startViewTransition and names each card:
import { ViewTransition, startTransition, useState } from 'react';
function RoomList({ initialRooms }) {
const [rooms, setRooms] = useState(initialRooms);
function sortCheapestFirst() {
startTransition(() => { // without this, no animation
setRooms((old) => [...old].sort((a, b) => a.price - b.price));
});
}
return (
<>
<button onClick={sortCheapestFirst}>Cheapest first</button>
{rooms.map((r) => (
<ViewTransition key={r.id}>
<div className="room">{r.name}</div>
</ViewTransition>
))}
</>
);
}
useState keeps the room list as the React state rooms, and setRooms replaces it with the sorted list. In Chromium 156 with React 19.3.1, the cards slid. Without startTransition, the order changed with no animation. On React 19.2 and older, keep using flushSync.
⚠️ Why does the card flicker, squash, or lag?
- Reading Last too early or too late. In React, measure Last in
useLayoutEffect, a hook that runs after the DOM changes and before the browser draws.useEffectcan run after the browser draws, so the card shows for one frame in its new place and then jumps back. - Switching between reading and writing. Read every First, then change the DOM. If each card is read and then written right away, layout is forced over and over.
- Squashed text.
scalealso affects the card's content. Correct the children (layouton the child), or uselayout="position". - FLIP under "Reduce motion". Skip the animation and let the cards move right away, or swap it for a short fade.
TL;DR: A layout change (the position and size the browser calculates), such as a sorted list of cards or a card's opened details, has no CSS value to transition. Animating top or height computes layout on every frame. The FLIP technique: read First (getBoundingClientRect, each card's box), change the DOM, read Last, set the Invert (translateY(First - Last), scaleY(old height / new height)) so the card looks like it is still in its old place, then Play to none. In the Lab, the room cards are sorted cheapest first. The Attic card moves up from 64 to 0 px and gets translateY(64px), the Terrace card moves down and gets translateY(-64px), and the Garden card does not move. scale squashes the card's content: the Terrace card, growing from 56 to 112 px, starts at scaleY(0.5). Its content needs the inverse scale (2) on every frame, because a correction with two keyframes is 12.5% off halfway through. In Motion, the layout prop does the FLIP on every render (when React rebuilds the view). Children with layout are corrected too, and layout="position" animates position only. The same layoutId links two different elements (thumbnail to large photo). Without a library: startViewTransition with a unique view-transition-name per card.
⚡ No CSS value changes
Meaning: When a list of cards is re-sorted, the cards only move in the DOM order. No CSS property changes, so transition has nothing to interpolate, and the card jumps to its new place. The FLIP technique (First, Last, Invert, Play) lets layout jump once, then covers that jump with transform.
⚡ First, Last, Invert, Play
Guess first: the Attic Room moves up 64 px from the second row to the first. Right after sorting, FLIP sets a transform that makes its card look like it is still in the old place. translateY(64px) or translateY(-64px)?
translateY(64px), a positive one. The card is already in the first row, and a positive transform pushes it back down to its old place, then lets go.
Meaning: Read each card's box (its position and size) with getBoundingClientRect() (First), change the DOM, then read again (Last). Set First minus Last as a transform (Invert), then release that transform to none (Play).
⚡ The layout prop in Motion
Write:
<motion.li
layout // automatic FLIP on every render
transition={{ layout: { duration: 0.3, ease: 'easeInOut' } }}
style={{ borderRadius: 12 }} // written in style so it gets corrected too
className="room"
>
<motion.div layout className="content"> {/* the child joins layout: its scale is corrected */}
<b>Terrace Room</b> <span>$35 / night</span>
{isOpen && <p>Free cancellation up to 3 days before check-in.</p>}
</motion.div>
</motion.li>
Meaning: The layout prop in Motion does the FLIP for you. Motion measures position and size before and after every render (when React rebuilds the view), then animates the difference with transform. So when isOpen is true and the details paragraph appears, the card grows smoothly. For size, FLIP uses scale, which usually squashes the card's content too. But a child that also has layout gets its scale corrected, so the text isn't squashed.
⚡ One photo, two places
Meaning: The thumbnail (a small photo) on the card and the large photo are two different elements. Give both the same layoutId and show only one of them. Motion then animates the large photo from the thumbnail's box to its new one.