Two Tools for Two Different Jobs
CSS has two separate systems for motion. Transitions smoothly animate a property between two states — usually triggered by :hover, :focus, or a class change. Animations use @keyframes to define a full motion sequence that can run automatically, repeat, and pass through several steps.
- ✓Use a transition when something changes because of user interaction — a hover, a click, a toggle.
- ✓Use an animation for anything that runs on its own — a loading spinner, a pulsing badge, an entrance effect.
- ✓Both rely on the same easing concepts, but animations offer far more control over the steps in between.
Smoothing a Change Between States
transition tells the browser to animate a property change instead of jumping instantly. It needs a property, a duration, and usually a timing function.
.btn { background: #ffffff; transform: translateY(0); transition: background .2s ease, transform .2s ease; } .btn:hover { background: #e8590c; transform: translateY(-4px); }
Controlling the Pace of Change
The timing function shapes how a transition accelerates and decelerates — not just how long it takes.
Constant speed throughout — often feels mechanical.
Starts fast, slows toward the end — the default, and usually the safest choice.
Slow start and slow end, faster in the middle — feels smooth and deliberate.
A fully custom curve — used for effects like overshoot or bounce.
Lifting a Card on Hover
One of the most common uses of transition: a card that lifts and gains a soft shadow on hover, using transform and box-shadow together.
.card { box-shadow: 0 4px 10px -6px rgba(0,0,0,0.15); transition: transform .2s ease, box-shadow .2s ease; } .card:hover { transform: translateY(-6px); box-shadow: 0 16px 26px -14px rgba(232,89,12,0.35); }
Course Card
Hover to lift.
Defining a Motion Sequence
@keyframes names a sequence of steps from 0% to 100% (or from/to). The animation property then applies that sequence to an element with a duration, timing function, and repeat behavior.
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.12); } } .dot { animation: pulse 1.4s ease-in-out infinite; }
The Pieces Behind animation
How long one cycle of the animation takes.
The easing curve — same values as transition.
How many times it repeats — a number, or infinite.
Whether the element keeps the last keyframe's styles after finishing (both/forwards).
More Isn't Better
Breaking Down What You Just Learned
Transitions vs animationsTransitions animate a state change; animations run self-contained sequences via @keyframes.
transition propertyNeeds a property, duration, and timing function to animate a change smoothly.
Timing functionslinear, ease, ease-in-out, and cubic-bezier() all shape the pace of motion differently.
@keyframesDefines named steps from 0% to 100%; animation applies them with duration and repeat behavior.
Use sparinglyMotion should support feedback and small accents, not run on everything at once.
Try It Yourself
Add a transition to every button and card on your Day 15 project — background and transform on hover — then build one @keyframes loading spinner and drop it into a "loading" section.
Add Motion to a Small Interface
Practicing transitions and keyframe animations together
Create day27.html linked to a stylesheet and build the following on the page.
- Build a button with a transition on background and transform, triggered by :hover.
- Build a card that lifts with translateY and gains a stronger box-shadow on hover.
- Write an @keyframes pulse animation and apply it to a small badge or dot, set to infinite.
- Write an @keyframes spin animation and build a loading spinner from a bordered circle.
- Build a fade-in @keyframes animation that runs once on page load for a heading or card.
Recap
Animate a property change between two states, usually triggered by interaction.
linear, ease, ease-in-out, and cubic-bezier() shape the pace of motion.
Defines a named sequence of steps that animation then applies to an element.
Motion should support feedback and small accents, not run everywhere at once.
Key Takeaways
- transition smoothly animates a property change, typically triggered by :hover, :focus, or a class toggle.
- Timing functions like ease and ease-in-out shape how a transition accelerates and decelerates.
- @keyframes defines named steps from 0% to 100%, which the animation property then plays on an element.
- animation-iteration-count: infinite repeats an animation forever — useful for spinners and pulsing indicators.
- Motion works best in moderation — reserved for feedback and small purposeful accents, not applied everywhere.