HTML Formatter

Beautify HTML — with its inline CSS and JavaScript — using Prettier.

Runs in your browser Instant No sign-up
Indent
Line width
Whitespace
HTML
Formatted HTML

Paste, type or drop a file — output appears as you type.

Waiting for input
Processed locally · nothing is uploadedProcessed locally

How to use the HTML formatter

  1. Paste, drop or open HTML in the left pane: a full page, a template fragment or minified markup copied from the browser.
  2. Choose Indent: 2 spaces, 4 spaces or Tabs.
  3. Choose Line width (80, 100 or 120 characters). Tags with long attribute lists are wrapped to fit.
  4. Set Whitespace and Keep > on last line if the defaults don’t suit you. The result updates as you type.

The formatter is Prettier, the same engine many editors run on save, loaded on demand in your browser.

Example

<ul><li><a href="/">Home</a></li><li>About</li></ul><p>Hello <b>world</b>!</p>

becomes

<ul>
  <li><a href="/">Home</a></li>
  <li>About</li>
</ul>
<p>Hello <b>world</b>!</p>

Block elements get their own lines. Inline content such as Hello <b>world</b>! stays together, because breaking it would change what the page shows.

Whitespace sensitivity

In HTML, a line break between two inline elements renders as a space. <span>a</span><span>b</span> displays “ab”, but the same spans on separate lines display “a b”. To avoid changing your page, the default Respect CSS display keeps inline elements where they are:

Whitespace <div><span>a</span><span>b</span></div>
Respect CSS display Stays on one line
Ignore Each span on its own line

Use Ignore for markup where spacing doesn’t matter, such as email templates you’ll minify later or XML-like fragments.

Long tags

When attributes don’t fit on one line, each attribute goes on its own line:

<button
  type="button"
  class="btn btn-primary"
  data-action="save"
  aria-label="Save changes"
>
  Save
</button>

With Keep > on last line, the > follows aria-label="Save changes" instead of starting a new line.

Embedded CSS and JavaScript

<style> and <script> contents are formatted too, so a minified page becomes readable in one pass. For stylesheets and scripts on their own, use the CSS Formatter and the JavaScript Formatter, which add options such as quote style and semicolons. For strict, well-formed markup such as RSS feeds or SVG, the XML Formatter validates as it formats.

Tips

  • To read a minified page, open View Source in your browser, copy everything and paste it here. Inline scripts and styles are expanded too.
  • Fragments work as well as full documents: a single component template or an email snippet is formatted without adding <html> or <body>.
  • HTML comments are kept where you wrote them.
  • Prettier is lenient: an unclosed <p> inside a <div> is closed for you rather than reported, so a clean result doesn’t prove the markup is valid. Use the W3C validator for that.

Doing it locally

npx prettier --write "src/**/*.html"
npx prettier --html-whitespace-sensitivity ignore page.html

Frequently asked questions

Is my HTML uploaded?

No. Formatting runs in your browser with the standalone build of Prettier and nothing is uploaded, so internal templates and emails stay on your machine.

Why are some inline elements kept on one line?

With Whitespace set to Respect CSS display, the formatter treats inline elements such as span, a and b like the browser does: adding a line break between them would add visible space on the page. Choose Ignore if the spacing between elements does not matter and you prefer one element per line.

Does it format the CSS and JavaScript inside the page?

Yes. Contents of style tags are formatted as CSS and contents of script tags as JavaScript, using the same Prettier rules as the dedicated CSS and JavaScript formatters.

Does it validate my HTML?

No. Browsers accept imperfect HTML, and so does Prettier: an unclosed p inside a div is closed for you rather than reported. The formatter only reports errors it cannot recover from.

What does Keep > on last line do?

When a tag's attributes are split over several lines, Prettier normally puts the closing > on its own line. With the option on, the > stays at the end of the last attribute, which some teams prefer for JSX-like markup.

Last updated