DEVELOPER TOOL

HTML Formatter & Beautifier

Paste messy or minified HTML to beautify it with clean indentation, or minify it into one compact line. Your markup is shown as text only, never rendered, and never leaves your browser.


  

About this tool

HTML that comes out of a CMS, an email builder, a minified build or a "view source" window is often one enormous line, or a mix of tabs and spaces that nobody can follow. This HTML formatter re-indents the markup so the nesting is visible at a glance, and its Minify mode does the reverse for when you need the smallest file. Beautifying is done by the open-source js-beautify library (version 1.15.1, MIT licence), loaded from this site. The result is placed in the output box as plain text, so the page never runs or displays your markup, and nothing is uploaded.

How to use the HTML Formatter

  1. Paste your HTML: a full page or just a fragment such as a single <div>. A small sample page is loaded to start with.
  2. Pick an indent (2 spaces, 4 spaces or a tab) and, if you like, a line length to wrap long lines at.
  3. Set the options. Untick Format inline CSS and JavaScript to leave the contents of <style> and <script> blocks exactly as they are.
  4. Click Beautify HTML, or Minify for a compact version. The status line shows the line and character counts.
  5. Copy or download the result as formatted.html or minified.html.

What beautifying changes, and what it leaves alone

Beautifying only adds, removes or changes whitespace between tags. Tag names, attributes, attribute order, quotes and text are not rewritten, and the tool does not fix broken markup: an unclosed <p> stays unclosed and simply affects the indentation that follows. In detail:

Why inline elements are indented so carefully

In HTML, whitespace in text is mostly collapsed, but not ignored. MDN's guide to how whitespace is handled explains that a run of spaces, tabs and line breaks is drawn as a single space, and that whitespace between adjacent inline or inline-block elements produces a visible space, just like the space between two words. So <b>bold</b><i>italic</i> renders as "bolditalic", while the same two tags on separate lines render as "bold italic". A formatter that put every element on its own line would quietly add those gaps. That is why the beautifier keeps a run of inline elements on one line and only breaks lines around block-level elements, where whitespace normally has no visible effect.

How the Minify option works

Minify is this site's own conservative code, not part of js-beautify. It reads the HTML into tags and text and then:

Because script and style contents are left alone, run them through the JavaScript Minifier and CSS Minifier separately if you want those smaller too. One caveat: if your CSS turns list items or other block tags into inline-block, or gives an element white-space: pre, the whitespace minify removes or collapses there may have been visible. Check such pages after minifying.

For example, this input:

<ul>
  <li>One   <b>two</b></li>
  <!-- note -->
  <li>Three</li>
</ul>
<pre>  keep   this</pre>

minifies to:

<ul><li>One <b>two</b></li><li>Three</li></ul><pre>  keep   this</pre>

Worked example

The sample in the tool is a small page written on two lines: everything up to the first line of the <pre> block, then the rest. With the default settings (2-space indent, no wrapping, inline CSS and JavaScript formatted, blank lines before <head>, <body> and </html>), Beautify HTML produces:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Team Page</title>
  <style>
    .card {
      padding: 16px;
      border: 1px solid #ddd
    }
  </style>
</head>

<body>
  <div class="card">
    <h1>Our <em>small</em> team</h1>
    <p>Write to <a href="mailto:hello@example.com">hello@example.com</a>.</p>
    <ul>
      <li>Design</li>
      <li>Engineering</li>
    </ul>
    <pre>  npm install
  npm test</pre>
  </div>
  <script>
    document.querySelector('h1').addEventListener('click', function() {
      alert('Hi')
    })
  </script>
</body>

</html>

Notice three things. The h1 and p lines keep their em and a tags inline, so no new spaces appear in the text. The second line inside <pre> is not indented to match the code around it, because that would change what the visitor sees. And the CSS rule and the click handler have been expanded and indented by the CSS and JavaScript beautifiers.

Common mistakes

Common use cases

Formatting data instead? Try the XML Formatter or the JSON Formatter. To see rendered output from Markdown, use the Markdown Previewer.

Frequently asked questions

What is the difference between an HTML formatter and an HTML beautifier?

Nothing in practice. Both names describe a tool that re-indents HTML and puts block elements on their own lines so the structure is easy to read. Some people also say pretty printer. The opposite operation, removing the layout whitespace to make the file smaller, is usually called minifying.

Is it safe to paste HTML that contains scripts?

Yes. The tool treats your HTML purely as text. The formatted result is written into the output box as text, so no tag in it is rendered, no script runs and no image or link is loaded. The processing also happens entirely in your browser, so the markup is not sent to a server.

Can I format just part of a page, like a single div?

Yes. Fragments work as well as full documents. Paste a single element, a table or a list and the tool indents it the same way. Nothing such as html, head or body tags is added around a fragment, so you can paste the result straight back where it came from.

Why does the formatter leave some tags on the same line?

Inline elements such as a, span, b, em and img are kept on the line of text they belong to, because a line break between inline elements is shown as a space in the browser. Moving them onto separate lines could add gaps between words or icons that were not there before.

Does minified HTML load faster?

Usually a little, because the file is smaller. The gain is often modest when the server already compresses responses with gzip or Brotli, since compression handles repeated spaces well. Minifying matters more for HTML that is embedded somewhere else, such as inside a JSON value, an email or a size-limited field.

How do I format HTML in VS Code?

VS Code includes an HTML formatter that is based on js-beautify. Open the file and use the Format Document command from the Command Palette, or its keyboard shortcut. This page is handy when you are not in your editor, or when you want to try options before changing your editor settings.