Anchor positioning

Tether an element to another one, wherever it ends up

Your browser does not support CSS anchor positioning, so the tiles below sit in their cells as plain grid content and jump instead of gliding.

Animating a grid layout

A grid cannot transition its items from one track to another. Anchor positioning can: every cell is an anchor, and its tile is absolutely positioned onto it with inset: anchor(inside). When the grid reflows, the resolved insets change, and a plain transition carries the tile to its new cell. Drag the resize handle of the dashed box, or switch the tile size (technique by Bramus).

1
2
3
4
5
6
7
8
9
10
11
12
markup.html
<div class="grid">
  <div class="cell"><div class="content">1</div></div>
  <div class="cell"><div class="content">2</div></div>
  …
</div>
styles.css
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, 6rem);
    gap: 1rem;
}

.cell {
    aspect-ratio: 1;
    /* every cell reuses --a; the scope keeps each one to its own subtree */
    anchor-scope: --a;
    anchor-name: --a;
}

.content {
    position: absolute;
    position-anchor: --a;
    inset: anchor(inside);
    /* explicit size, or the tile stretches while it travels */
    width: 6rem;
    height: 6rem;
    transition: inset 0.3s ease;
}

Gotchas