Code Formatters

HTML Formatter

Format and beautify HTML with Prettier, including inline CSS and JavaScript, with parse errors by line and a careful minify mode.

  • Free
  • No sign-up
  • Runs in your browser

HTML Formatter

Press Ctrl+Enter (⌘+Enter on a Mac) to format. The Prettier 3.9.10 engine is loaded from this site the first time you format, then runs in your browser.

Runs in your browser. Your inputs never leave your device.

Quick Answer

Paste your HTML and press Format. Prettier puts each block element on its own line, indents nested elements by 2 spaces (or 4, or tabs), formats the CSS inside style tags and the JavaScript inside script tags, and keeps inline elements such as span and a on the same line when it can. If the markup cannot be parsed, for example a closing tag that does not match, you get the line and column instead.

How to Use

  1. Paste your HTML. Paste a page or a fragment into the input box, or load the example.
  2. Choose the options. Pick Format or Minify, the indent, the print width and the whitespace sensitivity.
  3. Format. Press Format code or Ctrl+Enter. Errors show the line and column; warnings list elements that are never closed.
  4. Copy the result. Copy the formatted HTML from the output box.

HTML formatting conventions

HTML has no single official style guide, but most teams and style guides, including Google's HTML/CSS style guide, agree on a few habits that make markup easy to read and review:

  • One block element per line, with its children indented one level. Short elements whose content fits on the line, such as <li>Notebook</li>, stay on one line.
  • Consistent indentation, usually 2 spaces because HTML nests deeply. Never mix tabs and spaces in one file.
  • Lowercase element and attribute names and double-quoted attribute values, such as <a href="/contact/">.
  • Long tags wrapped one attribute per line when they go past the line width, with the closing > on its own line.
  • Embedded CSS and JavaScript indented inside their <style> and <script> tags and formatted like any other CSS or JavaScript file.

The formatter changes layout only. It does not rename classes, reorder attributes or fix invalid nesting, and the page looks the same afterwards, except where you choose the ignore whitespace setting.

Why inline elements are whitespace-sensitive

Browsers collapse runs of spaces, tabs and line breaks in text into a single space, but they do not remove that space entirely. So <b>Price:</b><span>$5</span> displays as "Price:$5", while the same two elements on separate lines display as "Price: $5". For block elements such as <div> and <p> the space is invisible, so adding line breaks around them is always safe.

Prettier's css setting uses each element's default CSS display value to decide where breaks are safe. That is why you will sometimes see a closing > moved to the start of the next line: it lets the line break sit inside the tag, where it cannot become a space. strict treats every element as inline, which is safest for pages that change display with CSS. ignore formats freely and is fine for markup where small spacing changes do not matter.

What minify does, and what it does not

Minify mode is deliberately conservative so it never changes how a page looks:

  • It removes HTML comments, except conditional comments such as <!--[if IE]>.
  • It removes whitespace next to block elements and collapses whitespace between inline elements and text to one space.
  • It leaves <pre>, <textarea>, <script> and <style> exactly as written.
  • It does not minify inline CSS or JavaScript, remove optional tags or attribute quotes, or shorten URLs. Build tools such as html-minifier-terser do that as part of a production build.

Servers usually compress HTML with gzip or Brotli anyway, which removes most of the benefit of whitespace removal. Minifying by hand is most useful for small snippets, such as an email template or embed code.

Format JSON with the JSON formatter, Python with the Python formatter and Java with the Java formatter. To compare two versions of a file, use the text diff checker.

How the HTML Is Formatted

Parse HTML → tree of elements → print with indentation and line width, keeping whitespace where the browser would render it

