How-to Guides Web & HTTP

How to Minify CSS and JavaScript

Shrink stylesheets and scripts by removing comments, whitespace and redundant code: run each file through a minifier, compare sizes, test the result and serve it compressed.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page
On this page

Steps

  1. Keep the readable source

    Never edit a minified file. Keep the original files in your project and treat the minified ones as build output you can recreate at any time.

  2. Minify the CSS

    Paste the stylesheet into the CSS Minifier (it uses the csso engine) and copy the result. Comments go, shorthand is merged, colours are shortened and rules with the same selector are combined.

  3. Minify the JavaScript

    Paste the script into the JavaScript Minifier (the terser engine). Compression removes dead code and simplifies expressions; mangling shortens local variable names. Leave mangling off for code that depends on names.

  4. Compare the sizes

    Check the saving the tools report for each file. Typical savings are 20 to 60 percent before compression, and more for heavily commented source.

  5. Test the result

    Load the page with the minified files and watch the browser console for errors. Run your usual checks, especially around forms and anything that reads function names.

  6. Serve it compressed and cached

    Enable gzip or Brotli on the server and give files long cache lifetimes, changing the file name when the content changes. If you load files from a CDN, protect them with How to Add Subresource Integrity to Scripts and Stylesheets.

What you need

A stylesheet or script you want to shrink, and a way to test the page afterwards.

Example

The tools above run real engines. Here is the output they produce for a tiny stylesheet and script.

CSS before (127 characters) and after (67):

/* header */
.card {
  margin: 0px 0px 10px 0px;
  color: #ffffff;
  background-color: #FF0000;
}
.card {
  padding: 0.50em;
}
.card{margin:0 0 10px;color:#fff;background-color:red;padding:.5em}

JavaScript before (161 characters) and after (65):

// add two numbers
function addNumbers(firstNumber, secondNumber) {
  const total = firstNumber + secondNumber;
  return total;
}
console.log(addNumbers(2, 3));
function addNumbers(d,e){return d+e}console.log(addNumbers(2,3));

Common pitfalls

  • Do not minify and then edit by hand; regenerate from source instead.
  • Source maps map minified code back to the original in the browser debugger; generate them in a build tool for production work.
  • Minifying already minified files gains almost nothing.
  • Code that uses eval, Function.prototype.name or string property names can break under mangling.

Try these tools

See also

Frequently Asked Questions

It should not. Minifiers keep behaviour identical, but mangling and aggressive compression can expose bugs, such as code that relies on function names. Always test the minified files.

No. Pair it with gzip or Brotli compression and caching; compression usually saves more than minifying, and the two add up.

AdSense Placeholder
Slot: footer_leaderboard