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

Layout Animations with layout Prop

Enable automatic layout animations by adding layout prop to motion components that change position or size.

Layout Animations with layout Prop

You'll learn how to automatically animate position and size changes by simply adding a layout prop to motion components.

The Magic of Layout Animations

Framer Motion can automatically animate elements when their position or size changes in the DOM—no manual calculation required. When you add the layout prop to a motion component, Framer Motion tracks the element's bounding box and smoothly animates any changes caused by CSS, flex/grid reordering, or conditional rendering.

Think of it like having a personal assistant who watches where your furniture is, and whenever you rearrange the room, they smoothly slide each piece to its new spot instead of teleporting it.

How It Works

import { motion } from 'framer-motion';
import { useState } from 'react';

function ExpandingCard() {
  const [isExpanded, setIsExpanded] = useState(false);

  return (
    <motion.div
      layout
      onClick={() => setIsExpanded(!isExpanded)}
      style={{
        width: isExpanded ? 400 : 200,
        height: isExpanded ? 300 : 100,
        background: 'lightblue'
      }}
    />
  );
}

The layout prop handles width, height, position, and even flex order changes. When isExpanded toggles, the size change animates smoothly—no transition property needed.

Common Use Cases

  • Grid reordering: Items shuffle positions when filtered or sorted
  • Expanding cards: Click to expand, and both size and sibling positions animate
  • Responsive layouts: Breakpoint changes trigger smooth repositioning
  • List animations: Items move naturally when others are added/removed

Key Takeaway: Adding layout to a motion component automatically animates any changes to its position or size in the layout—it's Framer Motion's most powerful feature for effortless, production-quality layout transitions.