The page uses Prettier's HTML printer, loaded from this site the first time you format. It parses the markup into a tree, then prints block elements (div, p, ul, section …) on their own lines with one level of indentation per nesting level, and breaks long lines at the print width you choose. Inline elements (span, a, strong, em …) are whitespace-sensitive: a line break between them shows up as a space on the page, so by default Prettier only adds breaks where the CSS display value says it is safe. CSS inside <style> is formatted with Prettier's CSS printer and JavaScript inside <script> with its JavaScript printer. Elements that are never closed are closed by the parser and reported as warnings with their line number.

  • Indent: 2 spaces (the most common choice for HTML), 4 spaces or tabs.
  • Print width: The line length Prettier aims for before wrapping attributes or content.
  • Whitespace sensitivity: css respects each element's default display; strict treats all whitespace as meaningful; ignore treats none of it as meaningful.

Worked Examples

Nested elements on one line

Before

<div><p>Hi<span>there</span></p></div>

After

<div>
  <p>Hi<span>there</span></p>
</div>
  1. div and p are blocks, so each starts a new line and the p is indented
  2. span is inline and touches the text, so adding a line break would add a visible space; it stays where it is

Inline CSS and JavaScript

Before

<style>body{margin:0;color:#222}</style><script>function hi(name){return 'Hi '+name}</script>

After

<style>
  body {
    margin: 0;
    color: #222;
  }
</style>
<script>
  function hi(name) {
    return "Hi " + name;
  }
</script>
  1. The style and script contents are handed to Prettier's CSS and JavaScript printers

A div inside a paragraph

Before

<p>Intro
<div>Block</div>
</p>
  1. A p cannot contain a div: the parser closes the p when the div starts
  2. The final </p> then has no open p to close

Result: Line 3, column 1: Unexpected closing tag "p"

Common Mistakes to Avoid

  • Putting a block element inside a paragraph: <p> may only contain phrasing content. A <div>, <ul> or <table> inside it ends the paragraph early, so styles and scripts aimed at the paragraph miss the rest of its content. Use a <div> as the wrapper instead.
  • Leaving elements unclosed: Browsers repair missing end tags, but not always where you expect, which can push later content inside the wrong element. Only a few elements, such as <li>, <p> and <td>, have optional end tags.
  • Adding line breaks between inline elements: <a>Home</a><a>About</a> and the same links on two lines render differently: the line break becomes a space. That is why the formatter sometimes keeps inline elements together or uses the unusual > placement at the start of the next line.
  • Expecting minify to optimise everything: Minify here only removes comments and the whitespace between tags. It does not shorten inline CSS or JavaScript, remove optional tags or quotes, or rewrite attributes. Use a build tool for production minification.
  • Formatting templates as plain HTML: Template syntax such as {{ }}, {% %} or JSX is not HTML. The formatter may move or break it. Format templates with a tool that understands that template language.

Frequently Asked Questions

How do I format HTML online?

Paste the HTML into the box and press Format, or Ctrl+Enter. Choose the indent, print width and whitespace sensitivity first if you need a particular style.

Why did the formatter not put every tag on its own line?

Inline elements like span, a and strong are whitespace-sensitive: a line break between them can show up as a visible space. With the default css sensitivity, Prettier only breaks lines where it cannot change how the page looks. Choose ignore if you do not mind small spacing changes.

Does it format CSS and JavaScript inside HTML?

Yes. The contents of style tags are formatted as CSS and the contents of script tags as JavaScript. If that code has a syntax error, it is left as it was.

What does 'Unexpected closing tag' mean?

The parser found a closing tag with no matching open element, usually because an element was closed in the wrong order or a block element such as a div was placed inside a p, which closes the p early. The error gives the line and column.

Can I minify HTML here?

Yes, choose Minify. It removes comments and the whitespace between tags, keeping one space between inline elements and leaving pre, textarea, script and style untouched. It does not compress inline CSS or JavaScript.

Is 2 or 4 spaces better for HTML?

Both are common. 2 spaces is the most widely used for HTML because markup nests deeply; Google's HTML style guide uses 2 spaces. Pick one and use it consistently.

Is my HTML uploaded?

No. The formatter is downloaded from this site and runs in your browser. Your code is not sent, stored or added to the page address; only your option choices are remembered on this device.

Sources & References