Small movements make an interface feel responsive: a button that darkens and lifts on hover, a card that grows a shadow, a spinner while data loads. CSS gives you two tools for this without any JavaScript. Transitions smoothly animate a property when its value changes, and keyframe animations run a sequence of steps by themselves, once or forever. This lesson shows both and how to keep motion accessible.
In this lesson you will learn
- The
transitionproperty: property, duration, timing function and delay - Animating with
transform(translate, scale, rotate) - Defining
@keyframesand theanimationproperty - Iteration counts, fill modes and delays
- Respecting
prefers-reduced-motion
Transitions
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Transitions</title>
<style>
body { font-family: sans-serif; margin: 10px; display: flex; gap: 12px; flex-wrap: wrap; }
.btn { background: #1565c0; color: white; border: none; padding: 10px 18px; border-radius: 6px;
font-size: 15px; cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease; }
.btn:hover { background: #0d47a1; transform: translateY(-3px); }
.card { width: 150px; padding: 12px; background: #fff3e0; border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: box-shadow .3s, transform .3s; }
.card:hover { box-shadow: 0 8px 18px rgba(0,0,0,.25); transform: scale(1.05); }
.grow { width: 60px; height: 30px; background: #2e7d32; transition: width 1s ease-in-out; }
.grow:hover { width: 200px; }
</style>
</head>
<body>
<button class="btn">Hover me</button>
<div class="card">Hover this card</div>
<div class="grow" title="hover to grow"></div>
</body>
</html>
Result
- A transition is written on the element’s normal state:
transition: background-color 0.3s ease. When:hoverchanges the background, the browser animates between the two values over 0.3 seconds. Hover over the items in the result to try it. - List several properties with commas, each with its own duration.
transform: translateY(-3px)lifts the button andscale(1.05)enlarges the card. Transforms do not affect the layout around the element, so neighbours do not jump.- The timing function controls the speed curve:
ease(the default),linear,ease-in,ease-out,ease-in-outor a customcubic-bezier().
Keyframe Animations
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Keyframe animations</title>
<style>
body { font-family: sans-serif; margin: 10px; display: flex; gap: 24px; align-items: center; }
@keyframes spin { to { transform: rotate(360deg); } }
.spinner { width: 36px; height: 36px; border: 4px solid #cfd8dc; border-top-color: #1565c0;
border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.2); } }
.heart { font-size: 32px; color: #c62828; animation: pulse 1.2s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-40px); opacity: 0; } to { transform: none; opacity: 1; } }
.msg { background: #e8f5e9; padding: 8px 12px; border-radius: 6px;
animation: slide .8s ease-out 0.3s both; }
@media (prefers-reduced-motion: reduce) { .spinner, .heart, .msg { animation: none; } }
</style>
</head>
<body>
<div class="spinner" aria-label="Loading"></div>
<div class="heart">♥</div>
<div class="msg">Saved successfully</div>
</body>
</html>
Result
@keyframes spin { to { transform: rotate(360deg); } }defines an animation namedspin. Steps can usefrom/toor percentages such as0%, 50%, 100%.animation: spin 1s linear infiniteapplies it: name, duration, timing function and iteration count.- The message uses a 0.3s delay and the fill mode
both, so it stays invisible during the delay and keeps its final state afterwards. - The
prefers-reduced-motionmedia query turns the animations off for people who have asked their operating system to reduce motion, for example because movement makes them dizzy.
Which Properties to Animate
| Property | Performance | Notes |
|---|---|---|
transform | Very smooth | Move, scale, rotate without changing layout |
opacity | Very smooth | Fades |
background-color, color, box-shadow | Good | Repaints the element |
width, height, top, margin | Slower | Recalculates layout on every frame |
display | Not animatable | Use opacity and visibility instead |
Common Mistakes
- Putting the transition only on
:hover, so the element animates in but snaps back instantly. - Using
transition: all, which animates properties you did not intend and can hurt performance. - Animations that are too slow or too many; most interface transitions should take 150 to 300 milliseconds.
- Ignoring reduced-motion preferences.
Practice Exercises
Exercise 1. Make a link’s underline colour change smoothly over 0.2 seconds on hover.
Show answer
a { text-decoration-color: transparent; transition: text-decoration-color .2s; }
a:hover { text-decoration-color: currentColor; }
Exercise 2. Write a keyframe animation that fades an element in from 0 to full opacity over half a second, once.
Show answer
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.notice { animation: fade-in .5s ease-out; }
Frequently Asked Questions
When should I use a transition and when an animation?
Use a transition for a change between two states triggered by something, such as hover, focus or a class added by JavaScript. Use keyframes for multi-step motion or anything that runs by itself, such as loaders and attention effects.
Can JavaScript start CSS animations?
Yes. The usual way is to add or remove a class with element.classList.add("open"), which triggers the transition or animation defined in CSS. See the JavaScript tutorials.
Summary
transitionanimates a property between states.@keyframesplusanimationruns multi-step motion.- Animate
transformandopacityfor the smoothest results. - Keep motion short and respect
prefers-reduced-motion.
