Skip to content

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

  1. Open the HTML minifier.
  2. Paste the HTML or click Open file. The minified version appears right away.
  3. 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.
  4. Read the size comparison above the editors: original, minified and saved bytes.
  5. Click Copy minified or Download, and test the page once.

Example: what the HTML minifier removes and keeps

page.html
<!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>
Minified
<!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>
The HTML minifier with all six options ticked. The size bar reads Original 483 B, Minified 324 B, Saved 159 B (32.9%). The original HTML with comments and indentation is on the left, the minified HTML on the right, where the script's comment and the pre text keep their line breaks.
483 bytes down to 324: saved 159 bytes (32.9%).
  • Removed: the HTML comment, the CSS comment, the indentation, the redundant type="text/javascript", and checked="checked" became checked.
  • 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 // Analytics comment. The CSS color #ffffff also 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:

VersionUncompressedgzip
Original483 bytes300 bytes
Minified324 bytes243 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
/*! 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:

Minified
/*! 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