In this article, we look at 20 ways to optimize your CSS so that it’s faster-loading, easier to work with and more efficient.
According to the latest HTTP Archive reports, the web remains a bloated mess with the mythical median website requiring 1,700Kb of data split over 80 HTTP requests and taking 17 seconds to fully load on a mobile device.
The Complete Guide to Reducing Page Weight provides a range of suggestions. In this article, we’ll concentrate on CSS. Admittedly, CSS is rarely the worst culprit and a typical site uses 40KB spread over five stylesheets. That said, there are still optimizations you can make, and ways to change how we use CSS that will boost site performance.
1. Learn to Use Analysis Tools
You can’t address performance problems unless you know where the faults lie. Browser DevTools are the best place to start: launch from the menu or hit F12, Ctrl + Shift + I or Cmd + Alt + I for Safari on macOS.
All browsers offer similar facilities, and the tools will open slowly on badly-performing pages! However, the most useful tabs include the following …
The Performance tab analyses browser processes. Start recording, run an activity such as a page reload, then stop recording to view the results. Look for:
- Excessive layout/reflow actions where the browser has been forced to recalculate the position and size of page elements.
- Expensive paint actions where pixels are changed.
- Compositing actions where the painted parts of the page are put together for displaying on-screen. This is normally the least processor-intensive action.
Chrome-based browsers provide an Audits tab which runs Google’s Lighthouse tool. It’s often used by Progressive Web App developers, but also makes CSS performance suggestions.
Alternatively, use online analysis tools that are not influenced by the speed and capabilities of your device and network. Most can test from alternative locations around the world:
2. Make Big Wins First
CSS is unlikely to be the direct cause of performance issues. However, it may load heavy-hitting assets which can be optimized within minutes. Examples:
- Activate HTTP/2 and GZIP compression on your server
- Use a content delivery network (CDN) to increase the number of simultaneous HTTP connections and replicate files to other locations around the world
- Remove unused files.
Images are normally the biggest cause of page bulk, yet many sites fail to optimize effectively:
- Resize bitmap images. An entry-level smartphone will take multi-megapixel images that can’t be displayed in full on the largest HD screen. Few sites will require images of more than 1,600 pixels in width.
- Ensure you use an appropriate file format. Typically, JPG is best for photographs, SVG for vector images, and PNG for everything else. You can experiment to find the optimum type.
- Use image tools to reduce file sizes by striping metadata and increasing compression factors.
That said, be aware that xKb of image data is not equivalent to xKb of CSS code. Binary images download in parallel and require little processing to place on a page. CSS blocks rendering and must be parsed into an object model before the browser can continue.
3. Replace Images with CSS Effects
It’s rarely necessary to use background images for borders, shadows, rounded edges, gradients and some geometric shapes. Defining an “image” using CSS code uses considerably less bandwidth and is easier to modify or animate later.
4. Remove Unnecessary Fonts
Services such as Google Fonts make it easy to add custom fonts to any page. Unfortunately, a line or two of code can retrieve hundreds of kilobytes of font data. Recommendations:
- Only use the fonts you need.
- Only load the weights and styles you require — for example, roman, 400 weight, no italics.
- Where possible, limit the character sets. Google fonts allows you to pick certain characters by adding a
&text=value to the font URL — such as
fonts.googleapis.com/css?family=Open+Sans&text=SitePonfor displaying “SitePoint” in Open Sans.
- Consider variable fonts, which define multiple weights and styles by interpolation so files are smaller. Support is currently limited to Chrome, Edge, and some editions of Safari but should grow rapidly. See How to Use Variable Fonts.
- Consider OS fonts. Your 500Kb web font may be on-brand, but would anyone notice if you switched to the commonly available Helvetica or Arial? Many sites use custom web fonts, so standard OS fonts are considerably less common than they were!
@import at-rule allows any CSS file to be included within another. For example:
/* main.css */ @import url("base.css"); @import url("layout.css"); @import url("carousel.css");
This appears a reasonable way to load smaller components and fonts. It’s not.
@import rules can be nested so the browser must load and parse each file in series.
<link> tags within the HTML will load CSS files in parallel, which is considerably more efficient — especially when using HTTP/2:
<link rel="stylesheet" href="base.css"> <link rel="stylesheet" href="layout.css"> <link rel="stylesheet" href="carousel.css">
That said, there may be more preferable options …