Skip to content
FileKit
Developer ToolsRuns in your browser6 min read

HTML Minifier

This html minifier collapses whitespace between tags, removes HTML comments and trims redundant spacing, while leaving the content of pre, textarea, script and style elements exactly as written. Paste a page or drop an .html file, and the tool returns a smaller document that a browser renders identically to the original.

Loading the tool interface
Files never leave your deviceNo upload wait and no queueNo signup, watermark or file limit

Most HTML written by hand or generated by a templating engine is full of whitespace that exists purely for the author's readability: indentation for nested tags, blank lines between sections, a line break after every closing tag. A browser's layout engine ignores almost all of that whitespace when it renders block level content, which means removing it is one of the safest size reductions you can make to a page, provided the minifier knows exactly which elements are the exception to that rule.

The whole process runs locally in your browser, so an unreleased landing page or an internal tool's markup never has to leave your machine to get smaller. That makes it a private way to minify html online before a manual deploy or a quick performance check.

What this html minifier collapses, and the four elements it protects

It removes every HTML comment, collapses runs of whitespace between tags down to a single space or nothing, and trims leading and trailing whitespace inside most elements. That html whitespace collapse step, combined with comment removal, typically saves 15 to 25 percent of the file size on a hand formatted page before any other optimisation is applied.

It leaves four elements completely untouched: pre and textarea, where whitespace is part of the displayed or submitted content and collapsing it would change what the user sees or types; and script and style, where the content is JavaScript or CSS, not HTML, and where a stray whitespace change could break a string literal or a significant space in a CSS value. Every other element gets the full whitespace and comment treatment.

  • Removes HTML comments, including multi line ones, everywhere except inside a preserved element.
  • Collapses whitespace between tags to a single space where whitespace is meaningful, or nothing where it is not.
  • Preserves pre and textarea content exactly, including every space, tab and line break.
  • Preserves script and style content exactly, since it is JavaScript or CSS rather than HTML.
  • Leaves every attribute, attribute value and tag name completely unchanged.

Why pre, textarea, script and style cannot be touched like the rest of the page

Whitespace inside a pre element is rendered exactly as written, including every leading space used to indent a line of example code, which is the entire reason pre exists as a tag. Collapsing that whitespace the way you would between two paragraph tags would visibly mangle the formatted content, which defeats the purpose of using pre in the first place. A textarea behaves the same way for its default value: whitespace inside it becomes the literal starting text of the form field.

Script and style elements contain a different language entirely. A JavaScript template literal can contain meaningful whitespace inside a string, and a CSS value can depend on the exact spacing around a shorthand property. Treating their contents as HTML text to be trimmed and collapsed would risk exactly the kind of corruption the standalone javascript minifier and css minifier on this site are built specifically to avoid, so this tool copies both element types through byte for byte instead.

Comment removal and the one HTML comment worth keeping

Ordinary HTML comments are safe to remove everywhere outside the four protected elements, since a browser never renders them and they exist purely to leave notes for a future reader of the source. This tool strips them by default.

The one exception is a conditional comment written for legacy Internet Explorer support, which some very old codebases still rely on. If your project needs those preserved, keep comment stripping off for that section and run the rest of the page through separately, since this tool does not attempt to detect that special syntax automatically.

How this html minifier works under the hood

The tool scans the markup character by character rather than using the DOM, specifically so it can process a document fragment that is not a full valid page, which is common when minifying a template partial or a component's markup before it gets assembled into a larger page. It tracks whether it is inside a tag, inside one of the four protected elements, or inside plain text content, and only trims and collapses whitespace in that last state.

That approach keeps the tool fast and dependency free. A multi hundred kilobyte HTML file, including its embedded script and style blocks, minifies in well under a second directly in the browser tab, with no server round trip and no size limit beyond your device's memory.

Where an html minifier fits in a deploy pipeline

For a static site or a template rendered on the server, running an html minifier as a build step before deployment is the normal place for this transformation, since it lets you keep readable, indented source files in version control while shipping a compact version to production. Doing it once at build time is more efficient than minifying on every request.

For a quick one off task, such as pasting a snippet into a system with a strict character limit or checking how much a specific page could shrink, using this tool directly in the browser is faster than wiring up a build step. Either way, the same html minifier logic applies: comments and non significant whitespace are removed, and pre, textarea, script and style content stays exactly as written.

How to minify HTML

  1. 1

    Add your HTML

    Paste markup into the input box, or drop an .html file onto the page. Nothing is uploaded.

  2. 2

    Review the options

    Comment removal and whitespace collapsing are both on by default and can be toggled independently.

  3. 3

    Minify

    Run the scanner and check the before and after size in the stats panel.

  4. 4

    Copy or download the result

    Copy the minified HTML to your clipboard or download it as an .html file.

Related tools worth bookmarking

Sources and further reading

Frequently asked questions

Common questions about the html minifier.

No. Content inside pre and textarea elements is copied through exactly as written, including every space, tab and line break, because whitespace there is part of what gets displayed or submitted rather than formatting for the source file. Only whitespace outside those two elements is collapsed.

No, and that is intentional. The content of style and script elements is copied through untouched, since collapsing HTML whitespace rules inside a different language can corrupt a string literal or a meaningful space in a CSS value. Run the javascript minifier or css minifier on that content separately if you also want it compressed.

Yes, as long as the output is valid HTML at the point you paste it in. The scanner works on markup fragments as well as full documents, so template partials that are not complete standalone pages minify correctly, since the tool never requires a valid document root.

No. The minifier runs entirely in your browser using a local character scanner. Your markup, including an unreleased page or internal tool, is never transmitted, stored or seen by anyone but you.

A hand formatted page with generous indentation and comments typically shrinks by 15 to 25 percent from whitespace and comment removal alone. A page that is already compact will save less, since there is simply less redundant whitespace left to remove.

Yes. The two operations are controlled by separate toggles, so you can strip only comments and leave the original formatting intact, which is useful when you want to clean notes out of a file but keep it readable for a future editor.