Sub topic 12
Keeping pages light on mobile connections
This chapter assumes you can write semantic HTML and basic CSS, and that you know how to open your browser's developer tools and read the Network panel. It builds on the earlier chapters about the single-column layout and responsive images. The goal here is to treat page weight as a design constraint that you set before you build, not something you fix afterwards.
Why weight is a design decision
A phone on a mobile connection differs from a desktop on broadband in three ways that matter for page weight:
- Bandwidth varies. A user might have a fast 5G connection at one moment and a weak signal on a train the next.
- Latency is high. Each new request pays a round-trip delay before any data arrives. Many small requests can cost more than one larger request.
- The processor is slower. Downloading a file is only part of the cost. The phone must also parse, compile and run JavaScript, and decode images. A script that is cheap on a laptop can freeze a mid-range phone for seconds.
Mobile-first design already helps. When you rank content by user need, you decide what the first screen must contain. A performance budget extends that decision to bytes and requests: each item on the page has to earn its cost.
What a performance budget is
A performance budget is a set of limits that the page must stay within. Typical limits are:
- Total transferred size, for example 500 KB for the first load.
- Size of JavaScript, for example 100 KB compressed.
- Number of requests.
- A timing target, for example the main content visible within a few seconds on a mid-range phone over a throttled connection.
The numbers above are examples. Pick limits from your own audience and measurements, then write them down so that the team can check changes against them.
The budget works best when it is set early. Compare two ways of working:
| Approach | What happens | Result |
|---|---|---|
| Add features, then optimise | Each feature arrives with its own scripts, fonts and images | Cutting later means removing work people already like |
| Set a budget first | Each addition must fit or replace something | Trade-offs are made while they are still cheap |
Where the bytes usually go
Before you optimise anything, measure. In most pages, the weight sits in a few places:
- Images. Usually the largest share. The responsive-image techniques from the previous chapter apply directly.
- JavaScript. Costly in two ways: transfer size and execution time.
- Web fonts. Each weight and style is a separate file.
- CSS. Normally small, but a large framework loaded for a few rules can add up.
- Third-party code. Analytics, chat widgets and ad scripts often add requests you do not control.
Images: send less, and send it later
Images are the first place to look. Three habits cover most of the gain.
Send the right size. The srcset and sizes attributes let the browser choose the smallest file that fits. A phone showing an image at 360 CSS pixels wide should not download a 1600-pixel file.
Use an efficient format. Modern formats such as WebP and AVIF usually give smaller files than JPEG at similar quality. You can offer them with a picture element and let older browsers fall back to JPEG.
<picture>
<source type="image/avif" srcset="team-400.avif 400w, team-800.avif 800w" sizes="100vw">
<source type="image/webp" srcset="team-400.webp 400w, team-800.webp 800w" sizes="100vw">
<img
src="team-800.jpg"
srcset="team-400.jpg 400w, team-800.jpg 800w"
sizes="100vw"
width="800"
height="450"
alt="The support team standing in front of the office"
loading="lazy">
</picture>The browser takes the first source whose type it supports. The img element is both the fallback and the element that carries alt, width, height and loading.
Defer what is off screen. loading="lazy" tells the browser not to fetch an image until the user scrolls near it. Do not apply it to the main image visible at the top of the page. Lazy loading that image delays the most important content, which is the opposite of what you want.
JavaScript: the most expensive byte
A kilobyte of JavaScript costs more than a kilobyte of image, because the phone must run it. A mobile-first page should work before any script runs, then improve once scripts load. This is progressive enhancement, and it matches the mobile-first idea of starting from the simplest version.
Practical steps:
- Use native features first. The
detailselement from the progressive disclosure chapter needs no script. A CSS-only layout needs none either. - Load scripts without blocking. Add
deferso the HTML is parsed without waiting for the script. - Load only what the page needs. Split code so that a feature used on one page is not shipped to every page.
- Check dependencies. A large library imported for one small function is a common source of weight. Look at what you import and what it costs.
<script src="/js/menu.js" defer></script>A script with defer downloads in parallel with parsing and runs after the document has been parsed, in the order the scripts appear. A script with async runs as soon as it arrives, in any order, which suits independent scripts such as analytics.
Fonts: fewer files, safer loading
Each font weight and style is a separate download. A page that uses regular, italic, bold and bold italic in one family loads four files before it looks as designed. Options for reducing this:
- Use only the weights you need. Two is often enough.
- Consider a system font stack for body text. It costs no download at all.
- Use a variable font if you need many weights, since one file can cover a range.
- Subset fonts to the character sets you actually use.
- Set
font-displayso text is visible while the font loads.
@font-face {
font-family: "Body Sans";
src: url("/fonts/body-sans-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
body {
font-family: "Body Sans", system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
}With font-display: swap, the browser shows text in the fallback font straight away and swaps in the web font when it arrives. Text stays readable instead of staying invisible. The swap can shift the layout slightly, so choose a fallback font with similar proportions.
The WOFF2 format is compressed and supported by current browsers, so it is the usual choice.
Compression and caching
Some savings come from the server, not the page code.
- Compress text responses. HTML, CSS, JavaScript and SVG compress well with gzip or Brotli. Check that your server has this turned on. In the Network panel, compare the "size" and "transferred" figures. If they are equal for a text file, it is probably not compressed.
- Set cache headers. Files that rarely change, such as fonts and versioned scripts, can be cached for a long time. A returning visitor then downloads almost nothing. Use file names that change when the content changes, such as
app.3f9a1c.js, so that a long cache lifetime is safe. - Reduce requests that block rendering. CSS in the head blocks rendering until it loads. Keep it small, and avoid long chains where one file imports another.
Mobile-first CSS helps weight
The mobile-first approach also reduces CSS work. Base rules apply to everyone, and min-width queries add rules for larger screens. A phone parses the base styles and skips the matching work for queries that do not apply.
A related trap is hiding content with display: none on small screens. The browser may still download a hidden image or run a hidden script. If a large image is only for desktop, serve it through picture or srcset so a phone never requests it. Hiding is not the same as not loading.
/* Avoid: the image file may still download on a phone */
.hero-desktop {
display: none;
}
@media (min-width: 64em) {
.hero-desktop {
display: block;
}
}Common errors
| Mistake | Why it hurts | Fix |
|---|---|---|
| Lazy loading the top image | The most important content arrives late | Remove `loading="lazy"` from images visible at load |
| Omitting `width` and `height` | Layout shifts as images load | Always set both |
| Hiding desktop assets with `display: none` | The files may still download | Use `picture` or `srcset` |
| Loading four or more font files | Delay before text looks right | Limit weights, use `font-display` |
| Testing only on fast office Wi-Fi | Problems stay invisible | Throttle the network in DevTools |
| Adding third-party scripts without review | Costs you cannot control | Review each one against the budget |
Checking your work
Open the Network panel in your browser's developer tools, then follow these steps:
- Choose a throttling profile such as "Fast 4G" or "Slow 4G", and turn on cache disabling to see a first visit.
- Reload and read the total transferred size and number of requests at the bottom of the panel.
- Sort by size to find the largest files.
- Compare the totals with your budget.
- Repeat after each change so you know which change helped.
Throttling in the browser is an approximation. The next chapter covers real-device testing, which also shows the processor cost that throttling cannot reproduce.
Exercise
A product page has a large banner image at the top of the page, five font files, and one analytics script. The page is slow on a phone. List three changes you would make, and say whether loading="lazy" belongs on the banner.
Show answer
Possible changes: serve the banner through srcset and sizes (and a modern format such as WebP or AVIF) so a phone downloads a small file; reduce the fonts to the two weights actually used, set font-display: swap, and serve WOFF2; add defer or async to the analytics script so it does not block parsing, and check whether it is needed at all. The banner is visible at the top on load, so it should not have loading="lazy". Lazy loading would delay the most important content. Keep width and height on it to avoid layout shift.
Exercise
A developer hides a 2 MB desktop-only photo with display: none below 64em and says phones are now safe. Why might this be wrong, and what is a better approach?
Show answer
Hiding an element with CSS does not reliably stop the browser from downloading an image that the HTML references. The phone may still fetch the 2 MB file. A better approach is to avoid requesting it on small screens, for example with a picture element whose source has a media="(min-width: 64em)" attribute, or to load the large image as a CSS background only inside a min-width media query.