What is a JS, CSS and HTML minifier?
Minifying means rewriting a file so that it does the same thing in fewer bytes. For JavaScript that removes comments and whitespace, shortens local variable names (counter becomes t) and simplifies expressions. For CSS it removes comments, shortens colours (#ffffff becomes #fff), drops units from zeros (0px becomes 0) and merges rules. For HTML it collapses the whitespace between tags and removes comments and redundant attributes. The page behaves the same; it simply downloads faster, which helps load time on slow connections and is one of the basics of web performance.
This minifier covers the three languages in one place. Pick JavaScript, CSS or HTML, paste the code or drop a file, and the minified result appears next to it, with a table of sizes before and after, the percentage saved and an estimated gzip size, which is closer to what a user really downloads when your server compresses responses. The work is done by well-known libraries (terser for JavaScript, csso for CSS and html-minifier-terser for HTML), loaded the first time you use each tab.
Everything runs in your browser: the code you paste is never uploaded, which matters for private source. Minifying is not obfuscation or security. Shortened names make code harder to read, but anyone can format it back, so never rely on it to hide secrets, and keep your original source files, since a minified file is not meant to be edited.
How to minify JavaScript, CSS or HTML online
- Choose the language at the top: JavaScript, CSS or HTML. Each one keeps its own text, so switching tabs does not lose what you pasted, and each starts with an example already loaded.
- Paste your code in the input box, or drop a file on the upload area (
.js,.cssor.html). The example and clear buttons reset the box. - Set the options for that language. For JavaScript: compress the code, shorten variable names, keep class and function names, treat the file as an ES module (which also shortens top-level names), and choose the target version (ES5, ES2015, ES2017 or ES2020). For CSS: restructure rules to merge and reorder them for a smaller result. For HTML: remove comments, collapse whitespace, and minify the CSS and JavaScript inside the page.
- Read the Minified box. A syntax error appears instead of the result, with the line and column when the parser knows them.
- Compare the Sizes table: bytes before and after, percentage saved, and the same for the estimated gzip. Brotli is not estimated.
- Press the copy button, or download the file, named
output.min.js,output.min.cssoroutput.min.html.
Examples
JavaScript: what the ES module option changes
Input:
const total = items.reduce((s, i) => s + i.price, 0);
function helper(){return 1}
const unused = 5;
console.log(total);Without the module option, top-level names stay visible and unused code is kept:
const total=items.reduce(((e,t)=>e+t.price),0);function helper(){return 1}const unused=5;console.log(total);With Treat as ES module on, top-level names are also shortened and what nothing uses is removed:
const e=items.reduce(((e,c)=>e+c.price),0);console.log(e);Turn it on only for real modules; in a classic script, other files may need those top-level names.
CSS: shorter colours, zeros and no comments
Input (223 bytes):
/* Buttons */
.button {
color: #ffffff;
margin: 0px;
padding: 10px 20px;
background-color: rgb(37, 99, 235);
}
.button:hover {
background-color: #1d4ed8;
margin: 0px;
}
.button.large {
padding: 14px 28px;
}Output (151 bytes, 32.3% saved):
.button{color:#fff;margin:0;padding:10px 20px;background-color:#2563eb}.button:hover{background-color:#1d4ed8;margin:0}.button.large{padding:14px 28px}HTML: whitespace, comments and empty attributes
Input:
<!doctype html>
<html>
<body>
<h1> Hello, world! </h1>
<p>
This page was minified.
</p>
</body>
</html>Output:
<!doctype html><html><body><h1>Hello, world!</h1><p>This page was minified.</p></body></html>A syntax error
The JavaScript let a = ; produces Syntax error (line 1, column 9): Unexpected token: punc (;). No output is shown until the code parses.
Common errors and how to fix them
- Syntax error with a line and column: the JavaScript has a typo, an unclosed bracket or newer syntax than the target allows. Fix the position shown. Pasting a JSON object or a fragment with a trailing comma often triggers it.
- A CSS rule disappeared: for CSS, broken rules are dropped, not repaired, and a warning appears with their position, such as
2:1 Selector is expectedfor a stray}. Everything after the stray brace can be lost, so read the warnings before using the result. - The page broke after minifying JavaScript: code that reads function or class names (
fn.name,constructor.name, dependency injection by parameter name) needs Keep class and function names, and code that relies on its top-level names needs the module option off. - The target does not convert syntax: choosing ES5 does not rewrite arrow functions or
?.into older code. For that you need a transpiler such as Babel before minifying.
Frequently asked questions
How much smaller will my file get?
It depends on the code. Heavily commented, indented code shrinks the most. The built-in examples save 25.5% of the bytes in JavaScript and 32.3% in CSS, and the table shows the real figures for your file, plain and gzip.
Is minifying the same as compressing with gzip?
No, they stack. Minifying rewrites the source; gzip or Brotli compresses the bytes the server sends. Use both. To shrink data such as JSON, try JSON minify; for images, use image compress.
Can I get the original code back?
Not the original names or comments. Whitespace and structure can be restored with a formatter, but shortened variable names are lost for good, so always keep your source.
Does the minifier send my code anywhere?
No. The libraries run in your browser and nothing is uploaded.
Should I inline small images in my CSS?
Sometimes, for tiny icons, since it saves a request. Generate the data with image to Base64, keeping in mind that Base64 adds about a third to the size.
Minify in your build or terminal
JavaScript
The same libraries are available as packages, so a build step can reproduce the result:
import { minify } from 'terser';
import { minify as minifyCss } from 'csso';
const js = await minify('function add(a, b) { return a + b }', { compress: true, mangle: true });
console.log(js.code); // function add(n,d){return n+d}
console.log(minifyCss('a { margin: 0px; color: #ffffff }').css); // a{margin:0;color:#fff}Bash
The terser command line does the same, and gzip lets you check the size that matters:
npx terser app.js --compress --mangle --output app.min.js
gzip -c app.min.js | wc -c