↑↓ to move ↵ to open Esc to close Browse all tools

HTML Minifier

Paste HTML or open a file to make it smaller. Comments and whitespace that browsers do not show are removed, and inline CSS and JavaScript can be minified too.

Pasted HTML is minified right away. Ctrl + Enter or Cmd + Enter runs it again.
Whitespace

Saved -

Paste HTML to minify it.

Minified HTML

The minified HTML appears here.

Your HTML is minified in your browser. Nothing is uploaded.

Hand-checked listFree Code Editors and IDEsFree code editors and IDEs for desktop, terminal and browser, with license and platforms for each.

HTML Minifier by Freebiefy, free online tools · Privacy · Legal notice

Embed this tool on your site

Paste this code into your page. The tool runs in the visitor's browser, free and without signup. The height adjusts to the content. All tools you can embed

What the HTML minifier does

Minifying HTML makes a page file smaller without changing what a visitor sees. Indentation, line breaks and comments help people read the markup, but browsers do not need them. This tool removes them, keeps everything that changes the page, and shows the size before and after in bytes, with the percent saved. With the inline options on, the CSS in style elements and the JavaScript in script elements are minified as well, with the same libraries as the CSS and JS minifier. To make minified HTML readable again, use the HTML formatter.

Which whitespace is safe to remove

MDN's guide to whitespace explains how browsers treat it with the default CSS: sequences of spaces collapse to a single space, spaces at the start and end of a line are removed, and a space right after another space is ignored, even across two inline elements. Block elements such as div, p and li each start their own line, so the whitespace between them is not shown. Between inline or inline-block elements it is: MDN notes that whitespace there results in spaces in the layout, like the spaces between words.

That gives the three whitespace settings:

  • Collapse and trim turns every run of spaces, tabs and line breaks into one space, then removes that space where it touches a block element or the start or end of a block. Between inline elements, as in <b>a</b> <i>b</i>, the space stays.
  • Collapse only turns every run into one space and never removes a space. It saves less, but it is safe for layouts that make list items or other block elements inline-block.
  • Keep leaves all whitespace as it is, for when you only want to remove comments or minify inline code.

The content of pre, textarea, title, script and style is never changed by these settings. In pre and textarea every space and line break is shown, and script and style hold code that has its own rules.

Inline CSS and JavaScript

Style elements go through csso 5.0.5 (MIT license) and classic and module scripts through terser 5.51.2 (BSD-2-Clause license). Both load only when your HTML has a style or script element, and run in a background thread. Top-level names in classic scripts are not renamed, because other scripts and onclick attributes on the page may use them. JSON scripts, such as structured data in application/ld+json, lose only the whitespace between values; strings and numbers stay exactly as written. Script types the tool does not know, such as HTML templates, stay as they are.

Any block that cannot be minified safely is kept exactly as written and listed with its line: code with a syntax error, CSS that csso cannot fully read, JSON that does not parse, and scripts that contain <!--, which HTML parses in a special way inside a script.

What it does not do

  • It does not remove quotes around attribute values, end tags that HTML lets you leave out, or default attribute values. Those save a few more bytes but are easy to get wrong.
  • It does not change attribute values, including style attributes and event handlers like onclick, URLs or the words of your page. Only the spaces between attributes become one space.
  • MDN notes that the DOM keeps whitespace text exactly as written. Removing it means fewer text nodes, so a script that walks every child node, text included, sees fewer of them.
  • It does not know your CSS. If an element gets white-space: pre from a stylesheet, its whitespace is collapsed like any other.
  • Tags that are not closed are listed under the result with their line, as the HTML formatter reports them. The output keeps exactly the tags you wrote.

A worked example

Press Fill in an example. The page has a doctype, a head with a comment and a style element, a paragraph with bold and italic words, a pre block with opening hours and a small script, formatted with two-space indentation: 742 bytes. With the default settings it becomes 464 bytes, 37.5% smaller. The comment is gone, #ff0000 became red, the script function's parameter became a single letter, and the pre block kept its three lines exactly. The space between "Save" and the bold 20% stayed, and the three spaces before "this week" became one. With inline CSS and JavaScript switched off, the result is 641 bytes, 13.6% smaller.

Limits

Files up to 10 MB can be opened and are read as UTF-8 text. Everything runs on your device, so very large pages take longer on phones and tablets. The preview shows the first 200,000 characters; Copy and Download include everything. Elements nested more than 1,000 levels deep are not minified. Always open the minified page and check it before you replace the live file.

How to use the HTML minifier

  1. 1Paste your HTML, drop a file on the box or open one.
  2. 2Choose how to treat whitespace and comments, and whether to minify inline CSS, JavaScript and JSON.
  3. 3Read the result: bytes before and after, percent saved and any blocks kept as written.
  4. 4Copy the HTML or download it as a .html file.

Sources

Frequently asked questions

Is my HTML uploaded anywhere?

No. The minifier reads your markup as text in your browser and never loads the page, so no script in it runs and no image loads. Inline CSS and JavaScript are minified in a background thread on your device. Copy link shares only the options.

Will the minified page look the same?

With the default CSS of a browser, yes: the tool only removes whitespace where browsers do not show it, next to block elements such as div, p and li, and keeps one space between words and inline elements like links and bold text. If your CSS makes block elements inline-block, the space between them can matter; use Collapse only, which never removes a space.

What happens to pre, textarea, script and style?

Their content stays exactly as written, because every space and line break in pre and textarea is shown, and script and style hold code. With the inline CSS and JavaScript options on, the code in style and script elements is minified with csso and terser; a block that cannot be read is kept as written and listed with its line.

Are conditional comments removed?

Not by default. Comments that start with <!--[if and the matching <![endif]--> target specific programs, so Keep conditional comments leaves them in while all other comments go. Switch it off to remove them too.

Does it remove quotes or optional tags?

No. Attribute values keep their quotes and every tag you wrote stays, including end tags HTML allows you to leave out. Those changes save a few more bytes but are easy to get wrong, so this tool keeps to changes that are safe for any page.

How are the sizes counted?

In bytes of UTF-8 text, the way the file is saved. 1 KB is 1,024 bytes here. The percent saved compares the minified size with the original size.