HTML Formatter
Beautify messy HTML with clean indentation, or minify it safely, in your browser. Inline CSS and JavaScript are formatted too. Nothing is uploaded.
How to format HTML
- Paste your HTML, or click Open a file to load an
.htmlor.htmfile (up to 10 MB). - Choose the Indentation (2 spaces, 4 spaces or a tab) and whether to Wrap lines at 80 or 120 characters.
- Tick Indent and if you want them indented inside
<html>, and Remove comments to drop<!-- … -->comments. - Click Format (beautify) for readable code, or Minify for the smallest safe version.
- Copy or download the result. The status line shows the size before and after.
The result is displayed as plain text in the output box. Your HTML is never rendered or run on this page, so scripts in the code you paste cannot do anything.
Beautify: readable, consistently indented HTML
Format (beautify) uses js-beautify, the formatter behind many code editors:
- block elements go on their own lines, indented by nesting depth, while inline elements such as
<a>,<b>or<span>stay within the text; - the contents of
<script>and<style>are formatted as JavaScript and CSS; <pre>and<textarea>are left exactly as written, since their whitespace is visible;- consecutive blank lines are reduced to one;
- with a wrap width, long lines of text and long attribute lists are wrapped.
Beautifying does not fix broken markup: an unclosed <div> stays unclosed, and the indentation that follows it shows where the problem is.
Minify: smaller HTML without changing the page
Minify uses a deliberately conservative approach:
- runs of spaces, tabs and line breaks in text become a single space, just as the browser displays them;
- whitespace next to block-level and head elements (
<div>,<p>,<li>,<td>,<meta>,<br>…) is removed, because it is never rendered; - a single space between inline elements is kept, so
<b>big</b> <i>world</i>does not turn into “bigworld”; - comments are removed when Remove comments is ticked, except conditional comments (
<!--[if IE]>) and comments starting with<!--!; - the contents of
<script>,<style>,<pre>and<textarea>are never touched, and attribute values are kept exactly, quotes included.
It does not remove optional closing tags, rewrite attributes or compress inline JavaScript and CSS: those optimisations save a little more but can break pages. For most pages, HTTP compression (gzip or Brotli) saves far more than any minifier, so minification is most useful for HTML embedded in e-mails, templates or JavaScript strings.
Frequently asked questions
Is my code uploaded?
No. Both formatting and minification run in your browser; your code never leaves your computer.
Why did my page look different after minifying?
It should not in normal cases. The one exception is CSS that changes how whitespace is displayed, for example white-space: pre on an element other than <pre> or <textarea>, or an element whose display was changed from block to inline: collapsed spaces there become visible.
Does the formatter validate my HTML?
No. It formats what you give it. To check that the markup is valid, use a validator such as the W3C Nu HTML Checker.
Can I format Vue, Svelte or Django templates?
Mostly. Template tags inside text and attributes are kept, but complex template syntax can confuse the indentation, so check the result.
What does "Indent <head> and <body>" change?
By default, <head> and <body> start at the same level as <html>, a common convention that saves one level of indentation. Tick the option to indent them one level deeper.
Related tools
- XML Formatter Format, minify and validate XML in your browser. Errors are reported with their line and column. Your files are never uploaded.
- JSON Formatter and Validator Free JSON formatter and validator that beautifies, minifies and checks JSON in your browser, with syntax errors reported by line and column.
- Meta Tag Generator Free meta tag generator for the title, meta description, canonical URL, robots and viewport tags, with length checks and correctly escaped HTML.