How to Minify CSS and Speed Up Your Website

Jamie Juviler
Jamie Juviler

Published:

When you need to speed up your website, you may hear suggestions to minify CSS. It's no secret that CSS is a handy tool that website owners can use to style pages and content precisely as desired — but too much of it can sometimes mean trouble regarding page load times. Large CSS files can weigh down your site when used excessively, slowing performance and sending visitors elsewhere.

Person using a computer to minify CSS code

Download Now: Free HTML & CSS Hacks

It helps to balance engagement and speed to compete with the rapidly increasing number of impressive technical sites. Doing that might seem tricky, but worry not: there are many quick wins and best practices to boost load times while keeping your design vision intact. 

One of the best? You guessed it — choosing minification (and yes, it's different from deciding to compress CSS!).

So, what exactly do we mean by "unneeded code?" With CSS (and most other coding languages), developers have introduced formatting and syntactic best practices to make the code more readable by humans. These effectively boost productivity, assist debugging, and make the CSS code easier to maintain. But they're extra because the browser and target platform do not need them to run. 

Consider the CSS in the example below:

 
/* heading elements */

body {
    color: #33475b
}

h1 {
    font-size: 2rem;
    font-weight: 700;
    line-height: 2.75rem
}

h2 {
    font-size: 1.625rem;
    font-weight: 700;
    line-height: 2.25rem
}

h3 {
    font-size: 1.5rem;
    font-weight: 500;
    line-height: 2.125rem
}

/* other elements */

p, span {
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.75rem
}

blockquote {
    font-size: 1.5rem;
    font-weight: 400;
    line-height: 2.375rem
}

ol, ul {
    list-style: none
}

table {
    border-collapse: collapse;
    border-spacing: 0
}

button {
    appearance: none;
    font: inherit;
    margin: 0
}

You may notice that this code contains spaces, indents, comments, and line breaks. While this makes things more digestible for us, the browser doesn't require any of this information to run the CSS code. While parsing the files, it ignores these extra whitespaces and comments. The result is a CSS file that's larger than it needs to work properly. And, as you may have already guessed, larger files equate to more time and resources required to process the files. 

Consequently, website visitors will get the impression that your website or application is slow — not exactly delivering the exceptional user experience they desire. 

And that's where CSS minification comes in — it removes extra code and results in a smaller CSS file that works identically to the original. Additionally, when you minify CSS, you may alter the code in various ways, such as shortening variable names and deleting redundant or unused information. These efforts aim to shrink the file even more. 

Here's what the CSS example above looks like after minification:

 
body{color:#33475b}h1{font-size:2rem;font-weight:700;line-height:2.75rem}h2{font-size:1.625rem;font-weight:700;line-height:2.25rem}h3{font-size:1.5rem;font-weight:500;line-height:2.125rem}p,span{font-size:1rem;font-weight:400;line-height:1.75rem}blockquote{font-size:1.5rem;font-weight:400;line-height:2.375rem}ol,ul{list-style:none}table{border-collapse:collapse;border-spacing:0}button{appearance:none;font:inherit;margin:0}

It's less readable to us, but it looks (and works) the same to a computer. And the best part? It will help your website improve user experience by boosting load speed to delight visitors. As a marketer, this is always your main priority.

It's possible to apply minification to other languages too. For example, HTML and JavaScript, the other two foundational languages of the web, also follow visual formatting conventions and can be minified.

Why should you minify CSS? 

Website owners mainly choose to minify CSS to increase their page speed. The basic principle is simple: The less code there is to process, the less time it takes to load the web page. This allows you to delight website visitors with fast load times. Almost 70% of consumers state that page speed impacts their willingness to purchase from an online retailer. 

There are two main ways that CSS minification achieves this. First, smaller files take less time and resources to fetch and compress in the origin server, send to the client, and finally download by the browser, simply because there's less information to tackle.

The second reason connects to how browsers render website pages. Once the browser downloads a web page, it parses its HTML file top-to-bottom before displaying it. When the browser runs into a link to an external CSS file, it pauses HTML parsing to process the external CSS code. Users don't see page content until the browser has processed all linked CSS. This is why CSS is called a "render blocking" resource.

No, this isn't necessarily a problem for simple web pages with little CSS. However, with more complex web pages, CSS files are more extensive, and visitors could notice a longer load time. That's why you complete the minification process — and it's especially relevant if your site is visually complex with a lot of CSS. With fewer render-blocking resources to parse, visitors will experience less time to first contentful paint.

In the spirit of transparency, CSS minification won't instantly make your website extremely speedy. But, the chance of a bounce increases 32% from 1 second to 3 seconds of load time. Any slight performance improvement could be the difference between a visitor choosing to stay or go. Faster pages also improve usability for mobile visitors. As of June 2022, over 61% of website traffic comes from mobile, so this could significantly impact your site's success. 

And, of course, you can't ignore how page load time influences your search engine ranking — and the best part? You don't have to alter your page content at all. If we didn't already sell on the perks of choosing to minify CSS, we have a feeling we have now!

What's the difference between compressing and minifying CSS?

Did you know that deciding to compress CSS is different than minifying? While it seems like minify and compress CSS are two synonymous terms, they are two disparate concepts. There are some similarities between the two — both deal with performance optimizations that ultimately lead to size reductions. 

However, minification includes the alteration of code content. As you know, it works by stripping unneeded formatting, characters, and spaces. The result is fewer characters. In contrast, compression might not alter the code content itself. Instead, file size is reduced by compacting it before providing the browser with it when requested. 

How to Minify CSS

Now that you've learned the perks of minifying CSS, let's dive into how you can tackle this on your website. There are several ways to process your CSS that vary in technicality. In this section, we'll cover some different methods, starting with the simplest:

Online Minification Tools

Whether you're looking to minify CSS (or HTML or JavaScript), several websites can help you for free. Some popular options include CSS Minifier and Dan's Tools minifier.

These websites are simple to use and work similarly. Just paste your formatted CSS, toggle any available options, and the website outputs a minified version. Then, copy and paste it into your file, and ta-da! You've successfully minified CSS. 

an online tool to minify CSS

Image Source

These websites do an excellent job of demonstrating how minification works. They're also ideal for smaller projects. However, they don't scale quite well if you're working on a larger project with various files. In that case, you'd have to copy the code manually, paste it into your code editor, and then minify each file. Remember that these tools require internet access, which you might not always have.

Therefore, while this is suitable in some cases, we wouldn't recommend this as your go-to method for minification. We'll get into more advanced options below if you plan to implement minification in larger projects.

Command Line Tools

If you're comfortable with the command-line interface, you can use a command-line minifer. While these work similarly to online tools, they run locally and don't require an internet connection. 

One option is to use an npm package for minifying CSS. If you already have npm installed on your machine, you can download the css-minify package with the command:

 
npm install css-minify -g

You can then minify a single file with the command:

 
css-minify --file filename

… where filename is your file, ending with the extension .css. Or, minify all CSS files inside a directory with the command:

 
css-minify -d sourcedir

… where sourcedir is the directory name. All minified CSS files are stored in a directory called css-dist by default. You can specify a different destination directory with the command:

 
css-minify -d sourcedir -o distdir

Alternatively, you might try the minify package, which works on multiple coding languages — HTML, CSS, and JavaScript.

CDN

Do you use a content delivery network (CDN)? If so, these services can minify your website files for you. This occurs before they ship them to browsers. The perk of the CDN method is that it's the most hands-off way to handle minification. Because process becomes the job of your CDN, it won't impact your source files as stored on your origin servers. 

WordPress Plugins

Run a WordPress site? Optimization plugins can minify your code, such as W3 Total Cache, Hummingbird, and Autoptimize. These plugins are all made to be codeless and easy, letting you optimize your site's files, images, and more with the click of a few buttons. 

Software Build Tools

If you're using a build tool for your project, it likely has a native minification function or an integration that you can leverage to minify your project files regularly. To learn more, review your tool's documentation or integration library.

Minify your CSS for a better user experience.

We like the word "minification" — it makes you sound like a mad scientist taking a shrink ray to your bulky code. And your visitors will like it, too, since it gives them a better experience and a smoother time navigating your website. And a delighted visitor returns. 

Don't get us wrong, CSS is great. Without it, the web would be a pretty dull place. But it is possible to have too much of a good thing, and too much CSS can ultimately slow your pages and hurt the user experience. CSS minification is one useful workaround that saves everyone time without impacting your workflow or page content.

This post was originally published in November 2021 and has been updated for comprehensiveness.

New Call-to-action

Topics: Bootstrap & CSS

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about HTML and CSS and how to use them to improve your website.