HTML Formatter and Minifier
Paste HTML to indent it neatly with 2 or 4 spaces, or minify it by removing unnecessary whitespace. The formatter reads the markup carefully: content inside <pre>, <textarea>, <script> and <style> is kept exactly, void elements such as <img> are handled, and attribute values are never touched. You can keep or remove comments. Nothing leaves your browser.
HTML Formatter and Minifier
The content of <pre>, <textarea>, <script> and <style> is kept exactly as written, and attribute values are never changed. Whitespace is only added or removed next to block elements such as <div> and <p>; between inline elements it is reduced to a single space. If your CSS turns block elements into inline ones (for example display: inline-block on list items), check the page after minifying.
Runs in your browser. Nothing you enter is uploaded or saved.
How to use this tool
- Paste your HTML. Paste a full page or a snippet into the input box.
- Pick options. Choose the indent size and whether to keep or remove comments.
- Format or minify. Press Format for readable code or Minify for a compact version.
- Copy and check. Copy the result and check the page still looks the same before replacing the original.
How whitespace is handled
Browsers collapse runs of spaces and line breaks in normal text into one space, and ignore whitespace next to block elements such as <div> and <p>. The formatter only adds or removes whitespace in those places, and keeps a single space between inline elements such as <a> and <strong>, where a space is visible.
What is left untouched
Preformatted text, text areas, scripts and styles are copied byte for byte, as are attribute values and character references such as . A non-breaking space is never collapsed. Minified CSS or JavaScript inside the page is not changed; use a separate minifier for those.
Formatting broken HTML
Unclosed comments, tags or quoted attribute values stop the formatter with the line number, which makes it a quick way to find them. Optional end tags, such as a missing </li>, are allowed, so check your markup with the W3C validator for full validation.
Frequently asked questions
Is minifying HTML worth it?
The saving is usually small once the server uses gzip or Brotli compression. It matters more for large pages or HTML generated in bulk.
Can formatting change how my page looks?
It is designed not to in normal layouts. If CSS makes block elements display inline, the added line breaks can show as small gaps, so check such pages.
Does it fix invalid HTML?
No. It only changes whitespace and comments. Use the W3C Markup Validation Service to find and fix errors.