How to minify CSS and JavaScript
How minification actually reduces file size, and how to test for the breakage it occasionally causes.
Minification strips everything from a CSS or JavaScript file that a human needed to read it but a computer does not: comments, whitespace, line breaks, and often long variable names, which get shortened where it is safe to do so. None of it changes what the code actually does — it changes only how many bytes it takes to say it.
Combined with server compression, minification reduces the amount of data a browser has to download before it can start using a script or applying a stylesheet. It is a straightforward, low-risk optimisation on its own, though the tools that apply it automatically occasionally introduce the one class of problem worth knowing to watch for.
What minification actually removes
/* Before */
.site-header {
background-color: #ffffff;
padding: 20px 0;
}
/* After */
.site-header{background-color:#fff;padding:20px 0}
The result is identical to a browser and completely unreadable to a person — which is exactly the point. Minified files are a build output, not something anyone is meant to edit directly; the original, readable source is kept separately and minified again each time it changes.
How to apply it
| Method | Best for |
|---|---|
| A build tool as part of development | Sites with a proper development workflow, where minification happens automatically before files are deployed |
| A caching or optimisation plugin | WordPress and similar platforms, where a plugin can minify CSS and JS from the theme and other plugins without any coding |
| A one-off minifier tool | A small, mostly static site where files rarely change and a manual pass is enough |
For most sites running on a content management system, an optimisation or caching plugin is the practical route — it can minify and often combine files automatically, including ones added by other plugins, without needing to touch any code directly.
The problem minification occasionally causes
CSS minification is close to risk-free — removing whitespace and comments from a stylesheet essentially cannot change how it renders. JavaScript minification is different: more aggressive settings rename variables and restructure code, and on a script that relies on a specific variable name being visible elsewhere, or that was already slightly fragile, this can break functionality that worked fine before minification was turned on. This is the single most common cause of "the site looked fine until I turned on the optimisation plugin".
This is rare, but common enough to test for deliberately rather than assume away. If a page stops working correctly — a form that no longer submits, a menu that no longer opens, an interactive element that goes silent — immediately after enabling minification, that is the first thing to suspect and undo.
Keep the original source, always
A minified file is a build output, not a working copy. Never edit a minified file directly, and never let the only surviving version of a script or stylesheet be the minified one — if you ever need to change the code, debug an issue, or hand the project to someone else, an unreadable file with the whitespace and comments stripped out is far harder to work with than the original was. Keep the readable source under version control or as the canonical copy, and treat minified output as something regenerated from it each time, rather than as a file to maintain by hand.
A source map is a separate file that maps positions in the minified output back to the original, readable source, so a browser's developer tools can show you the original code and line numbers even while the browser is actually running the minified version. Most build tools and plugins that minify JavaScript can generate one alongside the minified file. It costs nothing in terms of the page's actual load time — the source map is only fetched when developer tools are open and actively used — and it turns "which of these two thousand characters on one line broke" back into a normal, readable error report.
How to test safely
-
Enable minification on a staging copy first, if you have one
A staging environment lets you test changes without risking the live site.
-
Turn on CSS minification first
Lower risk, and a good first test of whether the tool itself is behaving correctly.
-
Click through every interactive element on the site
Forms, menus, sliders, any JavaScript-driven feature — not just a glance at whether the page loads.
-
Enable JavaScript minification and repeat the same check
This is where problems, if there are going to be any, actually show up.
-
Check the browser console for errors
Developer tools will show a JavaScript error immediately if minification has broken something, even if the page still looks fine at first glance.
If something breaks, most optimisation plugins offer an option to exclude a specific file from minification while still minifying everything else, which is a reasonable permanent fix for the one script that does not tolerate it well, rather than turning the feature off entirely.
What minification will not do
It reduces file size — it does not reduce how many files there are, and it does not reduce how much work a script actually does once it runs. A minified copy of a large, unnecessary script is still a large, unnecessary script; removing or deferring requests that do not need to happen at all addresses a different, often larger, part of the same problem.
Minification and server-side compression work well together but are not the same thing: minification reduces the file at rest, compression reduces what actually travels over the network on top of that. Enabling Gzip or Brotli compression covers the second half of this pairing, and doing both is more effective than either alone.
Related reading
Why every extra file a page loads adds delay, and how to cut the number down without changing how the page looks.
How to enable compression on your siteHow to turn on text compression for your site, and how to check it is actually being applied afterwards.
How to read a waterfall chartWhat every bar, gap and colour in a waterfall chart actually means, and how to use it to find a real cause.
How to fix images that will not loadA page full of broken image icons could be a wrong path, a permission, or a blocked request — here is how to tell them apart.