How to Minify HTML, CSS and JavaScript Safely
By Durim Hoti · Last updated
Minifying removes everything a browser doesn't need: comments, indentation, line breaks and, in JavaScript, long variable names. The file gets smaller and loads a little faster. The question is what is safe to remove, and how much it really saves.
Minify a page step by step
- Open the HTML minifier.
- Paste the HTML or click Open file. The minified version appears right away.
- Check the options. All are on by default: Remove comments, Collapse whitespace, Minify inline CSS, Minify JSON / JSON-LD, Shorten boolean attributes and Remove default type attributes.
- Read the size comparison above the editors: original, minified and saved bytes.
- Click Copy minified or Download, and test the page once.
Example: what the HTML minifier removes and keeps
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Page styles -->
<style>
/* Buttons */
.btn {
color: #ffffff;
padding: 8px 16px;
}
</style>
<script type="text/javascript">
// Analytics
window.dataLayer = window.dataLayer || [];
</script>
</head>
<body>
<p>Price: <strong>$5</strong> <em>per month</em></p>
<input type="checkbox" checked="checked">
<pre> keep
these spaces</pre>
</body>
</html>
<!DOCTYPE html><html lang="en"><head><style>.btn{color:#ffffff;padding:8px 16px}</style><script>
// Analytics
window.dataLayer = window.dataLayer || [];
</script></head><body><p>Price: <strong>$5</strong> <em>per month</em></p><input type="checkbox" checked><pre> keep
these spaces</pre></body></html>
- Removed: the HTML comment, the CSS comment, the indentation, the redundant
type="text/javascript", andchecked="checked"becamechecked. - Kept: the space between
</strong>and<em>(without it, "$5 per month" would read "$5per month") and the exact spacing inside<pre>. - Not touched: the inline script, including its
// Analyticscomment. The CSS color#ffffffalso stayed as written; the minifier removes whitespace and comments but doesn't rewrite values.
How much it really saves
Servers usually compress text files with gzip or Brotli before sending them. We compressed both versions of the example page with gzip:
| Version | Uncompressed | gzip |
|---|---|---|
| Original | 483 bytes | 300 bytes |
| Minified | 324 bytes | 243 bytes |
Compression alone cut the page by 38%; minifying first saved another 57 bytes. The ratios differ for every file, but the pattern is typical: minify and compress together, and don't expect miracles from minifying alone.
Minifying CSS and JavaScript files
The CSS minifier removes comments and whitespace from stylesheets; tick Keep /*! license comments to keep license notices. The JavaScript minifier goes further than removing spaces. With its defaults (Compress, Shorten names, comments: Keep license comments) this function
/*! cart.js v1 | MIT */
// total
function calculateTotal(items) {
const taxRate = 0.19;
let sum = 0;
for (const item of items) {
sum += item.price * item.qty;
}
return sum * (1 + taxRate);
}
console.log(calculateTotal([{ price: 10, qty: 2 }]));
becomes:
/*! cart.js v1 | MIT */
function calculateTotal(t){let c=0;for(const l of t)c+=l.price*l.qty;return 1.19*c}console.log(calculateTotal([{price:10,qty:2}]));The /*! license comment stayed; the other comment went. Local names such as items and sum became single letters, and the constant tax rate was folded into 1.19. The function name calculateTotal stayed, because other code may call it.
Common problems
Words run together after minifying
Check where the space was. The minifier keeps the line break between two <span> elements as one space, but removes it between block elements such as <div> and <li>. If your CSS displays those as inline-block, the small gap the line break created disappears. Set the gap in CSS instead, for example with gap or margin.
The script stops working
Code that relies on function names at runtime (for example reading fn.name) can break when names are shortened. Turn off Shorten names in the JavaScript minifier.
A license notice disappeared
Only comments that start with /*! count as license comments. Change the comment to that form, or choose to keep all comments.
Editing the minified file is impossible
Don't edit minified code. Keep the original as the source and minify again after each change; to read minified code, see how to format HTML code.
What to do next
Want to try the minified page right away? Paste it into the online HTML editor; our guide on running HTML online explains the preview and the console.
FAQ
- Is minified code still valid?
- Yes. A minifier only removes what the browser ignores, such as comments and indentation, or rewrites code into a shorter equivalent. The page should look and work the same; test it once after minifying.
- Do I still need minification if my server uses gzip?
- It still helps, but less. In our example, gzip alone shrank the HTML from 483 to 300 bytes; minifying first brought the compressed size down to 243 bytes.
- Can I undo minification?
- Formatting restores indentation and line breaks, but removed comments are gone, and JavaScript names shortened by the minifier stay short. Keep your original source files.
- Why is the JavaScript inside my HTML not minified?
- The HTML minifier leaves <script> contents unchanged. Minify the script with the JavaScript minifier, or move it into its own .js file.
- Is my code uploaded?
- No. All three minifiers run in your browser.
Try it and keep reading
- Open the HTML minifierCompress HTML and see how many bytes you save.
- How to Format HTML Code So It's ReadableIndent minified or messy HTML, choose how attributes wrap, and spot broken markup.
- How to Run HTML, CSS and JavaScript OnlineWrite HTML, CSS and JavaScript in the browser, see the result, and read the errors when it doesn't work.