HTML Minifier
Shrink HTML for faster pages. This HTML minifier collapses whitespace safely, removes comments and redundant attributes, minifies inline CSS and JavaScript, and shows the bytes and gzip size you save.
Press Ctrl+Enter (⌘+Enter on Mac) to run now.
Minify HTML Online
Every byte of HTML has to be downloaded and parsed before the browser can start rendering, so the document is the first file worth shrinking. This HTML minifier removes the indentation, line breaks, comments and redundant attributes that help developers but mean nothing to browsers, and it minifies the CSS and JavaScript embedded in the page.
It uses html-minifier-terser, the maintained fork of the widely used html-minifier, with clean-css and Terser for inline code. Whitespace is collapsed according to HTML rules, so <pre> and <textarea> keep their spacing. The panel below the boxes shows the bytes saved and a gzip estimate. Everything runs in your browser.
How to minify HTML
- Paste the HTML of a page, email template or component, or open an
.htmlfile. - Keep the safe defaults (whitespace, comments, inline CSS and JS, default and empty attributes) or tick the more aggressive options listed below.
- Check the savings panel: original size, minified size, bytes and percent saved, and the gzipped size compared with the gzipped original.
- Copy the minified HTML or download it as
index.min.html. If the HTML cannot be parsed, the error shows the line and column of the broken tag.
What gets removed and why it is safe
- Whitespace: runs of spaces, tabs and line breaks between block elements are removed; inside text and around inline elements such as
<a>or<strong>they become a single space, which is how browsers render them anyway. “Always keep one space” never removes whitespace entirely, and “Keep line breaks” turns whitespace that contains a line break into a single line break. - Protected content:
<pre>,<textarea>, scripts and styles are never whitespace-collapsed as text. Their code is only changed by the inline CSS and JS minifiers, and a script with a syntax error is left as it was. - Comments: normal comments are removed. Conditional comments (
<!--[if IE]>) are kept unless you untick that option, comments starting with<!--!are always kept, and anything between two<!-- htmlmin:ignore -->comments is left untouched. - Inline code:
<style>blocks andstyleattributes are minified with clean-css,<script>blocks and event handlers such asonclickwith Terser, and JSON-LD blocks are re-serialised as compact JSON. - Attributes: defaults such as
type="text/javascript"andtype="text"on inputs are removed,disabled="disabled"becomesdisabled, and emptyclass,idandstyleattributes are dropped.
HTML minifier options
| Option | Example | Risk |
|---|---|---|
| Collapse whitespace | <li>One</li> lines joined | Safe: follows HTML whitespace rules |
| Remove comments | <!-- nav --> removed | Safe; keep comments that a server or framework reads |
| Minify inline CSS / JS | margin: 0px → margin:0 | Safe; scripts that fail to parse are kept |
| Remove default attributes | <script type="text/javascript"> → <script> | Safe |
| Shorten boolean attributes | required="required" → required | Safe in HTML; not for XHTML served as XML |
| Remove attribute quotes | class="nav" → class=nav | Valid HTML5; quotes stay where values contain spaces or special characters |
| Remove optional tags | </li>, </p>, <head> dropped | Valid HTML5, but confuses tools that expect closing tags |
| Sort attributes and classes | class="b a" → class="a b" | Usually safe; can help gzip find repeats |
How much does minifying HTML help?
Savings depend on the source: deeply indented templates with many comments shrink a lot, while compact HTML may only lose a few percent. The gzip column matters most, because most servers compress HTML with gzip or Brotli, which already removes much of the repeated whitespace. Minifying still helps: the gzipped minified file is smaller than the gzipped original, and the browser has fewer characters to parse.
The gzip figure is measured in your browser with the CompressionStream API, so it estimates what your server sends; real sizes depend on its compression settings. If compression is not enabled yet, turn it on first; for Apache, the .htaccess Generator can write the rules.
Tips for smaller, faster pages
- Minify at build time. Test settings here, then use the same options in your build (html-minifier-terser also has a command line) so every deploy is minified.
- Keep a readable source. Never edit minified HTML by hand; edit the original and minify again, or re-indent it with the HTML Formatter.
- Shrink the heavy parts too: images are usually far larger than markup, so run them through the Image Compressor, and minify external files with the CSS Minifier and JavaScript Minifier.
- Watch for whitespace-dependent layouts: gaps between
inline-blockelements come from whitespace in the source; tick “Always keep one space” if a layout changes after minifying.
Frequently Asked Questions
Common questions about minifying HTML.
Paste your HTML into the left box. The minified version appears on the right with the bytes saved and a gzip estimate; copy it or download it as an .html file.
With the default options, yes: whitespace is collapsed according to HTML rules and <pre> and <textarea> content is preserved. The optional settings such as removing optional tags produce valid HTML5 but are less familiar, so test the page after enabling them.
Yes, by default. <!--[if IE]> blocks are kept when other comments are removed. Untick “Keep conditional comments” to delete them; for the <!--[if !IE]><!--> form only the markers are removed and the content stays.
Yes. Inline <style> blocks and style attributes are minified with clean-css, scripts and event handler attributes with Terser, and JSON-LD structured data is compacted.
Servers usually compress HTML before sending it, so the gzipped size is closer to what visitors actually download. It shows the real benefit of minifying on top of compression.
It reduces the document’s transfer and parse cost, which helps most on large pages. Images, scripts and server response time usually matter more.
The minifier needs well-formed tags. An attribute with a missing closing quote or a stray < stops it; the message shows the line and column where parsing failed.
No. The minifier library is downloaded once and runs in your browser, so your markup never leaves your device.