Minify HTML Code
Remove comments, extra whitespace and line breaks from your HTML to reduce the file size, while the page structure and the way the page looks stay the same.
Minify your HTML code by removing comments and extra whitespace. Fast, free and no signup.
Add your code to the editor or load the sample.
Pick what to remove: comments, whitespace, inline CSS and JavaScript.
Copy the minified code or save it as an HTML file.
Looking for a fast and free HTML minifier that you can use right now, without installing anything? Paste your HTML code into the editor above, choose the options you need and click Minify HTML. In a second you get a smaller version of the same page, together with the exact number of bytes you saved. The tool removes HTML comments, collapses extra spaces, tabs and line breaks, can optionally remove the gaps between tags, and can also compress the CSS inside style tags and clean up the JavaScript inside script tags. Content where whitespace really matters, such as pre blocks, textarea fields and JSON-LD structured data, is handled with care. Everything runs in your own browser, so your code is never uploaded, there is no signup, and there is no watermark. Whether you are a web developer who wants a lighter page, a blogger who wants a better page speed score, a marketer who needs a compact email template, or a student who is learning how HTML works, this online HTML compressor gives you clean, ready-to-use output in seconds. You can copy the minified HTML to your clipboard or download it as an .html file.
People find this tool by searching for html minifier, minify html online, compress html, html compressor, html minify tool, reduce html size or remove whitespace from html. The goal is always a lighter page that downloads faster and helps your page speed score. This online HTML minifier shows how many bytes you saved, lets you control each step and keeps content such as pre blocks and JSON-LD safe. It runs in your browser on Windows, Mac, Linux, Android and iPhone, so you can compress a template without installing a build tool.
Remove comments, extra whitespace and line breaks from your HTML to reduce the file size, while the page structure and the way the page looks stay the same.
Compress the CSS inside style tags by removing comments and unnecessary spaces, so the whole page becomes lighter and loads faster.
Strip block comments, full-line comments and blank lines from script blocks. Pre, textarea and JSON-LD content is handled with care.
The default options never touch the content of pre and textarea tags and keep Internet Explorer conditional comments, so your layout does not break.
After every run you see the size before, the size after and the percentage saved, so you can decide which options are worth using.
Your HTML is processed only in your browser. Nothing is sent to a server or stored, and there is no account to create.
Minifying a page with this tool takes less than a minute, and you do not need to know anything about build tools or the command line. Follow these steps to compress your HTML code.
HTML minification is the process of removing every character from an HTML document that a web browser does not need in order to display the page. When developers write HTML, they add indentation to show the structure, line breaks to keep the code readable, and comments to explain why something is there. These characters are very useful for people, but a browser ignores most of them, so they only add weight to the file. A minifier takes the readable source code and produces a compact version that works exactly the same way. The result is the same page for the visitor, but a smaller file that travels faster over the network.
You will also see this process called HTML compression, HTML compacting, HTML optimization or "make HTML smaller". These names describe the same idea. It is different from file compression such as gzip or Brotli, which a web server applies while sending the file. Minification changes the actual content of the file, and server compression packs the file during transfer. The two work well together, and we explain the difference in a table below.
Browsers follow a set of rules when they read HTML. In normal text, any group of spaces, tabs and line breaks is treated as one single space, so ten spaces look exactly like one. Comments are skipped completely and are never shown to the visitor. A minifier uses these rules and does three main jobs. First, it finds and removes comments. Second, it replaces every group of whitespace with a single space. Third, it can remove the space that sits only between two tags. Before it does any of this, the tool sets aside the parts of the document where whitespace has a meaning, such as pre, textarea, script and style blocks, handles them with their own rules, and puts them back in the right place at the end. This protect-and-restore step is what keeps the page safe.
This is a small page written the way most developers write it, with indentation, a comment, a style block and a pre block.
<!-- Header -->
<header>
<h1>Hello, World!</h1>
<p>
This is a sample
paragraph.
</p>
</header>
<style>
/* Main heading */
h1 {
color: #4f46e5;
margin: 0 auto;
}
</style>
After using the HTML minifier with the default options, the comments are gone, the whitespace is collapsed and the CSS is compressed, but the page looks exactly the same in the browser.
<header> <h1>Hello, World!</h1> <p> This is a sample paragraph. </p> </header><style>h1{color:#4f46e5;margin:0 auto}</style>
It is important to know exactly what changes in your code. The table below shows how each part of a page is treated.
| Part of the page | What the tool does | Is it safe? |
|---|---|---|
| HTML comments | Removed, when Remove comments is on | Yes. Visitors never see comments. |
| Internet Explorer conditional comments | Kept, because old browsers read them as code | Yes |
| Indentation, tabs and line breaks | Collapsed into one space | Yes, in normal text |
| Space between two tags | Removed only when you turn the option on | Check inline elements |
| Content of pre and textarea | Left exactly as written | Yes |
| Style blocks | Comments and extra spaces removed | Yes |
| Script blocks | Block comments, full-line comments and blank lines removed | Yes, basic cleanup only |
| JSON-LD structured data | Compacted as valid JSON, left alone if it cannot be parsed | Yes |
| Template scripts (text/template, text/x-) | Not changed | Yes |
Each option in the settings panel does one specific job. You can combine them in the way that fits your project.
| Option | What it does | Best used when |
|---|---|---|
| Remove comments | Deletes standard HTML comments and keeps conditional comments | Almost always. It is the safest way to save bytes. |
| Collapse whitespace | Turns every group of spaces, tabs and line breaks into one space | For every normal web page and template |
| Remove spaces between tags | Deletes the space that sits only between a closing tag and the next opening tag | When you need the smallest output and your layout does not depend on gaps between inline elements |
| Minify inline CSS | Removes comments and unnecessary spaces inside style blocks | When your page has critical CSS or embedded styles |
| Minify inline JavaScript | Removes comments and blank lines inside script blocks | When your page has small inline scripts |
There is one place where whitespace has a visible effect in HTML, and it is the space between inline elements. Links, spans, buttons, images and text in bold or italic are inline elements, and a single space between them is shown as a small gap. Consider two links written on separate lines. The browser shows a space between them, because the line break counts as a space. If you remove all spaces between tags, the two links can touch each other and look like one word. This is why the option Remove spaces between tags is turned off by default.
The normal Collapse whitespace option is safe in these cases, because it keeps one space where there was whitespace, so the gap stays the same. If you do use the more aggressive option, test your navigation menus, button groups, icon lists and any other row of inline items. If something looks too close, the quick fix is to turn that option off again, or to add the spacing with CSS margin or gap instead of relying on a space in the code.
Every byte of your page has to be downloaded before the browser can show it. The HTML document is the first file a visitor requests, and everything else, such as images, stylesheets and scripts, is discovered only after the browser starts reading it. This makes HTML one of the most important files for how fast a page feels. Making it smaller helps in several practical ways.
Many people ask whether minifying is still useful when their server already uses gzip or Brotli. The answer is yes, because they do different jobs and the best results come from using both.
| Feature | HTML minification | Gzip or Brotli |
|---|---|---|
| Where it happens | On your file, before you publish | On the web server, while sending the file |
| What it does | Removes characters that are not needed | Packs the data with a compression algorithm |
| Does the file change? | Yes, the saved file is smaller | No, the file on the server stays the same |
| Browser needs to unpack it? | No | Yes, automatically |
| Can you use both? | Yes. Minify first, and then let the server compress the result for the smallest download. | |
Because gzip and Brotli are very good at packing repeated patterns such as spaces and indentation, the extra saving from minifying a page that is already compressed is smaller than the saving on the raw file. It is still worthwhile, because comments, inline styles and unneeded characters are removed completely before the transfer starts, and the browser also has less text to parse.
The saving depends mostly on how the original code was written. A page with deep indentation, many comments and large inline styles will shrink more than a page that was already compact. The table gives a rough idea of what to expect. These are general estimates and not a promise, so use the result line in the tool to see the real number for your own code.
| Type of code | Typical saving | Why |
|---|---|---|
| Hand-written page with deep indentation and comments | Often 15% to 35% | Lots of indentation and comment text to remove |
| CMS or theme output that is already fairly compact | Often 5% to 15% | Less whitespace to remove |
| Page with large inline CSS | Higher when CSS minify is on | Style blocks lose comments and spaces |
| Email template built with nested tables | Often 20% or more | Many nested tags with indentation |
| Code that is already minified | Close to 0% | Nothing left to remove |
Many pages contain CSS inside style tags and scripts inside script tags, and these blocks can be a large part of the document. When Minify inline CSS is on, the tool removes comments, collapses spaces and removes the last semicolon before a closing brace, which gives compact style rules that work the same way in every browser. When Minify inline JavaScript is on, the tool removes block comments, lines that contain only a comment, and blank lines, and it trims the indentation. It does this on purpose without joining lines together, because joining lines in JavaScript can break code that relies on automatic semicolon insertion.
This means the JavaScript cleanup is basic and safe, and it is not a replacement for a full JavaScript minifier that shortens variable names. For a large script it is better to use a dedicated tool or a build step. Structured data in script tags with the type application/ld+json is compacted as valid JSON when possible, and scripts with template types are not touched. If you work with separate files, try our CSS Minifier and JavaScript Minifier for deeper compression.
A good minifier knows where it must stay out of the way. In a pre tag, spaces and line breaks are part of the content, because the browser shows them exactly as written, which is why it is used for code samples and preformatted text. In a textarea, the text between the tags is the default value that the visitor sees in the form field. Changing the whitespace in either of them would change what the visitor reads. This tool protects both of them, and it also protects the content of script and style blocks until they are processed with their own rules. You can see this in the sample code, where the pre block keeps its original spacing even after minification.
On the other hand, do not minify the file that you are still editing. Work on the readable version, and create the minified copy as the last step before you publish.
On a large project, minification is usually automated. Modern bundlers and static site generators have plugins that minify HTML during the build, and many content management systems and caching plugins have a setting that minifies the pages they deliver. Laravel projects can use a middleware or a package to minify the final response. Automation is the best choice for sites that change every day, because nobody has to remember to do it by hand. An online tool like this one is perfect for everything else: one-time pages, email templates, snippets for clients, quick tests and learning.
The most common mistake is removing the spaces between tags without testing the page, which can make links or buttons touch each other. The second is minifying a file and then losing the original, which makes later edits hard. Other problems are minifying code that has syntax errors, expecting full JavaScript compression from a basic cleanup option, and removing comments that a template engine or a content management system needs as markers. If your system uses special comments as placeholders, check that they still work after minification, or turn off Remove comments for that file.
Continue your optimization work with the CSS Minifier and the JavaScript Minifier for separate files, or use the HTML Beautifier to turn compact code back into a readable layout.
Minifying is safe in most cases, but a few layouts and scripts are sensitive to whitespace. If something looks different after minifying, check this table.
| Problem | Likely cause | How to fix it |
|---|---|---|
| Inline items such as links or buttons touch each other | Remove spaces between tags removed a meaningful gap | Turn that option off and minify again |
| A script stops working | The basic JavaScript cleanup changed code that depends on its layout | Turn off Minify inline JavaScript and use a dedicated JavaScript minifier |
| Some comments are still there | IE conditional comments are kept on purpose | Remove them by hand if you do not need old Internet Explorer support |
| The file is only a little smaller | The source was already compact, or the saving is mostly whitespace | Compression with gzip or Brotli on your server adds the larger gain |
| Layout shifts after minifying | The design relied on whitespace between inline-block elements | Keep the gaps between tags, or add margin in CSS instead |
| You need to read the code again | Minified code has no line breaks | Keep your readable source and use the HTML formatter to expand a copy |
Our HTML minifier works in your web browser, so you can compress HTML on Android, iPhone, Windows, Mac and Linux with nothing to install. That is handy when you need to shrink an email template, a snippet for a CMS or an embedded widget without setting up Node.js or a build pipeline. Paste the code, choose your options and copy the smaller version.
Your code is processed inside your browser. It is not uploaded, saved or stored on a server, and it is gone when you close or refresh the page. Minifying changes whitespace and comments, so keep a copy of your original file and test the result in a browser before you publish. As a general habit with any online tool, remove API keys and private URLs from code before you paste it.
Our free HTML Minifier works directly in your browser, without signup, without limits and without a watermark. Your code stays on your device while it is processed, so you can minify pages, templates and snippets quickly and safely whenever you need. Paste your code, pick your options, check the savings and copy the result. It is the easiest way to make your HTML smaller and your pages faster.
Find answers to common questions about minifying HTML, what gets removed and whether it is safe for your pages.
An HTML minifier is a tool that reduces the size of HTML code by removing comments, extra spaces and line breaks that browsers do not need. The page keeps the same structure and looks the same, but the file becomes smaller.
Paste your HTML into the editor, choose the options you want and click Minify HTML. The minified code appears below, and you can copy it or download it as an HTML file.
In most cases no. Removing comments and collapsing whitespace does not change the display. The option to remove spaces between tags can remove the visible gap between inline elements, so check your page after using it.
Yes. You can enable options to compress style blocks and clean script blocks. JavaScript cleanup is basic and removes comments and blank lines. For advanced script compression, use a dedicated JavaScript minifier.
No. Content inside pre and textarea tags is kept exactly as written, because spaces and line breaks are meaningful there.
No. The minification runs in your browser, so your code is not sent to a server or saved. It stays on your device.
Minification removes comments and formatting, so the original layout cannot be restored exactly. Keep a copy of your readable source file. You can use an HTML formatter or beautifier to make minified code easier to read again.
Minify HTML here, then use the CSS minifier, the JavaScript minifier, the HTML formatter or the HTML beautifier in seconds. Explore free developer tools to format, compress and debug your code in one place.
Explore All Developer Tools →