Reducing HTML, CSS, and JavaScript file size is a crucial part of speeding up your website.
You can make code files lighter by applying two techniques:
Minification - removing unnecessary parts like whitespace and comments from the code;
Compression - applying algorithms to rewrite the files’ binary code, using fewer bits than the original.
Both are low-effort, high-reward optimizations. And while different, the smart move is getting them in one go.
The good thing is there are lots of tools that can help you with that. If you aren’t interested in the “why” behind these techniques, skip ahead to the "Tools for HTML, CSS, and JavaScript Minification section".
And for the easiest way to optimize your website’s code, go to "Automated Minification and Compression with NitroPack";
For now, let’s dive a bit deeper into minification.
Minification is the process of removing unnecessary elements from your website’s code without affecting its performance.
A few examples of unnecessary elements are:
Comments;
Line-breaks;
Whitespace.
Browsers don’t need these to render the page. In that sense, all they do is bloat code files. (Of course, they also help developers write and review code, but browsers don’t care about that.)
For example, here’s how an unminified CSS files looks:
Generated with this CSS generator
When we run it through a CSS minifier, the same code looks like this:
In this example, the actual size difference between the regular and minified version is tiny. But when you apply the process to large code files, the savings are significant. That’s why minification has become an industry standard over the last few years.
Most JavaScript libraries provide minified versions of their files by default. Lots of CDN providers also automatically minify files on their servers.
People often confuse minification and compression (zipping), which is understandable. Both techniques essentially provide the same benefit, i.e., making files smaller.
However, there’s a fundamental difference between the two.
While minification removes unnecessary data from a file, compression rewrites the files’ binary code and encodes the information using fewer bits than the original.
The most popular compression tool is gzip. In fact, people often use the term “gzipping” as a substitute for compression.
Gzipping works by finding the repetitive strings and replacing them with pointers to the first instance of the string. The benefit is that pointers use less space than text.
Here’s a great example of this process in action:
As you can see, the text in brackets is repetitive, so gzip replaces it with a pointer.
While gzip is the most popular compression method, there are lots of others out there.
For example, the Brotli algorithm has proven to be equally good, if not better, in some aspects.
Like minification, a few CDN providers (and hosting companies) automatically compress files on their servers. Again, go over your providers’ documentation to see if this has been taken care of for you.
You can also use the Network tab to see if files have been minified and compressed. To do so, open the Network tab (right-click and select “Inspect”) in your browser and look at the file names and response headers.
Minified files typically have “.min” in their name. Compressed files have a content-encoding header, usually with a gzip or br value.
Concatenation is another common technique for improving web performance.
It consists of combining CSS or JavaScript files into just one CSS or JavaScript file. The benefit of having fewer files is that the browser has to make fewer HTTP requests.
Since browsers have connection limitations, concatenation is also a good practice when it comes to code optimization.
Now, concatenation isn’t as straightforward as minification and compression. Lots of things can go wrong, especially when combining JavaScript files.
For example, with some websites, you can find JavaScript in the code, framework, theme, and plugins. These scripts often depend on one another to work.
Let’s say script1 depends on script2, which depends on script 3. In that case, browsers must load and run them in a specific order. They must run script3 first, followed by script2, and finally - script1.
Any other order would break something.
The problem is that after concatenation, scripts can show up all over the new combined JavaScript file. As a result, browsers load and run them in the wrong order.
That’s why you have to test and figure out if concatenation is right for you. You can do this with a site speed service like NitroPack that offers intelligent concatenation features.
Just make sure to test it before deploying it to your live website.
One word - performance.
When you minify CSS and JS, along with HTML, you'll speed up your website, reduce file size, and pass the specific PageSpeed Insights audit.
Now let's dig deeper into each benefit:
The process of minifying JS, CSS, and HTML can improve your site's loading time and overall page performance. Minifying JavaScript, for instance, reduces the size of your JS files, thereby decreasing the time required for parsing scripts. This, in turn, leads to faster load times and better user experience.
Largest Contentful Paint measures how long it takes for the largest above-the-fold element to load on a page. Minification is an effective way of resolving issues that cause a low LCP score. Employing minification can enhance server response times, decrease JS and CSS blocking times, and improve resource loading times.
Smaller files mean faster delivery. But when it comes to which technique to use, CSS Tricks suggests that compressing (gzipping) reduces the file size by approximately five times more than minifying. However, minification is still a recommended technique that can provide improvement to the overall performance.
There are lots of minification tools online, including free ones. Here are a couple of suggestions:
As I said earlier, minification removes the parts that make code easier to read and review. If you ever need to go through minified code, Unminify can make it easily readable.
Finally, you can use NitroPack to optimize your website’s code files.
Here’s what NitroPack does to reduce HTML, CSS, and JavaScript file size:
Minification and compression. NitroPack applies both techniques automatically. Minification is done on AST trees instead of using regexes, yielding the best results in terms of code size and stability;
Intelligent CSS and JavaScript combining. Our service combines CSS files by default. You can also use NitroPack to combine JavaScript files, although that option isn’t enabled by default. For more information on our concatenation features, check out this article.
NitroPack also comes with everything else you need for a fast website.
Put simply, our service solves all of your page speed problems in one go. But don’t take my word for it. Try our free plan - it doesn’t expire or require a credit card, so you can test NitroPack without risking anything.
Evgeni writes about site speed and makes sure everything we publish is awesome.