Why page speed matters
We all want things straight away. If a page takes too long to load, there's a good chance visitors will give up and look elsewhere. Speed also matters for search engines: Google uses page experience, including loading speed, as one of the signals for ranking websites.
Google measures this with a set of scores called Core Web Vitals. CSS affects all three:
| Measure | What it means | Good score |
|---|---|---|
| LCP (Largest Contentful Paint) | How long until the main content, such as a big image or heading, appears | 2.5 seconds or less |
| CLS (Cumulative Layout Shift) | How much the page jumps around while it loads | 0.1 or less |
| INP (Interaction to Next Paint) | How quickly the page responds when you click or tap | 200 milliseconds or less |
As this site is dedicated to CSS, we'll focus on what you can do with your stylesheets. Other important techniques, such as caching, using a CDN and optimising large photos, are topics for another day.
Why CSS matters so much: CSS is render-blocking. Before a browser shows anything on screen, it downloads and reads all the CSS in your page's <head>. Until then, visitors see a blank page. Smaller, smarter CSS means they see your content sooner.
Tip 1: Minify and compress your CSS
Minifying means removing everything the browser doesn't need: spaces, line breaks and comments. Most of us write CSS with plenty of spacing so it's easy to read:
/* Main button styles */
.button {
padding: 10px 20px;
background-color: #ffffff;
border-radius: 6px;
}
Minified, the same rule becomes:
.button{padding:10px 20px;background-color:#fff;border-radius:6px}
Keep two versions
The original version of this post suggested writing your CSS on one line to save space. Please don't! Code that's hard to read is hard to fix. Instead, keep a nicely formatted version to work on, and publish a minified copy. Our CSS formatter can do this for you, and so can most build tools and WordPress caching plugins.
Turn on compression
Even more effective than minifying is compression. Your web server can squeeze files with Gzip or Brotli before sending them, and the browser unpacks them automatically. Text files like CSS often shrink by 70 to 80%.
Most hosting companies switch this on by default. To check, open your browser's developer tools, go to the Network tab, click your CSS file and look for content-encoding: br or gzip in the response headers.
Small habits: shorthand and short hex
Shorthand properties combine several lines into one, as explained in our earlier post on CSS shorthand properties:
/* Four separate properties */
padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;
/* One shorthand property */
padding: 10px 20px 30px 40px;
Similarly, colours where each pair of hex digits matches can be shortened from six digits to three, so #ffffff becomes #fff.
Correction: the original post said about 48 colours can be shortened to three digits. In fact there are 4,096 of them: every colour where the red, green and blue pairs each repeat a digit, such as #aabbcc (#abc).
These habits make code tidier, but once a file is minified and compressed, the speed difference is tiny. Use them because they make your CSS easier to read, not to save bytes.
Tip 2: Load CSS the smart way
Use link, not @import
There are two ways to load one stylesheet from another. @import inside a CSS file makes the browser wait: it can't discover the second file until it has downloaded and read the first. <link> tags in your HTML let it fetch all files at the same time.
<!-- Better: both files download at once -->
<link rel="stylesheet" href="/css/base.css">
<link rel="stylesheet" href="/css/layout.css">
/* Slower: layout.css can't start until base.css has loaded */
@import url("layout.css");
Mark stylesheets that aren't always needed
A print stylesheet is only needed when someone prints. Add a media attribute and the browser will still download it, but won't hold up the page waiting for it:
<link rel="stylesheet" href="/css/print.css" media="print">
Inline your critical CSS
Critical CSS is the small set of styles needed to show the top part of the page, the bit visible before anyone scrolls. Putting those styles directly in a <style> tag in your <head> lets the browser draw that part immediately, without waiting for a separate file.
<head>
<style>
/* Just enough CSS for the header and first section */
body { margin: 0; font-family: system-ui, sans-serif; }
.site-header { padding: 16px 20px; background: #1d4ed8; color: #fff; }
</style>
<link rel="stylesheet" href="/css/main.css">
</head>
Tip: keep inlined CSS small, ideally just a few kilobytes. It's sent with every page, so inlining your whole stylesheet would make each page heavier. Tools such as Critical, or caching plugins for WordPress, can work out your critical CSS automatically.
Tip 3: Remove CSS you don't use
Over time, stylesheets collect rules for features that no longer exist. CSS frameworks and themes often include styles for hundreds of components you'll never use. All of it still has to be downloaded and read.
Find unused CSS with Coverage
Chrome and Edge have a built-in tool for this:
- Open developer tools (right-click the page and choose Inspect).
- Press Ctrl + Shift + P (or Cmd + Shift + P on a Mac), type "coverage" and choose Show Coverage.
- Click the reload button in the Coverage panel.
- Each file shows a bar: red is code that wasn't used on this page.
Good to know: "unused on this page" doesn't always mean "unused everywhere". A rule may be needed on another page, or for a menu that only opens when clicked. Check a few pages before deleting anything.
If you use a framework such as Tailwind or Bootstrap, look for its built-in option to remove unused styles when you build your site. It can reduce a framework's CSS from hundreds of kilobytes to just a few.
Tip 4: Handle small images wisely
The original version of this post recommended two tricks for small images: converting them to data URIs, and combining them into sprite sheets. Both aimed to cut the number of separate files the browser had to request. Here's how they stand today.
Why the rules changed
When this post was written, browsers could only fetch a handful of files at a time from each website, so every extra request caused a delay. Modern websites use HTTP/2 or HTTP/3, newer versions of the system browsers use to talk to servers. These can download many files at once over a single connection, so lots of small files are no longer a big problem.
Data URIs: only for tiny images
A data URI turns an image into a long string of text that sits directly in your CSS. Our image to data URI converter will create one for you:
.icon {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' ...%3E");
}
This still works well for very small images, under about 1 to 2 KB, such as a tiny icon used on every page. For anything larger, avoid it. Data URIs are about a third bigger than the original image, and because they sit inside your CSS, they delay the whole stylesheet, which delays the whole page.
Sprite sheets: rarely needed now
A sprite sheet combines many small images into one file, with CSS showing just the part you need. Our CSS sprite generator still makes these, and they can help on older servers that don't support HTTP/2. On most modern sites, though, separate image files are just as fast and much easier to manage.
The modern choice: SVG for icons
For icons and simple graphics, SVG (Scalable Vector Graphics) is usually best. SVG files are tiny, stay sharp at any size, and can be coloured with CSS. You can even place them straight into your HTML, so they need no extra download at all.
| Technique | Use it for |
|---|---|
| SVG files or inline SVG | Icons, logos and simple illustrations |
| Data URI | Tiny decorative images used everywhere, under about 2 KB |
| Sprite sheet | Older servers without HTTP/2, or games with many small frames |
| Regular image files (WebP or AVIF) | Photos and anything larger |
Tip 5: Load web fonts efficiently
Custom fonts are often among the largest files on a page, and they can leave text invisible while they download. A few CSS settings make a big difference:
@font-face {
font-family: "CustomFont";
src: url("/fonts/customfont.woff2") format("woff2");
font-weight: 400;
font-display: swap;
}
- Use WOFF2, the smallest web font format, supported by every modern browser.
- Add
font-display: swap, so visitors see text in a backup font straight away instead of a blank space. - Load fewer files. Every weight and style is a separate download. Two or three is plenty for most sites.
- Preload your main font so the browser starts downloading it immediately.
<link rel="preload" href="/fonts/customfont.woff2"
as="font" type="font/woff2" crossorigin>
We cover all of this in detail in our post on fonts with the CSS @font-face rule.
Tip 6: Stop the page jumping around
Have you ever gone to tap a link, only for an image to load and push it down the page just before your finger lands? That's layout shift, and it's what the CLS score measures. It usually happens because the browser doesn't know how big an image will be until it arrives.
Give images their size
The simplest fix is to include width and height attributes on your images. The browser uses them to reserve the right amount of space before the image loads:
<img src="photo.jpg" width="1200" height="800" alt="A mountain lake">
Then add this CSS so images still shrink to fit small screens. The browser keeps the correct shape using the width and height you gave it:
img {
max-width: 100%;
height: auto;
}
Reserve space with aspect-ratio
For videos, embeds and image containers, the aspect-ratio property reserves space based on a shape rather than fixed sizes. The box below always stays in a 16 by 9 shape, however wide it is, so nothing moves when its content arrives:
.video-wrapper {
aspect-ratio: 16 / 9;
width: 100%;
background: #e5e7eb;
}
Text above the video.
Text below stays put while the video loads.
Tip: web fonts can cause small shifts too, when the backup font swaps to your custom one. Choosing a backup font of a similar size helps keep text steady.
Tip 7: Help the browser draw faster
Animate transform and opacity
When you animate properties like width, height, top or margin, the browser has to recalculate the layout of the page on every frame, which can make animations stutter. transform and opacity can be animated without touching the layout, so they stay smooth even on slower phones.
/* Can stutter: changes the layout every frame */
.panel:hover { margin-top: -10px; }
/* Smooth: moves the drawn element only */
.panel:hover { transform: translateY(-10px); }
Skip work for off-screen content
The content-visibility property lets the browser skip drawing sections that are below the visible part of the screen until the visitor scrolls near them. On long pages, this can noticeably speed up the first load:
.article-section {
content-visibility: auto;
contain-intrinsic-size: auto 600px; /* an estimated height, to keep the scrollbar steady */
}
contain-intrinsic-size gives the browser an estimated height for each skipped section. Without it, skipped sections would count as zero height and the scrollbar would jump around as you scroll.
Good to know: content-visibility works in all current major browsers. Browsers that don't support it simply ignore it, so it's safe to add.
Measure before and after
Speed improvements are easy to guess at and easy to get wrong, so measure them. Two free tools will test your page and point out what's slowing it down:
- PageSpeed Insights (pagespeed.web.dev) tests any public page and shows its Core Web Vitals.
- Lighthouse is built into Chrome's developer tools, under the Lighthouse tab, and works on any page you can open.
Run a test, make one change, then test again. That way you'll know which changes really helped.
Wrapping up
The biggest CSS speed wins today come from sending less CSS and sending it smartly: minify and compress it, load it with <link> tags, inline the critical part, and remove what you don't use. Then make the page feel fast by loading fonts efficiently, reserving space for images and animating with transform and opacity. A few small changes can make a real difference to both your visitors and your search rankings.