Sub topic 13
Keeping mobile pages fast
This chapter assumes you know basic HTML, the CSS cascade, and how to use browser developer tools. It also assumes you have read the earlier chapters on the viewport, min-width queries and responsive images.
Why mobile-first helps performance
Mobile devices often have slower processors and unreliable networks. Mobile-first CSS helps because small screens get the simple rules first. Wider screens add rules on top. A phone never has to apply a desktop layout and then undo it.
Performance work in CSS mostly comes down to three things: sending fewer bytes, avoiding layout shift, and not blocking the first paint.
Send the right image size
Images are usually the heaviest part of a page. In the earlier chapter on flexible media you saw srcset and sizes. They let the browser pick a file that matches the screen, so a phone does not download a 1600-pixel image.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 48em) 50vw, 100vw"
width="800" height="600"
alt="A hiker on a mountain ridge">The width and height attributes do a second job. The browser uses them to work out the aspect ratio before the file arrives. It reserves that space, so the text below does not jump when the image loads. That jump is called layout shift. With height: auto in your CSS, the image still scales correctly.
img {
max-width: 100%;
height: auto;
}Load only what is needed
Images far down the page do not need to load right away. Add loading="lazy" to them so the browser fetches them as the user scrolls. Do not use it on the main image at the top of the page. That image should load as early as possible.
<img src="gallery-1.jpg" width="400" height="300" loading="lazy" alt="Gallery photo">Keep the CSS small
CSS blocks rendering. The browser will not paint the page until it has downloaded and parsed the stylesheets in the <head>. Three habits reduce this cost.
- Remove rules you no longer use.
- Minify the file for production, which strips whitespace and comments.
- Avoid long chains of
@import, because each one adds another request.
Because mobile-first base styles are short, small screens parse less before they paint. Keep the larger layout rules inside their min-width queries so the intent stays clear.
Cheap rules and expensive rules
Some CSS is cheap for the browser to animate and some is not. Changing transform and opacity is usually smooth, because the browser can often handle them without recalculating the layout. Animating width, height or margin forces the browser to recompute the layout on every frame. On a low-end phone, that can make the animation stutter.
.menu {
transform: translateY(-100%);
transition: transform 200ms ease-out;
}
.menu.is-open {
transform: translateY(0);
}
@media (prefers-reduced-motion: reduce) {
.menu {
transition: none;
}
}The last rule respects users who asked their system to reduce motion. Performance and accessibility often point the same way.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Missing `width` and `height` on images | Layout shifts as images load | Add both attributes and keep `height: auto` in CSS |
| Lazy-loading the top image | The most important image loads late | Remove `loading="lazy"` from it |
| One large image for all screens | Phones download too many bytes | Use `srcset` and `sizes` |
| Animating `width` or `margin` | Janky motion on slow devices | Animate `transform` or `opacity` |
Exercise
A phone user scrolls through your page and the text jumps down when each photo loads. Name the cause and give the fix.
Show answer
The images have no reserved space, so the browser does not know their size until they download. Add width and height attributes that match the image's aspect ratio, and keep max-width: 100%; height: auto; in the CSS.
Exercise
Why is animating transform usually a better choice than animating margin-left for a sliding menu?
Show answer
Changing transform usually avoids recalculating the page layout, so the animation stays smooth on slow devices. Animating margin-left forces a layout recalculation on every frame.