HTML Formatter

Paste HTML and get it back indented, or minified if you are going the other way. Use it on a saved page, or on template output that arrived from a CMS as one long line. Format keeps inline elements such as links on the same line as their text, so the rendered spacing does not change.

Input HTML
Formatted / Minified HTML

1 HTML Formatter example

Input (Minified)

1
<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello World</h1><p>This is a test.</p><div><ul><li>Item 1</li><li>Item 2</li></ul></div></body></html>

Output (Formatted)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <h1>Hello World</h1>
    <p>This is a test.</p>
    <div>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
        </ul>
    </div>
</body>
</html>

2 What the HTML Formatter helps with

CMS and template output

Generated markup often arrives as a single line. Indented, the nesting is visible, and a missing closing tag shows up as a block that never steps back out.

Diff-friendly source

Format both versions before you diff them. With the same indentation on each side, the diff shows only the lines whose content changed.

How to use the HTML Formatter tool

  1. Paste markup into the left editor, or click Upload File for an .html file. Load URL fetches a live page, provided that server allows cross-origin requests.
  2. Choose 2 Spaces, 4 Spaces or Tab from the indentation dropdown.
  3. Click Format. The right panel shows the beautified HTML with one element per line and nested elements indented.
  4. Click Minify instead to strip comments and the whitespace between tags. It collapses whitespace everywhere, including inside pre and inline scripts, so use it only on markup that does not depend on that spacing.
  5. Click Copy Result to copy the output, or Clear to start over. Dummy Data loads a small sample page to try it on.

How to format and minify HTML in JavaScript, PHP and Python

JavaScript (js-beautify)

// npm i js-beautify
const { html: beautify } = require("js-beautify");

const raw = "<div><p>Hello <b>world</b></p><ul><li>One</li><li>Two</li></ul></div>";

console.log(beautify(raw, {
  indent_size: 2,
  wrap_line_length: 0,
  unformatted: ["b", "i", "code", "span"], // keep inline elements on one line
}));

// Minify: strip comments and whitespace between tags
console.log(raw.replace(/<!--[\s\S]*?-->/g, "").replace(/>\s+</g, "><").trim());

This is the library behind the tool itself. It runs in Node.js and as a browser bundle.

PHP

<?php
$html = '<div><p>Hello <b>world</b></p><ul><li>One</li><li>Two</li></ul></div>';

$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;

// The flags stop libxml wrapping the fragment in <html><body> and a doctype
libxml_use_internal_errors(true);
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

echo $doc->saveHTML();

libxml's HTML serializer puts each block element on its own line but does not indent nested elements. For real indentation, use tidy (ext-tidy).

Python (BeautifulSoup)

# pip install beautifulsoup4
from bs4 import BeautifulSoup

raw = "<div><p>Hello <b>world</b></p><ul><li>One</li><li>Two</li></ul></div>"

soup = BeautifulSoup(raw, "html.parser")
print(soup.prettify())          # one tag per line, indented

# Minify
import re
print(re.sub(r">\s+<", "><", str(soup)).strip())

prettify() puts every tag on its own line, inline ones like <b> included, which changes the whitespace the browser renders. Use it for reading, not for output you ship.

Common errors and how to fix them

Extra spaces appear between words after formatting
Inline elements such as <a>, <span> and <b> are whitespace-sensitive. If a formatter puts them on their own lines, the browser renders a space. Keep them on one line, or add them to the formatter's unformatted list.
Contents of <pre>, <textarea> or <script> changed
Whitespace inside these elements is significant. Format here leaves pre and textarea contents alone; Minify does not, so do not minify a page that relies on them.
Unclosed tags are silently closed in the wrong place
Formatters do not validate. A missing </div> makes every following element nest one level deeper. Run the file through the W3C validator to find the real problem.
Load URL fails with a CORS error
The target site does not send Access-Control-Allow-Origin, so your browser blocks the fetch. Save the page from View Source and use Upload File instead.
Minified HTML breaks a layout with inline-block elements
Removing the whitespace between <li> or inline-block items also removes the gap they relied on. Switch to flexbox with gap, or add a margin, so the layout does not depend on whitespace.

Good to know

Keep inline elements on the same line as their text. HTML's whitespace rules are why a formatter can change what a page looks like. Between block elements whitespace is ignored, so newlines and indentation there cost nothing. Inside inline content, any run of whitespace collapses to one space, and that space is rendered.

Minifying HTML saves less than you would expect once gzip or brotli is involved, because runs of whitespace compress to almost nothing. It still helps on very large pages, and it strips comments you would rather not ship to users.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

Will formatting break my HTML?

Format changes only whitespace and indentation, and it keeps inline elements on the same line as their text. The contents of pre and textarea keep their spacing. Minify, however, is a plain whitespace collapse: it also squeezes pre blocks and inline scripts, so a script with // comments will break.

Can it minify HTML as well?

Yes. The Minify button removes comments and the whitespace between tags, and collapses other runs of whitespace to one space.

Does it fix invalid HTML?

No. It formats what you give it and does not close tags or repair structure. Run the result through the W3C validator when you need a correctness check.

Which indentation styles are available?

The dropdown offers 2 Spaces, 4 Spaces or Tab.

Can I format a whole page from a URL?

Yes, with Load URL, if the server allows cross-origin requests. Otherwise save the page and use Upload File.

Related

The CSS formatter does the same for stylesheets, and Purge CSS uses your markup to work out which rules are dead.

More Code Formatters & Beautifiers

Browse all code formatters & beautifiers

Copied to clipboard!