CodeBlanks Logo Icon CodeBlanks

What is Framer Motion?

Learn the basics of writing animations with Framer Motion.

What is Framer Motion?

Framer Motion is a popular animation library for React. You add animation to JSX with motion components; you get smooth transitions without writing CSS.

Components like motion.div take initial, animate and transition props. This defines the start, end and duration in one place.

Framer Motion uses GPU-accelerated animations; this keeps it performant. You can also define component states with variants.

The motion component

motion.div is like a normal <div> but takes animation props. initial={{ opacity: 0 }} hides it at start, animate={{ opacity: 1 }} makes it visible.

transition={{ duration: 0.3 }} sets the animation duration to 300ms. These three props together give a simple fade-in animation.

Every HTML tag has a motion version: motion.span, motion.button, motion.ul and so on.

Practice Fade-in animation

Complete the skeleton. Write a FadeIn component. Render <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} transition={{ duration: 0.5 }}>Hello</motion.div>. Import motion from 'framer-motion'. Add export default.

Starter code

App.jsx
import { motion } from 'framer-motion';

// Write FadeIn here

Sample solution

App.jsx
import { motion } from 'framer-motion';

function FadeIn() {
  return (
    <motion.div
      initial={{ opacity: 0 }}
      animate={{ opacity: 1 }}
      transition={{ duration: 0.5 }}
    >
      Hello
    </motion.div>
  );
}

export default FadeIn;

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does Framer Motion use?

  • CSS keyframes
  • motion components Correct answer
  • Web Animations API
  • Canvas

Explanation: Motion components.

2 motion component

Question
<____ initial={{ opacity: 0 }} animate={{ opacity: 1 }}>Merhaba</motion.div>
Answer
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }}>Merhaba</motion.div>

Explanation: motion.div is an animated div.

3 What is Framer Motion: match the terms

Term Meaning
initial Starting state
animate Target state
transition Duration/easing
motion.div Animated div

All lessons in Advanced