Minify HTML
Paste a page or a fragment of HTML, or drop an .html file, and the minified version appears as you type, with its size before and after. The CSS in style tags and the JavaScript in script tags are minified along with it.
How to use it#
- Drop an .html file on the box, or paste the markup into it.
- Choose how whitespace is treated, and which extras to remove.
- Check the sizes, then copy the result or download it as a .min.html file.
What minifying HTML takes out#
Minifying HTML, also called compressing it, saves mostly whitespace: the indentation and line breaks that make markup readable. Comments go next, apart from conditional comments for old Internet Explorer, server-side include comments starting <!--#, and comments starting <!--!, which are kept on the assumption that they are there on purpose. The CSS in each style tag and style attribute is minified with clean-css, and the JavaScript in each script tag and event handler attribute, such as onclick, with terser.
Two extras are off at first, because each makes the markup harder to read and work with. Drop optional tags and quotes leaves out the closing tags HTML lets you omit, such as </li> and </p>, along with attribute quotes where they are not needed and the value of attributes such as disabled="disabled". Remove default attributes drops ones that only say what the browser assumes anyway, such as type="text" on an input and method="get" on a form. Together they are worth a percent or two.
To go the other way, and make a minified page readable again, paste it into the HTML formatter, which indents it and the CSS and JavaScript inside.
The whitespace a page depends on#
Some of the whitespace in HTML shows on the page. A space between two inline elements is drawn, so <b>one</b> <b>two</b> needs its space. The default keeps one space wherever there was any, which leaves every gap the page shows, and turns runs of spaces and line breaks into that single space. Pre and textarea elements are left exactly as written.
Also remove it around block tags takes the space out between elements such as div and li as well, where a browser normally shows none. It saves a little more, and it closes the gaps in a row of items that CSS has set to display: inline-block, such as a menu built from a list. Use it when you know the layout does not rely on them.
The minifier reads the markup, not the stylesheet, so it cannot know that CSS has given some element white-space: pre. Choose Leave it as written for a page like that, and the rest of the minifying still runs.
The CSS and JavaScript inside#
Script tags are minified as classic scripts, type="module" ones included, so the names they define at the top level keep their spelling, since another script on the page may use them. A script that does not parse is left unminified, and so is a script tag holding data rather than code, such as JSON-LD. For a standalone file, the JavaScript minifier offers more control, with module support and a choice about comments.
The CSS minifier predates CSS nesting, and it silently deletes a rule written inside another rule, along with the rules after it. A page whose CSS uses nesting, or has a brace left open, keeps its CSS as written, and a line under the sizes says so. The markup and scripts are minified as usual. An @import in the CSS stays an @import and is never fetched.
Frequently asked questions#
Can minifying change how the page looks?
#
Not with the default settings, with one exception: text that CSS sets to white-space: pre or pre-wrap outside a pre element, where runs of spaces are collapsed. Choose Leave it as written for such a page. Also remove it around block tags can close the gaps between inline-block items, which is the other setting that can show.
Which options can change what the page does?
#
Remove default attributes can. A stylesheet rule like input[type="text"], or a script reading getAttribute("type"), stops matching once the attribute is gone, although the browser still treats the input as text. Drop optional tags and quotes produces valid HTML that browsers read the same way, but a tool that expects XHTML will reject it. Remove comments also removes comments a script reads, such as the <!-- ko --> bindings some templating libraries use; untick it for those pages.
How much does minifying HTML save?
#
Less than it first appears, because a server sends HTML gzip or Brotli compressed, and compression already squeezes out most repeated whitespace. As a measured example, an indented 35 KB page with inline CSS and scripts came out at 24 KB, 31% smaller. Gzipped, it went from 7.7 KB to 6.1 KB, 20% smaller, and the two extras moved that by about one percentage point. The Gzipped row measures your own page by compressing both versions in the browser.
Why does it say it could not read my HTML?
#
Almost always a < that does not start a tag, as in 1 < 2, which a browser forgives and the minifier does not. Write it as < and the page will minify. The message gives the line and column, and the last result that worked stays on screen while you fix it.
Is it worth minifying HTML by hand?
#
For a single static page, an email template, or a snippet going into a CMS, yes: paste it, download the .min.html, and publish that. A site that generates pages should minify as it builds them, which many static site generators do already. Keep the readable version as the one you edit, since the minified copy is hard to change by hand.
Is my HTML uploaded?
#
No. The minifier runs in this browser tab, in the background so typing stays smooth on a long page, and nothing you paste or drop is sent anywhere.