T2 CSS · Lesson 9
Transitions and animations
Making changes take time with transition; how a change moves, with timing functions and delay; moving, growing and turning boxes with transform; things that move by themselves with @keyframes and animation; and motion with care, for visitors who ask for less and for cheap phones.
- make a change of style take time with transition, and say which properties can change smoothly
- choose how a change moves with timing functions, and delay it
- move, grow and turn boxes with transform, and choose the point they work from
- make things move by themselves with @keyframes and animation
- respect prefers-reduced-motion, and keep motion smooth with transform and opacity
The back cover
A page that snaps from one look to the next feels like a light switch; a page that eases between them feels like a dimmer. Movement can guide the eye, but it can also distract, and for some readers it causes real harm.
These pages show transitions that make a change take time, and the timing functions that shape it. They show transform, which moves, grows and turns a box without moving anything else, and animations that run by themselves. They end with motion done with care, so that the page works for everyone.
Before you start
The story: The board that jumped
Corin has made each stall's card turn gold when a visitor points at it. It works, but the change is a jump: grey one instant, gold the next, like a lamp switched on in a dark room. Visitors blink at it.
The warden has two more wishes. While the list of stalls is being fetched, a small wheel should turn, so that visitors know the board is working. And an old weaver, Kamala, asks that nothing on the board should swing about, because moving things make her dizzy.
Meher smiles. "A change can take its time," she says. "Tell each change how long to take and how to move. And let anyone who asks for stillness have it."
Every change so far has happened in an instant: a rule applies, and the page is redrawn at once. Real things do not move like that. A door swings open; a lamp warms up; a card slid across a table slows down before it stops. This lesson makes changes on a page take time, so that they look and feel natural, and makes things move by themselves when there is a reason to. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| A rule can change a style. | .card.on { background-color: gold; } when a class is added. | A transition makes that change take time. |
| States change styles. | a:hover and button:focus style a link or a button. | Hover and focus changes are the usual things to animate. |
| Colours are numbers. | rgb(255, 215, 0) is gold. | A colour can pass smoothly through the numbers between two colours. |
| Time has units. | One second is 1000 milliseconds. | Durations are written in s or ms. |
| Relative positioning moves a box without moving others. | position: relative; top: -2px. | transform moves a box the same way, and more cheaply. |
| Media queries ask about the screen. | @media (min-width: 600px). | One query asks whether the visitor wants less motion. |
| The cascade picks a winner. | A later rule beats an earlier one of equal weight. | Shorthands and longhands for transitions follow the same rules. |
| Accessibility matters. | alt text, labels, contrast. | Motion can make some people dizzy or ill, so it must be optional. |
| Measuring what was drawn. | getComputedStyle(), getBoundingClientRect(). | The checks pause a change halfway and measure it. |
By the end of this lesson you will be able to:
- make a change of style take time with transition, and say which properties can change smoothly;
- choose how a change moves with timing functions such as ease and linear, and delay it;
- move, grow and turn boxes with transform: translate, scale and rotate;
- make things move by themselves with @keyframes and animation, such as a loading spinner;
- respect visitors who ask for less motion, and keep animations smooth by moving only transform and opacity.
How this lesson checks itself
A change that takes time is hard to check by looking. So the checks ask the browser for the running change itself, stop it at a chosen moment, such as halfway through, and measure the style there.
In one line
Transitions and animations make style changes take time and things move by themselves; you need states such as hover, colours as numbers, time in s and ms, and media queries.
Changes that take time: transition
At a glance
A light switch is either off or on, but a dimmer turns slowly and the light rises through every brightness in between. A transition turns a change of style from a switch into a dimmer. You name the property and give the change a duration; the default duration is 0s, which is why changes are instant.
Write the transition on the ordinary rule, not on the hover rule, so that it plays both ways, in and out. Only properties with values in between, such as colours and lengths, can change smoothly.
Take a guess: If transition is written only on the :hover rule, does the change also animate when the pointer leaves?
A light switch is either off or on: press it and the room is lit at once. A dimmer is turned slowly, and the light rises through every brightness in between. So far every change on a page has been a switch. This section makes it a dimmer.
The checks in this lesson cannot move a mouse. So when a card should change as a visitor points at it, a class called lit stands in for the pointer, and the check adds it. On a real site you would write :hover in its place, as the later blocks do.
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 200px; height: 80px; background-color: rgb(200, 200, 200); }
.card.lit { background-color: rgb(255, 215, 0); }
</style>
<div class="card">Masala tea, ₹15</div>
Checked in Chrome
- adding the class starts no change that takes time
- the card is gold at once: a switch, not a dimmer
A rule that makes a change of style take time. When the property it names changes, for any reason, the browser moves it through all the values in between over the duration you give, instead of jumping.
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 200px; height: 80px; background-color: rgb(200, 200, 200); }
.card.lit { background-color: rgb(255, 215, 0); }
.card { transition: background-color 200ms linear; }
</style>
<div class="card">Masala tea, ₹15</div>
Checked in Chrome
- adding the class now starts one change that takes time
- at 0ms it is still grey
- at 100ms, halfway, it is rgb(228, 208, 100), between grey and gold
- at the end, gold
Look at the halfway colour. Grey is rgb(200, 200, 200) and gold is rgb(255, 215, 0). Halfway, each number is halfway between its two ends: red goes from 200 to 255, and its middle is 227.5, drawn as 228; green from 200 to 215, so 207.5, drawn as 208; blue from 200 to 0, so 100. A colour can change smoothly because it is made of numbers, and there is always a number in between.
The four parts of a transition
| Part | Longhand property | Example | What it says |
|---|---|---|---|
| what changes | transition-property | background-color | which property to animate |
| how long | transition-duration | 200ms or 0.2s | the time from start to end |
| how it moves | transition-timing-function | linear, ease | steady, or speeding up and slowing down (the next section) |
| when it starts | transition-delay | 100ms | a wait before the change begins |
The shorthand transition takes them in one line, in that order: transition: width 300ms ease-in 100ms. The rule for the two times is simple to keep: the first time is the duration, the second is the delay.
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; transition: width 300ms ease-in 100ms; }
</style>
<div class="bar">Loading</div>
Checked in Chrome
- the property: width
- the first time, the duration: 300ms, read back as 0.3s
- the timing function: ease-in
- the second time, the delay: 100ms
A transition with no duration
The duration starts at 0s. A rule such as transition-property: width on its own names what should change but gives it no time, so the change is still a jump. Every transition needs a duration.
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; transition-property: width; }
.bar.full { width: 300px; }
</style>
<div class="bar">Loading</div>
Checked in Chrome
- (the slip: with no duration, no change runs, and the bar jumps to 300 at once)
Several properties, or all
A comma separates the properties, and each can have its own timing: transition: transform 200ms, background-color 300ms. The word all makes every property that changes take the same time.
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 200px; height: 80px; background-color: rgb(200, 200, 200); }
.card.lit { background-color: rgb(255, 215, 0); }
.card { transition: transform 200ms, background-color 300ms; }
.card.lit { transform: translateY(-4px); }
</style>
<div class="card">Masala tea, ₹15</div>
Checked in Chrome
- two changes run, each with its own duration: 300 and 200 ms
Use all with care. It also animates properties you did not mean to, such as a width that changes when a sidebar opens, and every animated property costs the browser work. Naming the properties says exactly what should move.
What can change smoothly
A property can change smoothly only if there are values in between its two ends.
| Kind of value | Examples | Smooth? |
|---|---|---|
| colours | color, background-color, border-color | yes |
| lengths | width, padding, font-size | yes |
| numbers | opacity | yes: 0.5 is between 0 and 1 |
| transforms | transform | yes, and cheaply (the transform section) |
| kinds and names | display, font-family | no: there is nothing between none and block, so they switch |
<!DOCTYPE html>
<style>
body { margin: 0; }
.tip { display: none; transition: display 300ms; }
.tip.open { display: block; }
</style>
<p class="tip">Free delivery today</p>
Checked in Chrome
- (the slip: display switches from none to block at once; a fade needs opacity)
Where to write the transition
Write the transition on the element's ordinary rule, not on the rule for its changed state. A transition is read from the style the element is changing to. Written only on the state rule, it is there on the way in, but gone on the way out, so the card warms up slowly and then snaps back. Written on the ordinary rule, it is there both ways.
<!DOCTYPE html>
<style>
body { margin: 0; }
button { padding: 12px; background-color: rgb(200, 200, 200); transition: background-color 200ms linear; }
button:hover, button:focus { background-color: rgb(255, 215, 0); }
</style>
<button>Add to cart</button>
Checked in Chrome
- given focus, the button is halfway to gold at 100ms
- and when it loses focus, the change back also takes time
A memory aid
The transition lives on the box; the change lives on the state. Then it plays both ways.
Worked example 1
A sweet shop's "Order now" button is orange, rgb(240, 120, 0). When a visitor points at it or reaches it with the Tab key, it should turn deep red, rgb(120, 0, 0), over 200 milliseconds, at a steady speed, and turn back the same way. Write the rules and work out its colour at 100 milliseconds.
What we have: the two colours, the time, a steady speed, and two ways of reaching the button
What we need: the transition, the state rule, and the colour halfway
The plan: a transition of background-color 200ms linear on the button's own rule; the red on :hover and :focus; halfway, each number is the middle of its two ends
- Red: from 240 to 120, the middle is 180.
- Green: from 120 to 0, the middle is 60.
- Blue: 0 at both ends, so 0.
<!DOCTYPE html>
<style>
body { margin: 0; }
.order { padding: 12px; color: white; background-color: rgb(240, 120, 0); transition: background-color 200ms linear; }
.order:hover, .order:focus { background-color: rgb(120, 0, 0); }
</style>
<button class="order">Order now</button>
Checked in Chrome
- at 100ms: rgb(180, 60, 0)
- at the end: deep red
.order { transition: background-color 200ms linear; } .order:hover, .order:focus { background-color: rgb(120, 0, 0); }: rgb(180, 60, 0) at 100 milliseconds.
Check: the browser, paused at 100 milliseconds, showed rgb(180, 60, 0).
When you are stuck: transitions
Still a jump: is there a duration? The default is 0s.
Smooth on the way in, a snap on the way out: move the transition from the state rule to the ordinary rule.
Nothing happens for a property: it may have no values in between, as display has; use opacity or transform.
Your turn
In transition: color 400ms ease 200ms, which time is the delay?Tap to reveal
- The first time is the duration.
200ms, the second time.
Why can background-color change smoothly but display cannot?Tap to reveal
- Is there a value between none and block?
Colours are numbers with values in between; display has nothing in between.
Which rule should hold the transition: .card or .card:hover?Tap to reveal
- It should play both ways.
.card, the ordinary rule.
Did you know?
Transitions began at Apple
CSS transitions began at Apple. On 31 October 2007 Dave Hyatt announced on the WebKit blog that WebKit now supported animation specified in CSS, starting with the simplest kind, called a transition. The CSS Transitions standard lists him and two other Apple engineers, Dean Jackson and Chris Marrin, among its former editors.
Why it fits here: This topic's transition property is the one that 2007 post introduced.
Sources: WebKit blog: CSS Animation, by Dave Hyatt; W3C: CSS Transitions
The way back is quicker
Point at a card, leave before its transition ends, and the way back is quicker. The CSS Transitions standard explains that if the return trip took its full time to cover the shorter distance, the effect would be distractingly lopsided. So it makes the second transition shorter, in step with how far the first one had got.
Why it fits here: It is a detail of this topic's both-ways transition: the way back shrinks when the way in was cut off.
Source: W3C: CSS Transitions
In one line
A transition makes a change of style take time: name the property, give it a duration (the default is 0s), and write it on the ordinary rule so that it plays both ways. Only properties with values in between can change smoothly.
How a change moves: timing functions and delay
At a glance
An autorickshaw does not leave a signal at full speed or stop dead at the next one: it speeds up gently and slows down before it stops. A change on a page looks natural when it moves like that. The timing function says how a change spreads over its duration.
The value linear is steady, and ease, the default, starts quickly and ends gently. The value ease-out suits things arriving, ease-in suits things leaving, and steps() jumps in steps. The transition-delay property waits before the change begins.
Take a guess: Which timing function suits a menu that slides in and stops on screen: ease-in or ease-out?
An autorickshaw does not leave a signal at full speed, and it does not stop dead at the next one. It speeds up gently, runs, and slows down before it stops. Things in the real world move like that, and a change on a page looks natural when it does the same. The timing function says how a change spreads over its duration: steady, or faster at some moments and slower at others.
Every block in this section grows a bar from 100 to 300 pixels over one second, and the checks stop it at a quarter, a half and three quarters of the way through, to see how far it has gone.
linear: the same speed all the way
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; height: 20px; background-color: teal; transition: width 1000ms linear; }
.bar.full { width: 300px; }
</style>
<div class="bar"></div>
Checked in Chrome
- at 250ms: 150 wide
- at 500ms: 200 wide
- at 750ms: 250 wide
With linear, a quarter of the time gives a quarter of the distance: 50 of the 200 pixels at each quarter. It is steady, like a train at full speed, and it looks mechanical for things that start and stop.
ease: the default, fast early and gentle at the end
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; height: 20px; background-color: teal; transition: width 1000ms ease; }
.bar.full { width: 300px; }
</style>
<div class="bar"></div>
Checked in Chrome
- at 250ms: 182 wide
- at 500ms: 260 wide
- at 750ms: 292 wide
ease is the default, used when you name none. Halfway through the time, it has already covered 160 of the 200 pixels, 80% of the way, and it spends the second half settling gently into place.
| Timing function | At 250ms | At 500ms | At 750ms | How it feels | Good for |
|---|---|---|---|---|---|
linear | 150 | 200 | 250 | steady, mechanical | spinners and progress that should not speed up |
ease | 182 | 260 | 292 | quick, then gentle | most small changes (the default) |
ease-in | 119 | 163 | 224 | slow start, fast finish | things leaving the screen |
ease-out | 176 | 237 | 281 | fast start, slow finish | things arriving, such as a menu opening |
ease-in-out | 126 | 200 | 274 | slow at both ends | things that move from one place to another |
The numbers in the table are the bar's width in the browser at each moment. Notice that ease-in-out is exactly halfway at the half, like linear, but slower than it at both ends.
ease-out: arriving
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; height: 20px; background-color: teal; transition: width 1000ms ease-out; }
.bar.full { width: 300px; }
</style>
<div class="bar"></div>
Checked in Chrome
- at 250ms: 176 wide
- at 500ms: 237 wide
- at 750ms: 281 wide
A memory aid
ease-out for arriving, ease-in for leaving. A thing that arrives slows down as it settles; a thing that leaves speeds up as it goes.
steps(): jumping in steps
steps(4) does not glide at all: it makes the change in 4 equal jumps, like the second hand of a station clock. It suits things that should tick, such as a counter, or a picture made of separate frames.
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; height: 20px; background-color: teal; transition: width 1000ms steps(4); }
.bar.full { width: 500px; }
</style>
<div class="bar"></div>
Checked in Chrome
- at 200ms, still 100: the first jump has not come
- at 250ms, the first jump: 200
- at 999ms, three jumps: 400; the last jump comes at the end
Your own curve: cubic-bezier()
Every smooth timing function is a curve, and cubic-bezier() lets you draw your own with four numbers. ease itself is cubic-bezier(0.25, 0.1, 0.25, 1). You rarely need to type one: DevTools shows a curve editor when you click the little curve icon beside a timing function in the Styles panel.
the curve that ease is made of
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; height: 20px; background-color: teal; transition: width 1000ms cubic-bezier(0.25, 0.1, 0.25, 1); }
.bar.full { width: 300px; }
</style>
<div class="bar"></div>
Checked in Chrome
- at 250ms: 182 wide
- at 500ms: 260 wide
- at 750ms: 292 wide
transition-delay: waiting before the change
A delay makes the change wait before it begins. A menu that opens when the pointer rests on it for 300 milliseconds, rather than at the slightest touch, is easier to use, because a pointer passing over it on its way elsewhere does not open it.
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { width: 100px; height: 20px; background-color: teal; transition: width 200ms linear 300ms; }
.bar.full { width: 300px; }
</style>
<div class="bar"></div>
Checked in Chrome
- at 200ms, inside the 300ms delay: nothing has moved
- at 400ms, 100ms into the 200ms change: halfway, 200
- at 500ms, 300 + 200: finished
How long?
Small changes on buttons and cards feel right at about 150 to 300 milliseconds. Shorter is hard to notice; much longer makes the page feel slow, because the visitor waits for every change to finish.
Worked example 2
A delivery app's menu should slide open when a button is pressed, taking 300 milliseconds. Which timing function fits, and how far through its change is the menu at 150 milliseconds with that function, if it grows from 100 to 300 pixels in that time?
What we have: a menu arriving on screen, 300ms, growing 200 pixels
What we need: the timing function and the width halfway through the time
The plan: a menu arriving should use ease-out; measure it at half its time
- Arriving: the menu should move quickly at first and settle gently, so ease-out.
- At half the time, ease-out has covered more than half the distance; the browser measures it.
<!DOCTYPE html>
<style>
body { margin: 0; }
.menu { width: 100px; height: 20px; background-color: teal; transition: width 300ms ease-out; }
.menu.open { width: 300px; }
</style>
<div class="menu">Menu</div>
Checked in Chrome
- at 150ms, half the time: 237, more than halfway
transition: width 300ms ease-out; At 150 milliseconds the menu is 237 pixels wide: 137 of its 200, more than two thirds of the way, as an arriving thing should be.
Check: the browser, stopped at 150 of 300 milliseconds, measured 237, the same share as the one second bar at its half.
When you are stuck: timing functions
Not sure which to choose: ease for most things, ease-out for things arriving, ease-in for things leaving, linear for spinners.
A change that seems to wait: is there a second time in the shorthand? It is the delay.
To see a curve: click the curve icon beside the timing function in DevTools' Styles panel.
Your turn
Which timing function is used when you name none?Tap to reveal
- The default.
ease.
A toast message slides in. Which timing function suits it best?Tap to reveal
- It is arriving.
ease-out: fast at first, gentle as it settles.
transition: width 200ms linear 300ms. When does the change end?Tap to reveal
- Delay first, then the duration.
At 500ms: 300 + 200.
Did you know?
ease is one exact curve
The default, ease, is one exact curve: the CSS Easing standard defines it as cubic-bezier(0.25, 0.1, 0.25, 1). In such a curve the two x values must lie between 0 and 1, but the y values may go beyond, so a change can overshoot its end and settle back, like a spring.
Why it fits here: It shows what this topic's ease really is, and how cubic-bezier() can make curves of your own.
Curves that came from car bodies
The curves behind cubic-bezier() came from cars. They are named after the French engineer Pierre Bézier, who used them in the 1960s to design the curves of Renault car bodies. Earlier, in 1959, the mathematician Paul de Casteljau had worked out a method for the same curves, and he was the first to use them in computer-aided design, at Citroën.
Why it fits here: Every curve this topic draws with cubic-bezier() is one of these car-body curves.
Sources: Wikipedia: Bézier curve; W3C: CSS Easing Functions Level 1
In one line
The timing function spreads a change over its time: linear is steady, ease (the default) is quick then gentle, ease-out suits arrivals, ease-in departures, steps() jumps; transition-delay waits before the change begins.
Moving, growing and turning: transform
At a glance
Tilt a photo in front of a window and its shadow on the wall tilts too, yet the photo's place in the album has not changed. The transform property works the same way: it changes how a box is drawn, not where it sits, so nothing around it moves.
The function translate() moves the drawing, scale() resizes it and rotate() turns it, about the point set by transform-origin. Several steps go in one value, applied in order. And a transform on a parent traps any fixed box inside it.
Take a guess: If a card has transform: scale(1.5), do the cards beside it move out of the way?
Hold a photo up to a window and its shadow falls on the wall. Tilt the photo and the shadow tilts; move it closer and the shadow grows; yet the photo's place in the album has not changed. transform works on the same idea. It changes how a box is drawn, not where it sits in the page's layout, so nothing around it moves.
A property that moves, resizes or turns the drawing of a box, without changing its place in the flow. The boxes around it behave as if nothing had happened.
translate: moving
<!DOCTYPE html>
<style>
body { margin: 0; }
.stall { width: 100px; height: 50px; background-color: gold; }
.moved { transform: translate(20px, 10px); background-color: teal; }
</style>
<div class="stall moved">Rice</div>
<div class="stall">Dal</div>
Checked in Chrome
- Rice is drawn at 20, 10, and Dal stays at 50, as if Rice had not moved
- the browser holds it as a matrix whose last two numbers are the move
translate(20px, 10px) moves the drawing 20 pixels right and 10 down. translateX() and translateY() move along one direction. It looks like position: relative with offsets, and the old place is kept in the same way. A percentage in translate means a share of the box's own size, which makes the classic way to centre a pinned box of any size.
<!DOCTYPE html>
<style>
body { margin: 0; }
.screen { position: relative; width: 400px; height: 200px; }
.badge { position: absolute; top: 50%; left: 50%; width: 100px; height: 50px; transform: translate(-50%, -50%); }
</style>
<div class="screen"><div class="badge">Sale</div></div>
Checked in Chrome
- top and left put the badge's corner at the centre, then it moves back half its own size: 150, 75
scale: growing and shrinking
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 100px; height: 50px; margin: 100px; background-color: gold; transform: scale(1.5); }
</style>
<div class="card">Rice</div>
Checked in Chrome
- drawn 150 by 75, one and a half times the size, grown from its centre
- yet its width in the layout is still 100
scale(1.5) draws the box one and a half times as wide and tall; scale(0.5) draws it half the size. The text inside grows too, because the whole drawing is scaled. The box grew from its centre, 25 pixels each side across and about 12 up and down, which is why it starts at 75 and 88.
rotate: turning
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 100px; height: 50px; margin: 100px; background-color: gold; transform: rotate(90deg); }
</style>
<div class="card">Rice</div>
Checked in Chrome
- turned a quarter of a circle, clockwise
- so the area it covers is 50 wide and 100 tall
deg means degrees: 90 is a quarter turn clockwise, 180 a half, 360 a whole circle, and a negative number turns anticlockwise.
transform-origin: the point it works from
A box scales and turns about its centre, unless you choose another point. Think of a door: it turns about its hinges, not its middle. transform-origin sets that point.
<!DOCTYPE html>
<style>
body { margin: 0; }
.a, .b { position: absolute; top: 100px; left: 100px; width: 100px; height: 50px; transform: scale(2); }
.b { transform-origin: top left; }
</style>
<div class="a"></div>
<div class="b"></div>
Checked in Chrome
- from the centre, it grows in every direction: 50, 75
- from the top left, that corner stays at 100, 100, and it grows right and down
Several at once, and their order
A transform can hold several steps, separated by spaces, and the order matters. Each step works in the box's own turned and moved directions, the way "walk ten steps forward" means something different after you have turned to face another way.
<!DOCTYPE html>
<style>
body { margin: 0; }
.a, .b { position: absolute; top: 100px; left: 100px; width: 100px; height: 50px; }
.a { transform: translateX(100px) rotate(90deg); }
.b { transform: rotate(90deg) translateX(100px); }
</style>
<div class="a"></div>
<div class="b"></div>
Checked in Chrome
- move right, then turn: it ends up to the right, at 225, 75
- turn, then move "right": its right now points down, so it ends up below, at 125, 175
Two rules that both set transform
A second transform replaces the first; it does not add to it. If .card has translateY(-4px) and .card.big has scale(1.2), the big card loses its lift. Write both steps in one value.
With a transition
Transforms and transitions belong together: a card that lifts a little as the pointer reaches it is the most common small movement on the web.
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 200px; height: 80px; background-color: gold; transition: transform 200ms linear; }
.card.lit { transform: translateY(-4px); }
</style>
<div class="card">Masala tea, ₹15</div>
Checked in Chrome
- at 100ms, halfway: lifted 2 pixels
- at the end: lifted 4 pixels
A trap: a transform on a parent
The positioning lesson promised this detail. A position: fixed box is pinned to the window, unless one of its ancestors has a transform. That ancestor then becomes the box's containing block, and the fixed box stays inside it and scrolls with it.
<!DOCTYPE html>
<style>
body { margin: 0; }
.panel { margin: 50px; height: 100px; transform: translateX(0); }
.help { position: fixed; top: 0; left: 0; width: 60px; height: 30px; background-color: gold; }
</style>
<div class="panel"><div class="help">Help</div></div>
Checked in Chrome
- (the slip: the panel's transform traps the fixed button at the panel's corner, 50, 50, not the window's corner)
Even a transform that moves nothing, such as translateX(0), does this. A transform also starts a new stacking context, the little pile of the positioning lesson. If a fixed box or a z-index behaves strangely, look up its ancestors for a transform.
Worked example 3
A product card, 200 by 100, sits 20 pixels from the top and left of the page. When pointed at, it should lift 4 pixels and grow by 2%, over 200 milliseconds, and settle back the same way. Write the rules and work out where its top left corner is drawn at the end, and how big it is.
What we have: a 200 by 100 card at 20, 20; a lift of 4; a growth of 2%
What we need: one transform with both steps, a transition on the ordinary rule, and the final numbers
The plan: transform: translateY(-4px) scale(1.02) in one value, because a second rule would replace the first; the card grows from its centre
- Size: 200 × 1.02 = 204 wide, and 100 × 1.02 = 102 tall.
- Across: growing from the centre takes 2 pixels to the left: 20 − 2 = 18.
- Down: growing takes 1 pixel up, and the lift 4 more: 20 − 1 − 4 = 15.
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 200px; height: 100px; margin: 20px; background-color: gold; transition: transform 200ms; }
.card.lit { transform: translateY(-4px) scale(1.02); }
</style>
<div class="card">Masala tea, ₹15</div>
Checked in Chrome
- drawn at 18, 15, and 204 by 102
.card { transition: transform 200ms; } .card:hover { transform: translateY(-4px) scale(1.02); }: drawn at 18, 15, 204 by 102 pixels.
Check: the browser measured exactly those four numbers once the change had finished.
When you are stuck: transforms
A transform that does nothing: it may be replaced by a later rule that also sets transform; write the steps in one value.
Not where you expected: the order of the steps matters; read them left to right, each in the box's turned directions.
Growing from the wrong place: set transform-origin.
Your turn
A box has transform: translate(0, 30px). Do the boxes after it move?Tap to reveal
- A transform changes the drawing only.
No: the layout stays as it was.
What does scale(0.5) do to a box 200 wide?Tap to reveal
- Half.
It is drawn 100 wide, though its layout width stays 200.
Which transform-origin makes a card swing like a door hinged on its left edge?Tap to reveal
- The hinge is the fixed point.
transform-origin: left.
Did you know?
The separate transform properties keep a fixed order
CSS also has separate translate, rotate and scale properties, and their order is fixed. The CSS Transforms standard builds the drawing by translating with translate, then rotating with rotate, then scaling with scale, and only then applying the transform property's steps, from left to right. So writing scale before translate in your rule changes nothing.
Why it fits here: In this topic's transform value the order you write matters; in these separate properties it does not.
Source: W3C: CSS Transforms Level 2
A full circle is 1turn
Angles in CSS are not only degrees. The CSS Values standard has a unit called turn, with exactly 1 turn in a full circle, so rotate(0.25turn) is a right angle, the same as rotate(90deg). It also accepts gradians, with 100grad in a right angle, and radians.
Why it fits here: This topic's rotate() accepts any of these units; turn is the easiest one to picture.
In one line
A transform changes the drawing of a box, not its place: translate moves, scale resizes and rotate turns it, about transform-origin. Steps go in one value, in order, and a transform on a parent traps fixed boxes.
Moving by itself: @keyframes and animation
At a glance
A transition waits for a change. Some movement needs no cause: a wheel turning while a list loads, a badge that pulses. An animation runs by itself as soon as its rule applies, and it can pass through many stops, like a train's timetable of stations.
The @keyframes rule names a list of stops, from 0% to 100%. The animation property plays it on an element, with a name, a duration, a timing function, a delay, a count, a direction and a fill mode.
Take a guess: Does an animation need a hover or a click before it starts to move?
A transition waits for a change, and goes from one state to another. Some movement has no such cause: a wheel that turns while a list loads, a badge that pulses. An animation runs by itself, as soon as the rule applies, and can pass through as many stops as you like, the way a train's timetable lists Mumbai, then Surat, then Ahmedabad, each at its own time.
A named list of stops for an animation. Each stop gives the styles at one moment, as a share of the animation's time: from is 0%, to is 100%. The browser works out everything in between.
An animation has two halves: the @keyframes rule, which says what happens, and the animation property on an element, which says who does it, for how long, and how many times.
A loading wheel
<!DOCTYPE html>
<style>
body { margin: 0; }
.wheel { width: 40px; height: 40px; box-sizing: border-box; border: 6px solid lightgrey; border-top-color: teal; border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
<div class="wheel"></div>
Checked in Chrome
- one animation runs by itself, named spin
- and it never stops: infinite
- at 250ms, a quarter of the second, it has turned 90 degrees
- and at 1250ms, on the second turn, the same again
Read animation: spin 1s linear infinite aloud: "play the keyframes called spin, each round taking one second, at a steady speed, for ever". The timing is linear because a wheel that sped up and slowed down each turn would look like it was limping. The wheel itself is a circle with one coloured side, which is what makes the turning visible.
Stops in percentages
With more than two stops, write each as a percentage of the time. A badge that swells and shrinks back needs three.
<!DOCTYPE html>
<style>
body { margin: 0; }
.badge { display: inline-block; padding: 4px; background-color: gold; animation: pulse 1s linear infinite; }
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
</style>
<span class="badge">New</span>
Checked in Chrome
- at 250ms, halfway to the first stop: scale 1.1
- at 500ms, the 50% stop: scale 1.2
- at 750ms, on the way back: scale 1.1
The parts of an animation
| Longhand property | Example | What it says |
|---|---|---|
animation-name | spin | which keyframes to play |
animation-duration | 1s | how long one round takes |
animation-timing-function | linear | how it moves within each round |
animation-delay | 200ms | a wait before it starts |
animation-iteration-count | 3 or infinite | how many rounds |
animation-direction | alternate | forwards every round, or back and forth |
animation-fill-mode | forwards | which styles to keep once it has ended |
animation-play-state | paused | running or paused |
The shorthand animation takes them in one line. As with transitions, the first time is the duration and the second the delay.
Back and forth: alternate
alternate plays every second round backwards, so that a movement swings to and fro without a jump at the end of each round.
<!DOCTYPE html>
<style>
body { margin: 0; }
.bar { height: 20px; background-color: teal; animation: grow 1s linear 2 alternate; }
@keyframes grow {
from { width: 100px; }
to { width: 300px; }
}
</style>
<div class="bar"></div>
Checked in Chrome
- at 500ms, halfway out: 200
- at 1000ms, the far end: 300
- at 1500ms, halfway back: 200
What happens at the end: fill mode
When an animation ends, the element goes back to its own styles, as if the animation had never been. To keep the last stop's styles, write forwards.
<!DOCTYPE html>
<style>
body { margin: 0; }
.note { opacity: 0.2; animation: show 1s forwards; }
@keyframes show {
to { opacity: 1; }
}
</style>
<p class="note">Saved</p>
Checked in Chrome
- with forwards, the note stays at the last stop's opacity, 1
<!DOCTYPE html>
<style>
body { margin: 0; }
.note { opacity: 0.2; animation: show 1s; }
@keyframes show {
to { opacity: 1; }
}
</style>
<p class="note">Saved</p>
Checked in Chrome
- (the slip: without forwards, it fades in, then jumps back to its own 0.2)
There is often a tidier way: make the element's own style the final look, and let the keyframes give only the starting look with from. Then nothing needs to be kept, and the element is right even if the animation never plays.
Transition or animation?
| transition | animation | |
|---|---|---|
| starts when | a property changes | the rule applies, by itself |
| stops | two: from and to | as many as you like |
| repeats | no | any number of times, or for ever |
| plays backwards when undone | yes, by itself | no; the element jumps to its own style |
| use it for | responses to the visitor, such as hover | loading wheels, attention, entrances |
A memory aid
A transition answers; an animation performs. Something happens and a transition responds; an animation goes on by itself.
Worked example 4
A "Saved" message should appear by rising 20 pixels and fading in over 400 milliseconds, once, as it arrives. Its final look is its ordinary look. Write the keyframes and work out its opacity and its offset at 200 milliseconds, at a steady speed.
What we have: a rise of 20 pixels, a fade in, 400 milliseconds, once
What we need: keyframes that give only the starting look, and the halfway values
The plan: from { opacity: 0; transform: translateY(20px); } and no to stop, so the animation ends at the element's own look; halfway, each value is at its middle
- Opacity: from 0 to 1, the middle is 0.5.
- Offset: from 20 pixels to 0, the middle is 10.
<!DOCTYPE html>
<style>
body { margin: 0; }
.saved { animation: rise 400ms linear; }
@keyframes rise {
from { opacity: 0; transform: translateY(20px); }
}
</style>
<p class="saved">Saved</p>
Checked in Chrome
- at 200ms: opacity 0.5
- and 10 pixels below its place
- at the end, its own look: opacity 1
@keyframes rise { from { opacity: 0; transform: translateY(20px); } } .saved { animation: rise 400ms linear; }: opacity 0.5, 10 pixels low, at 200 milliseconds.
Check: the browser, stopped at 200 milliseconds, read exactly those values.
When you are stuck: animations
Nothing moves: check that the name in animation matches the name in @keyframes, letter by letter; a mismatch gives no error.
It jumps back at the end: add forwards, or make the element's own style the final look.
A jerk at the end of each round: use alternate, or make the last stop match the first.
Your turn
What does animation: pulse 2s ease 500ms 3 do?Tap to reveal
- First time, duration; second, delay.
Plays pulse 3 times, 2 seconds a round, after a 500ms wait.
Why should a loading wheel use linear timing?Tap to reveal
- It should not limp.
So that it turns at a steady speed.
What does a from stop with no to stop end at?Tap to reveal
- The element's own style.
The element's ordinary look.
Did you know?
Missing ends are filled in
A @keyframes rule does not need both ends. The CSS Animations standard says that if the 0% or from keyframe is missing, the browser builds one from the element's own current values, and the same for a missing 100% or to keyframe. So a single to keyframe animates from wherever the element already is.
Why it fits here: It lets this topic's keyframes list only the stops that differ from the element's normal look.
Source: W3C: CSS Animations Level 1
Same name, last one wins
Two @keyframes rules with the same name do not combine. The CSS Animations standard says that the last one in document order wins, and all the earlier ones are ignored. So a stop written only in the first rule is lost, even though the second rule never mentions it.
Why it fits here: It is worth knowing when this topic's keyframes seem to ignore some of their stops.
Source: W3C: CSS Animations Level 1
In one line
@keyframes names a list of stops, and the animation property plays it on an element: name, duration, timing, delay, count, direction and fill mode; animations run by themselves, where transitions answer a change.
Motion with care
At a glance
For most people, a card that lifts is a small pleasure. For some, large movement on a screen brings dizziness or sickness, and for others it makes reading beside it hard. A careful page lets them have it still: it respects prefers-reduced-motion, the setting a phone or computer passes on.
It never flashes more than three times a second, and it lets long movement be paused. For smoothness, it animates transform and opacity, which need neither layout nor paint, so the movement stays even.
Take a guess: If a reader has asked their phone for reduced motion, should a page still play its big sliding animation?
For most people, a card that lifts or a panel that slides is a small pleasure. For some, it is a real problem. People with a disorder of the balance organs in the inner ear can feel dizzy or sick from large movement on a screen, as others do from reading in a moving car. Others find it hard to read while anything moves beside the text. A careful page lets them have it still.
Asking for less motion
Phones and computers have a setting for this: "Reduce motion" on an iPhone, "Remove animations" on Android, "Animation effects" off on Windows. A web page can read that setting with a media query.
A media feature that is reduce when the visitor has asked their device for less motion, and no-preference otherwise. Write it as @media (prefers-reduced-motion: reduce).
<!DOCTYPE html>
<style>
body { margin: 0; }
.wheel { width: 40px; height: 40px; background-color: teal; animation: spin 1s linear infinite; }
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.wheel { animation: none; }
}
</style>
<div class="wheel"></div>
Checked in Chrome
- for most visitors, the wheel turns
- this visitor has asked for less motion
- for a visitor who asked for less motion: so no animation runs
The check ran the same page twice: once in the usual window, and once in a window set up as a visitor who asked for less motion. A second way to write it starts from stillness, and adds movement only for visitors who have not asked for less. If a browser does not know the query, the page stays still.
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 200px; height: 80px; background-color: lightgrey; transition: background-color 200ms; }
.card.lit { background-color: gold; }
@media (prefers-reduced-motion: no-preference) {
.card { transition: background-color 200ms, transform 200ms; }
.card.lit { transform: translateY(-4px); }
}
</style>
<div class="card">Masala tea, ₹15</div>
Checked in Chrome
- for most visitors, two changes run: the colour and the lift
- for a visitor who asked for less motion, only the colour changes
Notice what stayed: the colour still changes. Reduced motion does not mean no change. Fades and colour changes are usually fine; it is large movement, spinning and sliding across the screen, that causes trouble.
Flashing, and a way to pause
| Rule | Why |
|---|---|
| Nothing should flash more than three times a second. | Fast flashing can bring on seizures in people with photosensitive epilepsy. This is the one rule of motion that can do real physical harm. |
| Anything that moves for more than five seconds needs a way to pause it. | A moving banner next to the text makes reading very hard for many people. |
A ticker of today's vegetable prices can pause itself when it is pointed at or reached with the keyboard, using animation-play-state.
<!DOCTYPE html>
<style>
body { margin: 0; overflow: hidden; }
.ticker { animation: slide 20s linear infinite; }
.ticker:hover, .ticker:focus { animation-play-state: paused; }
@keyframes slide {
from { transform: translateX(100%); }
to { transform: translateX(-100%); }
}
</style>
<p class="ticker" tabindex="0">Tomatoes ₹30 a kilo, onions ₹25 a kilo</p>
Checked in Chrome
- the ticker runs
- and given focus, it pauses
Smooth motion: what the browser does for each frame
A film is a run of still pictures, and so is movement on a screen. To look smooth, a page needs about 60 pictures a second, which leaves the browser 1000 ÷ 60, about 16.7 milliseconds, for each one. In that time, it may have three jobs to do.
| Job | What the browser does | Set off by changing |
|---|---|---|
| Layout | works out the size and place of every box | width, height, top, left, margin, font-size |
| Paint | fills in the colours of the boxes it must redraw | color, background-color, box-shadow |
| Composite | stacks the finished pictures on the screen | transform, opacity |
Each job sets off the ones below it. Changing a width forces layout, then paint, then composite, for every frame, and on a page with many boxes that can take longer than 16.7 milliseconds, so the movement stutters. Changing transform or opacity needs only the last job, and the graphics chip can usually do that alone. This is why the card in this lesson lifts with transform, and not with top.
<!DOCTYPE html>
<style>
body { margin: 0; }
.a, .b { position: relative; left: 0; width: 80px; height: 30px; background-color: teal; }
.a { transition: left 1000ms linear; }
.a.on { left: 200px; }
.b { transition: transform 1000ms linear; }
.b.on { transform: translateX(200px); }
</style>
<div class="a">left</div>
<div class="b">transform</div>
Checked in Chrome
- moving left: at 500ms, 100 across, with layout at every frame
- moving transform: the same picture, 100 across, with no layout at all
A memory aid
Animate transform and opacity. They cost the least, so they stay smooth, even on a low-priced phone.
will-change
will-change: transform warns the browser that an element is about to move, so that it can prepare a separate layer. Use it only to cure a stutter you have seen, and not on everything: each layer takes memory, and on a phone memory is short. Like a transform, it also starts a new stacking context.
Worked example 5
A loading wheel should turn for most visitors. A visitor who has asked for less motion should see the words "Loading..." in its place. Write the rules, and say what each visitor sees.
What we have: a wheel, the words, and a setting that decides between them
What we need: one page that shows the right one to each visitor
The plan: show the wheel and hide the words as the base; inside the reduced-motion query, reverse them
- Base: the wheel turns, and
.words { display: none; }hides the text. - Inside
@media (prefers-reduced-motion: reduce):.wheel { display: none; }and.words { display: block; }.
<!DOCTYPE html>
<style>
body { margin: 0; }
.wheel { width: 40px; height: 40px; background-color: teal; animation: spin 1s linear infinite; }
.words { display: none; }
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.wheel { display: none; }
.words { display: block; }
}
</style>
<div class="wheel"></div>
<p class="words">Loading...</p>
Checked in Chrome
- most visitors see the turning wheel
- this visitor sees the words, and nothing moves
.words { display: none; } @media (prefers-reduced-motion: reduce) { .wheel { display: none; } .words { display: block; } }
Check: the browser showed the wheel in the usual window and the words in the reduced-motion window.
Worked example 6
A photo panel slides 300 pixels to the right over 300 milliseconds when opened, and it was written with left. It stutters on cheap phones, and it should not slide at all for visitors who asked for less motion. Rewrite it.
What we have: a panel moved with left, a stutter, and a setting to respect
What we need: the same movement for most visitors, cheap to draw, with no slide for the others
The plan: use transform: translateX(300px), which needs no layout; put the transition inside the no-preference query, so the others get the jump to the end with no movement
- Move with transform:
.panel.open { transform: translateX(300px); }. The panel ends in the same place, with no layout at any frame. - Only for visitors with no preference:
.panel { transition: transform 300ms ease-out; }inside the query. - For the others there is no transition, so the panel is at once in its place.
<!DOCTYPE html>
<style>
body { margin: 0; }
.panel { width: 100px; height: 60px; background-color: gold; }
.panel.open { transform: translateX(300px); }
@media (prefers-reduced-motion: no-preference) {
.panel { transition: transform 300ms ease-out; }
}
</style>
<div class="panel">Photos</div>
Checked in Chrome
- for most visitors, the panel slides
- and ends 300 across
- for a visitor who asked for less motion, nothing slides
- for a visitor who asked for less motion: and the panel is at 300 at once
.panel.open { transform: translateX(300px); } @media (prefers-reduced-motion: no-preference) { .panel { transition: transform 300ms ease-out; } }
Check: the panel ended at 300 in both windows, sliding only in the usual one.
When you are stuck: careful motion
To test reduced motion without changing your settings: in DevTools, press Ctrl+Shift+P, type "reduced motion", and choose to emulate it.
A movement that stutters: is it changing width, top or left? Move it with transform.
Not sure a movement is needed: try the page without it. Motion should explain what happened, not decorate.
Your turn
Which media feature reads a visitor's wish for less motion?Tap to reveal
- It starts with prefers.
prefers-reduced-motion.
Which two properties are the cheapest to animate?Tap to reveal
- They need only the last job.
transform and opacity.
At 60 pictures a second, how long does the browser have for each?Tap to reveal
- 1000 ÷ 60.
About 16.7 milliseconds.
Did you know?
Why the rule against flashing exists
The rule against flashing has a famous reason. On 16 December 1997, a single broadcast of a Pokémon episode in Japan carried flashing scenes that set off photosensitive seizures in children across the country, and over 600 people, mostly children, were taken to hospitals. WCAG, the accessibility standard, asks that nothing on a page flashes more than three times in any one second.
Why it fits here: It is why this topic's careful page never flashes more than three times a second.
Sources: Wikipedia: Dennō Senshi Porygon; W3C: WCAG 2.2
Less motion helps attention too
The reduced motion setting is not only about dizziness. Media Queries Level 5 says a reader who chooses it prefers an interface that removes or replaces the kinds of motion that either bring discomfort to those with vestibular motion sensitivity, or distraction to those with attention deficits.
Why it fits here: This topic's prefers-reduced-motion serves readers who are easily distracted as well as those who get dizzy.
Source: W3C: Media Queries Level 5
In one line
Respect prefers-reduced-motion, never flash more than three times a second, let long movement be paused, and animate transform and opacity, which need neither layout nor paint, so that motion stays smooth.
Where it is used
Motion on a good page is small and has a reason: it shows what just happened, or that something is still happening.
| What you learned | Where it is used | A real example |
|---|---|---|
| transition on colour | buttons and links | a Pay button that deepens as it is pressed |
| transform with transition | cards and menus | a shop's product card lifting towards the pointer |
| ease-out | things arriving | a drawer sliding in from the side of a banking app |
| a linear, infinite animation | waiting | the wheel while a train ticket is being booked |
| keyframes with a from stop | messages | a "Saved" note rising into place |
| steps() | ticking counters, frame-by-frame pictures | a walking figure made of eight separate drawings |
| prefers-reduced-motion | every page with movement | a news site that stops its moving banners for those who ask |
| animation-play-state | tickers | a share-price ticker that pauses when pointed at |
Mistakes and debugging
These are the motion slips everyone makes, each measured in the browser.
1. The transition on the state rule
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { background-color: lightgrey; }
.card.lit { background-color: gold; transition: background-color 200ms; }
</style>
<div class="card">Masala tea</div>
Checked in Chrome
- on the way in, the change takes time
- (the slip: on the way out no change runs, and the card snaps back)
The fix: move the transition to .card, the ordinary rule, so that it plays both ways.
2. A keyframes name that does not match
<!DOCTYPE html>
<style>
body { margin: 0; }
.wheel { width: 40px; height: 40px; background-color: teal; animation: spinn 1s linear infinite; }
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
<div class="wheel"></div>
Checked in Chrome
- (the slip: spinn is not spin, so nothing moves, and no error appears anywhere)
The fix: copy the name, so that the two match letter by letter.
3. Two rules that set transform
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 100px; height: 50px; transform: translateY(20px); }
.card.big { transform: scale(2); }
</style>
<div class="card big">Rice</div>
Checked in Chrome
- (the slip: the second rule replaced the first, so the 20px move is gone)
The fix: write both steps in one value: transform: translateY(20px) scale(2).
4. Animating what forces layout
Animating width, height, top or margin works on your fast laptop and stutters on a cheap phone. The fix: move with transform, and fade with opacity.
5. transition: all
It is quick to write, and it animates things you never meant to, such as the width of a box whose window was resized. The fix: name the properties.
6. Fading with display
display has no values in between, so a fade from none to block is a jump. The fix: animate opacity, and add visibility: hidden so that the hidden box cannot be clicked.
7. Slow changes
A half-second change on every button makes a fast page feel slow, because the visitor waits for it each time. The fix: keep small changes between 150 and 300 milliseconds.
8. Motion with no way out
An endless animation, with no reduced-motion rule and no way to pause, is a real barrier for some visitors. The fix: a prefers-reduced-motion rule for every large movement.
The debugging corner: slow it down and look
In Chrome's DevTools, the Animations panel (in the More tools menu) records every transition and animation on the page. It can replay them at a quarter or a tenth of their speed, and you can drag a change to any moment to study it. The Performance panel shows whether frames are taking longer than 16.7 milliseconds, and its CPU throttling option makes your laptop as slow as a cheap phone.
Quick card
The lesson on one card.
Every idea of the lesson
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 200px; height: 80px; background-color: rgb(200, 200, 200); transition: background-color 200ms linear; }
.card:hover, .card:focus { background-color: rgb(255, 215, 0); }
.wheel { width: 40px; height: 40px; background-color: teal; animation: spin 1s linear infinite; }
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: no-preference) {
.card { transition: background-color 200ms linear, transform 200ms ease-out; }
.card:hover, .card:focus { transform: translateY(-4px); }
}
@media (prefers-reduced-motion: reduce) {
.wheel { animation: none; }
}
</style>
<button class="card">Masala tea, ₹15</button>
<div class="wheel"></div>
Checked in Chrome
- given focus, the card is halfway to gold at 100ms
- its colour and its lift both take time
- and at the end it has lifted 4 pixels
- and the wheel turns by itself
- for a visitor who asked for less motion, only the colour changes
- for a visitor who asked for less motion: and the wheel stays still
| Tool | Write it as | It means |
|---|---|---|
| transition | transition: transform 200ms ease-out 100ms | what, how long, how, and the wait |
| several | transition: transform 200ms, opacity 300ms | a comma between them |
| timing | linear, ease, ease-in, ease-out, ease-in-out | steady; the default; leaving; arriving; both |
| jumps | steps(4) | 4 equal jumps |
| move | translate(20px, 10px), translateY(-4px) | the drawing moves, the layout does not |
| resize | scale(1.5) | one and a half times the size |
| turn | rotate(90deg) | a quarter turn, clockwise |
| fixed point | transform-origin: top left | work from that corner |
| keyframes | @keyframes spin { to { transform: rotate(360deg); } } | a named list of stops |
| play them | animation: spin 1s linear infinite | name, duration, timing, rounds |
| at the end | animation-fill-mode: forwards | keep the last stop's styles |
| less motion | @media (prefers-reduced-motion: reduce) | rules for those who ask |
And the three habits: the transition on the ordinary rule, move with transform and opacity, and always offer stillness.
The story: Change, in its own time
Now a card warms from grey to gold over a fifth of a second, and lifts a little as it does, as if leaning towards the visitor. While the list arrives, the small wheel turns steadily in the corner, round and round.
On Kamala's glass, which asks for less motion, the cards still turn gold, but without lifting, and the wheel is replaced by a quiet line of text.
"It is the same board," Corin says, "but it feels alive now." Kamala nods. "And it keeps still for me."
Reagents gathered: transition, transform, ease, @keyframes, animation, prefers-reduced-motion.
Sources
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)
- HTML Living Standard (WHATWG) (CC BY 4.0: "Copyright WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a Creative Commons Attribution 4.0 International License" (its acknowledgements page, read 2026-10-26); reused with credit)