Have you ever made a flipbook in the corner of a school notebook? Every page holds a still drawing. When you flip the pages fast, the drawing seems to move. Web animation works the same way: a CSS value changes a little at a time, and the browser draws the page again many times a second. One picture that the browser shows is called a frame, just like one page of the flipbook. An animation feels smooth when the browser always finishes drawing the next frame before it is due on screen.
The page we will animate
Throughout this track we use the booking page of Mist Homestay, Mrs. Sri's small homestay in the mountains, which the UI/UX track uses too. The page has:
- the Terrace Room card ($35 a night), the Attic Room ($27.50) and the Garden Room ($42.50),
- the 1, 2, 3 night buttons,
- the total ("Total for 2 nights: $70"),
- the green button "Book the room, pay on arrival",
- a
<dialog>confirmation (a small window on top of the page), - the toast "Booking sent" (a short message that appears for a moment).
Its colours: green #15803d (#166534 on hover), text #1f2937, secondary text #475569, lines #e2e8f0.
When and why a moment needs animation is covered in the lesson Microinteractions & Animation in the UI/UX track. This track covers how, down to the numbers. The table below lists all the animations on that page. Durations are in ms (milliseconds). Easing is the speed pattern of a movement: ease-out slows down at the end, ease-in is slow at the start, and ease-in-out is slow at the start and at the end.
| Moment | What moves | Duration | Easing |
|---|---|---|---|
| Night button pressed | transform: scale(0.96) (the button shrinks a little) |
100 ms | ease-out |
| Night button selected | background-color becomes #15803d |
150 ms | ease-out |
| Total price changes | the new number rises 4 px as it appears | 200 ms | ease-out |
| Room card enters | opacity 0 to 1, rises 16 px |
300 ms | ease-out |
| Confirmation dialog | open: opacity 0 to 1, scale(0.96) to 1 |
open 200 ms, close 150 ms | open ease-out, close ease-in |
| Toast "Booking sent" | rises from the bottom, shows for 5 seconds | in 200 ms, out 150 ms | in ease-out, out ease-in |
| Room photo gallery | slides one photo | 300 ms | ease-in-out |
Five families of animation
Take one animation from the table, the Terrace Room card entering: 300 ms, ease-out, opacity from 0 (see-through) to 1 (fully visible) while rising 16 px. This animation can be written five ways. Two terms first: a state is the condition an element is in (for example, before and after a click), and a library is ready-made code written by other people. The first two ways use CSS:
/* 1. CSS transition: moves between two states when the value changes */
.card { opacity: 0; transform: translateY(16px);
transition: opacity 300ms ease-out, transform 300ms ease-out; }
.card.in { opacity: 1; transform: none; }
/* 2. CSS animation: a @keyframes script that runs by itself once its rule applies */
@keyframes card-in { from { opacity: 0; transform: translateY(16px); } }
.card { animation: card-in 300ms ease-out; }
In that code, translateY(16px) means the card is moved 16 px down. Way 2 uses @keyframes, a list of keyframes (states at given points in time). Here there is only a start keyframe (from): the card moves from there to its usual style. The next three ways use JavaScript:
// 3. Web Animations API: the same keyframes from JavaScript, and it can pause and reverse
const anim = card.animate(
[{ opacity: 0, transform: 'translateY(16px)' }, { opacity: 1, transform: 'none' }],
{ duration: 300, easing: 'ease-out' }
);
// 4. requestAnimationFrame: you compute the value in every frame yourself
const start = performance.now();
function frame(now) {
const t = Math.min((now - start) / 300, 1); // time, 0 to 1
const p = 1 - (1 - t) ** 2; // slows down at the end, like ease-out
card.style.opacity = p;
card.style.transform = `translateY(${16 * (1 - p)}px)`;
if (t < 1) requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
// 5. A library (Motion, say): one call, with the Web Animations API or rAF underneath
animate(card, { opacity: [0, 1], transform: ['translateY(16px)', 'none'] }, { duration: 0.3, ease: 'easeOut' });
Here, card is the card element, and rAF is short for requestAnimationFrame. Each way is called a family, has its own lesson in this track, and fits a different need:
| Family | Fits | On Mrs. Sri's page |
|---|---|---|
| CSS transition | two states triggered by a change: hover, focus, a class, an attribute | the night buttons, the Book button's colour |
CSS animation (@keyframes) |
more than two steps, repeating, or running by itself on appearing | the toast, the "Booking..." dots, the card entering |
| Web Animations API | needs control: pause, reverse, knowing when it ends, values from data | a dialog cancelled halfway |
requestAnimationFrame / canvas |
values you calculate every frame: physics, running numbers, canvas | the total counting up |
| Libraries (Motion, GSAP, Lottie) | long sequences, gestures, React component animation, After Effects files | a photo gallery swiped by finger |
Teachers often group the five families into three pillars: CSS transitions, CSS animations (@keyframes) and JavaScript. The third pillar holds families 3 to 5: the Web Animations API (the browser's official animation commands), requestAnimationFrame, and the libraries built on both.
Frame budget: 16.7 ms or 8.3 ms
A screen shows a new picture dozens of times per second. That number is the refresh rate, measured in Hz: a 60 Hz screen shows 60 pictures per second. One second = 1,000 ms, so the time for one frame is 1,000 divided by the refresh rate:
| Screen | Time per frame | A 300 ms card takes |
|---|---|---|
| 60 Hz | 1,000 / 60 = 16.7 ms | 18 frames |
| 90 Hz | 1,000 / 90 = 11.1 ms | 27 frames |
| 120 Hz | 1,000 / 120 = 8.3 ms | 36 frames |
For example, Mr. Hadi opens the Terrace Room card on a 60 Hz phone, and Mrs. Sri on a 120 Hz phone. The animation takes 300 ms on both phones, because a duration is measured in time, not in frames. Mrs. Sri's phone draws 36 frames, so the motion looks smoother. But the browser on that phone has only 8.3 ms to prepare each frame. This amount of time per frame is called the frame budget.
When the browser's work for one frame takes longer than the frame budget, that frame is not shown in time (a dropped frame) and the motion stutters. That stutter is called jank. The web.dev article "Rendering performance" gives the 60 Hz budget as 16.66 ms. It advises finishing your code's work in about 10 ms, because the browser needs time for its own work too.
Five steps to draw one frame
For each frame, the browser can go through five steps (web.dev calls them the pixel pipeline):
- JavaScript: running code that changes something, such as adding the class
into the card. - Style: working out which CSS rules apply to which element.
- Layout: working out the position and size of every element. If the card grows taller, every element below it is calculated again too.
- Paint: colouring the pixels for text, backgrounds and shadows. The result is kept in one or more layers.
- Composite: putting those layers together into one picture on screen.
The property you animate decides which steps repeat in every frame:
| Property | Repeated every frame |
|---|---|
top, left, width, height, margin |
Style, Layout, Paint, Composite |
box-shadow, color, border-color |
Style, Paint, Composite |
transform, opacity |
Composite only |
The fewer steps repeat, the lighter the browser's work in each frame. We measured the difference in Chromium 147 with a Performance trace (a tool that records the browser's work), while the card rose 16 px over 300 ms. With top, layout ran 35 to 37 times and paint 70 to 72 times. With transform, layout ran only once and paint twice, both only at the start. The details are in the lesson Transform & Composite.
The browser divides its work between several threads (lines of work that run at the same time). JavaScript and layout run on the main thread. The composite step runs on another thread, the compositor thread. transform and opacity animations from CSS or the Web Animations API can run entirely on the compositor thread, without waiting for the main thread.
We tested this in Chromium 147, on a 120 Hz screen: we kept the main thread busy for 500 ms in the middle of an animation. The CSS transition and the Web Animations API both still drew 60 frames (one frame every 8.3 ms for 500 ms). The requestAnimationFrame version drew only 1 frame: its motion froze, then jumped.
CSS or JavaScript?
Start with CSS. Move to JavaScript when the animation needs one of these three things:
- Dynamic logic: the value is only known while the page runs. Examples are a target position calculated from data, or a total counting up from $70 to $105.
- Complex interaction: for example, a photo gallery that follows the finger swiping it.
- Playback control: pausing (
pause()), changing direction (reverse()), jumping to a given time, or waiting for the animation to end (anim.finished) before the next step.
These three reasons have nothing to do with threads. In fact requestAnimationFrame always runs on the main thread, so its animation stutters when the main thread is busy, as the test above shows.
prefers-reduced-motion from day one
Some people get dizzy or nauseous when they see motion on screen. They turn on "Reduce motion" in their phone or computer settings. The browser passes that choice on as the media query (a condition in CSS) prefers-reduced-motion: reduce. According to MDN, this media query is supported since Chrome 74, Firefox 63 and Safari 10.1. Respect that choice from your first animation: remove the movement, and keep a short fade. In CSS, you do it like this:
@media (prefers-reduced-motion: reduce) {
.card { transform: none; transition: opacity 150ms linear; }
}
And like this in JavaScript, with the Web Animations API:
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
card.animate(
reduce ? [{ opacity: 0 }, { opacity: 1 }]
: [{ opacity: 0, transform: 'translateY(16px)' }, { opacity: 1, transform: 'none' }],
{ duration: reduce ? 150 : 300, easing: 'ease-out' }
);
- In CSS, the rules inside
@mediareplace the usual movement rules. The card doesn't rise 16 px, it only fades in over 150 ms. - In JavaScript,
matchMedia(...).matchesistruewhen "Reduce motion" is on. The code above then uses keyframes with no movement and a duration of 150 ms. - Don't replace the rule above with
transition: none: code that waits for the transition to end then never runs, as the lesson Microinteractions & Animation shows.
⚠️ Missteps in a first animation
- Counting motion per frame, not per time. The code
x += 2in every frame covers 300 px in 2.5 seconds on a 60 Hz screen, but in only 1.25 seconds on a 120 Hz screen. Calculate the position from the time that has passed, like(now - start) / 300. - Animating
top,widthormargin. Layout is calculated again in every frame. Usetransformfor movement and size, andopacityfor appearing and disappearing. - Starting with JavaScript. Hover, focus and a simple appearance only need CSS: the code is shorter, and it can run on the compositor thread.
- Forgetting
prefers-reduced-motion. Keep a short fade for people who turn on "Reduce motion".
TL;DR: Web animation is a CSS value that changes a little at a time and is drawn again many times a second. One picture is called a frame. There are five families (ways to write an animation): CSS transitions (moving between two states), CSS animations with @keyframes (a script of steps that runs by itself), the Web Animations API (the same script from JS, with pause and reverse), requestAnimationFrame or rAF (you calculate the value for every frame yourself), and libraries (ready-made code). In short, three pillars: transitions, @keyframes, and JavaScript. The frame budget is the time allowed for one frame: 1,000 ms divided by the screen's refresh rate (frames per second), which gives 16.7 ms at 60 Hz and 8.3 ms at 120 Hz. A 300 ms animation stays 300 ms on both screens, and only its frame count differs (18 or 36). Every frame can go through JavaScript, Style, Layout (position and size), Paint (colouring pixels) and Composite (putting layers together into one picture). transform and opacity animations repeat only Composite. They can run on the compositor thread (a separate line of work) while the main thread, where JavaScript runs, is busy. rAF runs on the main thread, so it stutters when that thread is busy. Use JavaScript when you need dynamic logic (values calculated while the page runs), complex interaction or playback control (pause, reverse). Respect prefers-reduced-motion (the user's "Reduce motion" setting) from the start: remove the movement, keep a fade.
⚡ The three pillars of web animation
Write:
.card { opacity: 0; transform: translateY(16px);
transition: opacity 300ms ease-out, transform 300ms ease-out; }
.card.in { opacity: 1; transform: none; }
Meaning: This code makes a card (.card) enter in 300 ms: from see-through and 16 px lower, to fully visible in its place. This is the first pillar, a CSS transition: a value moves between two states when the class in is added. The second pillar is @keyframes, a script of steps that runs by itself. The third pillar is JavaScript: the Web Animations API (animation commands built into the browser), requestAnimationFrame (you calculate the value for each picture yourself), and libraries (ready-made code).
⚡ Frame budget
Guess first: a 120 Hz screen shows 120 pictures (frames) per second, a 60 Hz screen only 60. Does a 300 ms animation finish sooner on a 120 Hz phone?
No. The animation stays 300 ms, because a duration is measured in time. Only the number of frames differs: 18 at 60 Hz, 36 at 120 Hz.
Meaning: The frame budget is the time the browser has for one frame: 1,000 ms divided by the refresh rate (the number of frames per second). That gives 16.7 ms at 60 Hz and 8.3 ms at 120 Hz. When the browser's work takes longer than that budget, the frame is not shown in time and the motion stutters. That stutter is called jank.
⚡ Composite only
Guess first: in every frame of an animation, the browser may repeat the steps Layout (working out position and size), Paint (colouring pixels) and Composite (putting layers together into one picture). Which only need Composite: `top`, `width`, `transform` or `opacity`?
transform and opacity. The properties top, left, width and margin repeat Layout and Paint in every frame.
Meaning: The browser works on several threads (lines of work): JavaScript on the main thread, the Composite step on the compositor thread. transform and opacity animations from CSS can run on the compositor thread, so they stay smooth while the main thread is busy. requestAnimationFrame always runs on the main thread, so it stutters when that thread is busy.
⚡ Respect "Reduce motion"
Write:
@media (prefers-reduced-motion: reduce) {
.card { transform: none; transition: opacity 150ms linear; }
}
Meaning: People who get dizzy from motion on screen turn on "Reduce motion" in their device settings. The rules inside this @media apply only to them: the card only fades in over 150 ms, without rising 16 px.