RSS FeedTwitterMastodonBlueskyShare IconHeart IconGithub IconArrow IconClock IconGUI Challenges IconHome IconNote IconBlog IconCSS IconJS IconHTML IconShows IconOpen Source Software IconSpeaking IconTools IconShuffle IconNext IconPrevious IconCalendar IconCalendar Edit IconNotebook IconObservable Notebooks IconSlash IconGoogle G IconYouTube IconEye Icon
A ditto pokemon holding an anchor on an island
A cartoon skull with a hotpink hat on.7 min read

Anchor Interpolated Morph (AIM)

css
514 views · 24 active

It's more natural when animations start near the trigger point, this CSS technique makes it easy. AIM animates from any element, and to any element.

Thanks to anchor(), anchor-size(), @starting-style, and interpolate-size.

All together they can create an anchor interpolated morph: an interruptible contextual transition from where a user invoked it.

That's a popover=hint + interestfor,
no JS CSS transition. Try it

This is a very powerful and capable feature combination.

Similar results are visually possible with child elements using their parent as the anchor, but:

  1. What if they're not ancestors?
  2. What if one is in the :top-layer?
  3. What if you want semantic HTML and a healthy accessibility OM?

AIM is the remedy.

FLIP #

This technique is inspired by Paul Lewis's coined animation technique called FLIP (First, Last, Invert, Play).

The gist of FLIP is to use JS to calculate the difference in positions/size/whatever of an element, before and after some DOM change.

AIM #

The gist of AIM is to use CSS to access properties from where another element is (first), to where natural render is (last), and let transition handle invert and play.

First: anchor(), anchor-size() & @starting-style
Last: interpolate-size: allow-keywords & auto

The roles where JS was accessing properties, measuring natural sizes, and calculating the differences… are now handled by CSS. Definitely not 100% like FLIP 😅 but hopefully you see the connection.

Here's a less designed example (so there's less CSS to sift through) that's a <dialog> element:

Try it. You can uncomment a top-left position example too!

How it works #

1: The anchor-name property is used to identify the element that the element is morphing from.

button {
  anchor-name: --⚓︎-morph; 
}

Can you believe this one line of CSS enables any other element to observe the height, width, and position of this element 🤯

Want more about that emoji naming convention?

2: Link up the dialog with that anchor.

dialog {
  position-anchor: --⚓︎-morph;

  /* adjust position as desired */
  left: anchor(left);
  top: anchor(top);
}

3: Enable transitioning to auto width/height.

dialog {
  interpolate-size: allow-keywords;
}

4: Enable transitions for all properties you want in the morph.

dialog {
  @media (prefers-reduced-motion: no-preference) {
    transition:
      display var(--_speed) allow-discrete,
      overlay var(--_speed) allow-discrete,
      height  var(--_speed) var(--ease-3),
      width   var(--_speed) var(--ease-3),
      left    var(--_speed) var(--ease-3),
      top     var(--_speed) var(--ease-3);
  }
}

5: Now we can use @starting-style to apply the styles from the anchor to the dialog before it opens. I call this "enter stage from" styles, and you can apply as many of the anchor values as you want.

I specify all corners here and setup a mask system to allow the dialog to transition to fit it's content, from the anchor size, without distorting the dialog contents.

dialog {
  @starting-style {
    &[open] {
      left: anchor(left);
      top: anchor(top);
      right: anchor(right);
      bottom: anchor(bottom);
      width: anchor-size(width);
      height: anchor-size(height);
    }
  }
}

Notice anchor-size() for height and width, this combined with height and width auto and interpolate-size: allow-keywords will allow the dialog to transition to its natural size from the anchor with a nice reveal.

And for exit out on the dialog, it's similar as enter stage in my example but you could easily transition a different way or to a different element.

dialog {
  &:not([open]) {    
    left: anchor(left);
    top: anchor(top);
    right: anchor(right);
    bottom: anchor(bottom);
    width: anchor-size(width);
    height: anchor-size(height);
  }
}

That's it for a dialog!

AIM can be used for:

  • Dialogs
  • Popovers
  • Tooltips
  • Page preview
  • Fullscreen takeover (go big!)
  • Quick actions
  • Disclosures
  • Submenus
  • Hovercards
  • Particles? add some CSS random()
  • Other overlay components?

Here's the popover from the video at the beginning. It's using all the above techniques, plus some extras, precise timings, easings and intentionality.

View Transitions #

What about view transitions and morphing, don't they do that too?

Yep! They do, and can do some neat morphing tricks. Here's a view transitions demo I called "anything to anything".

But… that requires JS, is always a straight line and is not elegantly interruptible.

"always straight lines"… once you notice it you can't unsee it. View transitions can't curve or swing to a new destination.

Here's some slides I made using the Astro Morphull repo I made that use no JS view transitions so content morphs: https://cascadiajs-2025.netlify.app.

Spatial Continuity #

Traditional transitions often appear out of thin air or fade in from a generic center point. By leveraging anchor() and @starting-style, we can let CSS tell a story about where an element came from.

We're telling a story about where an element came from and why it’s there.

Go forth and make more natural transitions that feel spatially contextual. Stop letting your elements "teleport" into view, start letting them grow, stretch, and evolve directly from the components that triggered them.

Your users (and their muscle memory) will thank you. Speaking of, don't forget to gate the motion behind a prefers-reduced-motion media query (test the dialog and popovers).

Hot tip: You could easily morph to a different element than the invoking element, anchor() doesnt care.

Summary checklist for AIM:

  1. Set the Anchor:
    anchor-name on the invoking element to create a spatial reference.
  2. Set "Enter From" and "Exit To" styles with anchor():
    Use @starting-style to declare the entry and exit properties relative to the anchor.
  3. Transition:
    Orchestrate transition properties to allow the browser to interpolate the path, shapes and colors between the anchor point and the final layout.

4 comments #

48likes
8reposts
  • dOSKEL D. DOSKINGTON
  • Thibaultmol ???? ???? FOSDEM
  • Javi Aguilar
  • Brecht De Ruyte
  • Penny
  • Matthias Ott
  • Toby Evans
  • Anthony Frehner

Join the conversation on

I love the ditto visual ????
Brecht De RuyteBrecht De Ruyte
That’s pretty cool, I might have to mess around with that later now that I’ve been experimenting with dev again. I’m curious though too what you use to record the interaction and make your gifs with the keystroke annotations. I had been looking for a new tool and was thinking of Gifox.
TylerTyler

@argyleink I definitely just happened to hover over the button just as the GIF started playing (I think it only plays while hovering on desktop) and thought you had somehow put a Codepen in a Mastodon post ????

ChrisChris