Course contentsShow
← React
Lesson 1986 of 2,39645. AnimationsFree lesson

layoutId for Element Transitions

Master layoutId to animate elements that appear to move between different parts of your component tree smoothly.

layoutId for Element Transitions

You'll learn how to make an element appear to seamlessly morph between different positions in your component tree using layoutId.

The Core Idea

Sometimes you want the same visual element to appear in two different places—think of a product thumbnail that "transforms" into a full-screen detail view, or a list item that expands into a modal. With layoutId, Framer Motion can detect that two separate DOM elements represent the same conceptual item and automatically animate the transition between them.

When you give multiple motion components the same layoutId, Framer Motion treats them as the same element moving through space. As one unmounts and another mounts, it smoothly interpolates position, size, and other properties—creating the illusion of a single morphing element.

How It Works

function Gallery() {
  const [selected, setSelected] = useState<string | null>(null);

  return (
    <div>
      {items.map(item => (
        !selected && (
          <motion.div
            key={item.id}
            layoutId={item.id}
            onClick={() => setSelected(item.id)}
          >
            {item.thumbnail}
          </motion.div>
        )
      ))}

      <AnimatePresence>
        {selected && (
          <motion.div
            layoutId={selected}
            onClick={() => setSelected(null)}
          >
            {/* Full-size detail view */}
          </motion.div>
        )}
      </AnimatePresence>
    </div>
  );
}

When you click a thumbnail, the layoutId matches between the disappearing thumbnail and the appearing detail view. Framer Motion calculates the difference in position/scale and animates between them—no manual coordinate math needed.

The Analogy

Think of layoutId as a name tag. When two actors wear the same name tag on different sets, the director knows they're playing the same character and can film a seamless scene transition.

Key Takeaway: Use layoutId with matching strings on different motion components to create smooth element transitions between different parts of your UI, making them appear as one morphing element.