Web Developer Tools · · 4 min read
How to Minify HTML to Shrink Page Size
Use the HTML Minifier to strip comments and whitespace from HTML, with options to collapse inter-tag spacing and minify inline CSS.
To minify HTML, paste it into the HTML Minifier and read the compacted result instantly in the output box — it strips comments, collapses whitespace, and minifies any <style> blocks by default, with no button to click.
What the HTML Minifier does
Minifying HTML removes the comments, line breaks, and extra spacing that make source markup easy for people to read but add dead weight to what actually ships to a browser. The HTML Minifier does this directly in your browser: it deletes HTML comments, collapses runs of whitespace down to single spaces, and removes whitespace sitting between tags, all without touching your actual tags, attributes, or text content. It also minifies the CSS inside any <style> block it finds, using the same engine as the standalone CSS Minifier. Content inside <pre>, <textarea>, and <script> tags is left untouched — the minifier never rewrites script code or alters text that's meant to preserve its exact spacing.
How to minify HTML with Toolbench
- Open the HTML Minifier.
- Paste your HTML into the input box on the left.
- Leave the three toggles on for maximum shrinkage, or switch any of them off:
- Remove comments (on by default) — deletes
<!-- ... -->comments, but keeps IE conditional comments like<!--[if .... - Remove whitespace between tags (on by default) — collapses the space between
</h1>and<p>into nothing. - Minify
<style>blocks (on by default) — runs the contents of any<style>tag through the CSS minifier.
- Remove comments (on by default) — deletes
- Read the minified HTML in the output box — it updates live as you type or paste.
- Click Copy to grab the result, or use Send to → to pass it into another tool, such as the HTML Beautifier to check the output is still valid.
A real worked example
Starting from this commented, indented HTML:
<!-- hero -->
<div class="hero">
<h1>Hello</h1>
</div>
With all three options at their defaults, the HTML Minifier produces:
<div class="hero"><h1>Hello</h1></div>
The comment is gone, the newlines and indentation between <div class="hero">, <h1>, and </div> have collapsed away entirely, and the tags, attributes, and text content are exactly as written.
How the options compare
| Option | Default | What it does when on | What happens when off |
|---|---|---|---|
| Remove comments | On | Deletes <!-- ... --> comments (IE conditional comments are always kept) |
Comments stay in the output |
| Remove whitespace between tags | On | Collapses the space between adjacent tags down to nothing | A single space remains between tags instead of a line break or indentation |
Minify <style> blocks |
On | Strips comments and whitespace from CSS inside <style> tags |
<style> block contents are left exactly as written |
Note that whitespace within text content is always collapsed to single spaces regardless of these toggles — the Remove whitespace between tags option controls only whether that single space between two tags is removed entirely.
Tips and common mistakes
- Script and pre/textarea content is never rewritten. The minifier protects
<script>,<pre>, and<textarea>blocks so their exact contents survive untouched — it won't shrink your inline JavaScript. Run that through the JavaScript Minifier separately if you need it compacted too. - Minifying doesn't validate your markup. If a tag is unclosed or malformed going in, it's still unclosed or malformed coming out — this tool only removes whitespace and comments, it doesn't fix structural issues.
- Turn off "Remove whitespace between tags" for debugging. If you need to eyeball the minified output without everything crammed onto one line, switching that toggle off keeps a single space between tags while still dropping comments.
- It's the inverse of the HTML Beautifier. If you need the readable version back, paste the minified output into the HTML Beautifier — note it won't restore the removed comments, since those are gone for good.
Related tools
- HTML Beautifier — reformat minified or messy HTML back into readable, indented markup.
- CSS Minifier — strip comments and whitespace from standalone CSS files.
- JavaScript Beautifier — format JavaScript if you need to read it before or after minifying the page around it.
Frequently asked questions
How do I minify HTML?
Paste it into the HTML Minifier. It removes comments and collapses whitespace instantly, with no button to click and no account needed.
Does minifying HTML change how the page looks?
No. The minifier only removes comments and whitespace — it doesn't touch tags, attributes, or text content, so the rendered page looks identical before and after.
Will it minify the CSS and JavaScript inside my HTML?
CSS inside <style> tags is minified automatically when the Minify <style> blocks toggle is on. JavaScript inside <script> tags is left exactly as written; use the JavaScript Minifier separately for that.
What happens to HTML comments?
They're deleted by default when Remove comments is on, except for IE conditional comments (<!--[if ...-->), which are always preserved since they affect rendering in older browsers.
Is my HTML sent to a server?
No. Minifying runs entirely in your browser. If you're signed in, your input syncs privately to your account; if you're signed out, nothing is saved anywhere.
More guides
- How to Beautify and Format JavaScriptUse the JavaScript Beautifier to turn minified or messy JS into clean, indented code with 2-space, 4-space, or tab indentation.
- How to Minify CSS to Shrink Stylesheet File SizeUse the CSS Minifier to strip comments and whitespace from CSS instantly in your browser, with a real before-and-after example.
- How to Beautify and Format CSSUse the CSS Beautifier to turn minified or cramped CSS into clean, indented rules with 2-space, 4-space, or tab indentation.