Last reviewed:
What is HTML formatting?
HTML is often written or delivered in a form that is hard to read: minified pages with everything on one line, templates whose indentation drifted over years of edits, markup produced by CMS editors, email builders or JavaScript frameworks. An HTML formatter, also called a beautifier, parses the markup into a tree of elements and prints it back with consistent indentation, one block element per line, and attributes and closing tags in predictable places. Inline <style> and <script> blocks can be formatted with CSS and JavaScript rules at the same time.
The difficult part is whitespace. In HTML, a space or line break between inline elements such as <a>, <span>, <strong> or <button> is rendered as visible space. Adding a line break between two inline buttons can put a gap between them that was not there before, and removing one can join two words together. A formatter that ignores this can change how a page looks.
This tool uses Prettier's HTML printer, which by default follows CSS display rules. It knows that <div> and <li> are block elements, where whitespace around them is not significant, and <a> or <button> are inline, where it is. When an inline element must be broken over lines without adding whitespace, Prettier moves the > of a tag onto the next line. That looks unusual, but the page renders exactly as before.
How to use this tool
- Paste HTML into Input or click Paste. A full document, a fragment or a single element all work. Sample loads a minified page with inline CSS and JavaScript.
- Choose an indent of 2 spaces, 4 spaces or Tab, and a line width of 80, 100 or 120 columns (120 is the default). Lines longer than the width are broken where it is safe to do so.
- Tick One attribute per line to put every attribute of a multi-attribute tag on its own line, which keeps diffs small for elements that change often.
- Tick Ignore whitespace if whitespace between inline elements does not matter to you. The output is cleaner, but rendering can change slightly (see the examples).
- With Format as I type on, the result updates as you edit; otherwise click Format. Errors are shown under the input with their line and column.
- Click Copy, or Download to save
formatted.html.
Examples
Minified navigation, 80 columns
Input:
<ul class="nav"><li><a href="/">Home</a></li><li><a href="/docs/" class="active">Docs</a></li></ul><p>Read the <a href="/guide/">guide</a>, then <strong>start</strong>.</p>
Output:
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="/docs/" class="active">Docs</a></li>
</ul>
<p>Read the <a href="/guide/">guide</a>, then <strong>start</strong>.</p>
The paragraph fits within the width, so its inline content stays on one line.
Inline elements and the Ignore whitespace option
Input:
<div><button class="btn btn-primary" type="submit">Save changes now</button><button class="btn btn-secondary" type="button">Cancel and go back</button></div>
In the input the two buttons touch, with no space between them. At 80 columns with default settings, the output keeps it that way:
<div>
<button class="btn btn-primary" type="submit">Save changes now</button
><button class="btn btn-secondary" type="button">Cancel and go back</button>
</div>
With Ignore whitespace ticked:
<div>
<button class="btn btn-primary" type="submit">Save changes now</button>
<button class="btn btn-secondary" type="button">Cancel and go back</button>
</div>
The second version is easier to read, but the line break between the buttons now renders as a space between them.
One attribute per line
<input
type="email"
id="email"
name="email"
required
placeholder="[email protected]"
autocomplete="email"
/>
The input was a single <input …> tag. Void elements are printed with a closing />, which HTML accepts and ignores.
Inline CSS and JavaScript
<style>.a{color:red;margin:0 auto}</style> becomes:
<style>
.a {
color: red;
margin: 0 auto;
}
</style>
Scripts are formatted with Prettier's JavaScript rules in the same way, including semicolons and double quotes.
Common errors and how to fix them
Unexpected closing tag "span". A closing tag has no matching opening tag, as in<div></span></div>. Remove the stray tag or add the missing opener.Unexpected closing tag "p"with a block inside a paragraph. In<p><div>block</div></p>, HTML's parsing rules close the<p>automatically when<div>starts, so the final</p>has nothing to close. Use a<div>as the outer element, or keep only inline content inside<p>.Unexpected character "EOF". Something was opened and never closed, most often an attribute quote such asclass="a>. Every character after it is read as part of the attribute. Check quotes near the reported position.- Template syntax. Handlebars, Jinja, Liquid, PHP or JSX inside attributes or between tags is not HTML and can cause errors or odd layout. Format the rendered output, or use a formatter for that template language.
- Odd
>on the next line. This is not an error; see the whitespace example above. Tick Ignore whitespace if you accept small rendering changes. - Missing end tags are added.
<ul><li>one<li>two</ul>is valid HTML because</li>is optional; the output adds the end tags. Check the result if your source relied on implied tags.
When developers use this
- Reading minified HTML from a production page, a server response or a static site build to find a specific element.
- Inspecting HTML email templates, which are usually table-based and machine-generated, before editing them.
- Cleaning up a component or template copied from documentation, a design tool export or an older project.
- Normalising snapshot or fixture HTML in tests so that diffs show real changes.
- Checking the structure of markup returned by a CMS or a WYSIWYG editor that renders strangely.
Privacy and how it works
Prettier runs in your browser tab with its HTML, CSS and JavaScript plugins. Your markup is not uploaded, stored or rendered; it is only parsed as text, so scripts inside it are never executed. That is useful when formatting pages from an internal admin tool, emails containing customer data, or templates with embedded keys.
Limitations
- The formatter repairs structure where HTML rules allow, but it is not a validator. It will not report invalid attributes, duplicate IDs or accessibility problems.
- Whitespace-sensitive formatting cannot always produce the prettiest layout without changing rendering. Ignore whitespace trades exact rendering for readability.
- Text inside
<pre>and<textarea>keeps its whitespace, because it is significant there. Prettier may add a line break straight after<pre>, which browsers ignore.
FAQ
Will formatting change how my page looks?
With the default settings it is designed not to: whitespace that affects rendering between inline elements is preserved. With Ignore whitespace ticked, line breaks may add or remove small gaps between inline elements and text.
Why does the output have > at the start of a line?
Prettier needed to break a line between two inline elements that have no space between them. Putting the > of the previous tag on the new line avoids adding a space that would render.
Does it format CSS and JavaScript inside the page?
Yes. <style> blocks are formatted as CSS and <script> blocks as JavaScript. Short attribute values are tidied too: style="color:red;margin:0" becomes style="color: red; margin: 0" and onclick="go( 1 )" becomes onclick="go(1)".
Can it minify HTML?
No, it only expands and tidies. Minification for production is better done by your build tool, which can also remove comments and optional tags safely.
Why were closing tags added to my HTML?
HTML allows some end tags, such as </li> and </p>, to be omitted. The parser builds the same tree a browser would, and the printer writes the end tags explicitly.
Which line width should I choose?
120 columns suits most widescreen editors and is the default here. Choose 80 if your project uses Prettier's default or if you review code side by side.
Can I format a fragment instead of a whole document?
Yes. A fragment such as a single component or a table is formatted on its own; no <html> or <body> is added.