How to minify HTML
Minifying HTML removes the comments and formatting whitespace a browser ignores, shrinking the first file it downloads. Here is what gets removed, what is protected, and the inline-spacing trap worth checking before you publish.
What minifying removes from a page
Formatting a browser ignores
A page written for humans is full of formatting: indentation showing the nesting, blank lines between sections, comments marking where a block starts and ends. A browser ignores all of it. Minifying strips it out — comments are deleted, the line breaks and indentation between tags are collapsed, and runs of several spaces become one. A page written with generous formatting can lose a third of its size without changing a single visible thing.
Four kinds of content stay untouched
Four kinds of content are protected and copied through unchanged: pre blocks, where spacing is the content; script blocks, where a removed line break can change JavaScript; style blocks, where the same is true for CSS; and textarea blocks, where the text is user-facing. Everything else is fair game.
Three ways to minify HTML
The quickest way is a browser tool: paste the page, click a button, copy the result. That suits a one-off job — a landing page built by hand, an email template, a snippet for a CMS. Our HTML Minifier does exactly that, in your browser, with the before-and-after size shown.
For a site you ship regularly, the job belongs in a build step or at the server. Static site generators and bundlers minify every page on deploy, and some hosts and content delivery networks offer automatic minification as a toggle — Cloudflare calls it Auto Minify. That approach minifies every page without touching your source files, which is the best of both worlds when it is available.
The whitespace trap
A space between inline elements is content
Whitespace in HTML is mostly ignored, but not always. A single space between two inline elements — two words, two links, an icon and its label — is real content, and removing it would join them together. A careless minifier that strips every space between tags can change how a page looks, in ways that are easy to miss until a visitor reports them.
Only line-break whitespace is removed
A careful minifier only removes whitespace that contains a line break, which is the formatting between tags on separate lines. A single space between inline elements on the same line is kept, because it might be doing a job. Even so, check the result before publishing — pages that rely on line breaks inside inline content, or on whitespace inside elements styled with white-space: pre-line, can shift slightly.
Minify HTML in the browser
- Open the HTML Minifier and paste your page into the Input box.
- Click Minify HTML. Comments are removed and the whitespace between tags is collapsed; pre, script, style and textarea blocks are protected.
- Check the size readout to see how many bytes were saved.
- Open the result in a browser and confirm it renders as expected, paying attention to spacing between inline elements.
- Click Copy output or Download to save the minified page, and keep the readable original for editing.
Check the result before publishing
Minifying is safe for well-formed pages, but safe does not mean unchecked. Open the minified page in a browser and scroll through it: look for words that ran together, icons that lost their gap, and any block that shifted. The problems minification causes are visual and small, which is exactly why they slip past — a quick visual pass catches them in seconds.
If something did shift, the fix is usually a single space in the source where the minifier removed one. Add it back, minify again, and check once more. Two passes is normal; the goal is a smaller page that looks identical, and the check is what guarantees it.
Minifying versus compressing
Minifying and compressing solve different parts of the same problem. Minifying removes characters from the file; compression (gzip or Brotli) squeezes the file while it travels. They stack, and production sites use both — but compression alone gets you most of the saving on HTML, because HTML compresses very well. Minifying is the extra step that squeezes out the rest.
That is why HTML minification is usually the lowest priority of the three minifications. Minify CSS and JavaScript first, where the savings are bigger; enable compression at the server; then minify HTML if you want the last few percent. For a page served millions of times, even a few percent is real bandwidth.
Related guides
- How to Minify CSS — strip CSS down to what the browser needs.
- How to Format HTML — lay out HTML so a human can read it.
- How to Escape HTML (Free HTML Entity Encoder) — turn code characters into entities a browser displays as text.
Questions people ask
Will minifying break my page?
Not for well-formed HTML. Comments and formatting whitespace are removed, while pre, script, style and textarea blocks are protected. Preview the result before publishing, especially on pages that rely on spacing between inline elements.
Does it minify the CSS and JavaScript too?
No. Style and script blocks are copied through unchanged. Use the CSS Minifier for stylesheets and a JavaScript minifier for scripts.
How much smaller will my pages get?
It depends on how the page was written. Heavily indented pages can lose a third of their size; pages already written compactly may save only a few percent. HTML is mostly tags and text, and tags cannot shrink.