HTML beautifier

Format minified/messy HTML to improve readability and maintenance.

Overview

HTML is, at its core, the most widely read document format in the world — not by people, but by machines. Tim Berners-Lee created HTML in 1991 as a subset of SGML (Standard Generalized Markup Language), the scientific document markup standard used at CERN. The idea was simple: a set of tags to connect physics articles via hyperlinks. What happened in the thirty years that followed was that this same format was pushed far beyond its creators' intentions, until it became the structure behind interfaces as complex as online text editors, browser-based spreadsheets, and collaborative design tools.

The problem of unreadable HTML is not the original developer's fault. HTML generation tools — CMS platforms like WordPress, Figma exporters, HTML email generators, component libraries — produce functionally perfect markup that the browser parses without complaint. But it becomes a continuous 8,000-character line where a closing `</div>` might be the tenth in a queue and you have no way to know which is the parent of what without a supporting parser.

The HTML beautifier solves exactly that: it uses a real parser (not regex) to analyze the tag tree, calculates nesting levels, and adds consistent indentation and line breaks. The parser may normalize some fragments according to HTML5 rules — meaning the output can be slightly different from the input for malformed markup. It is not a validator: for formal compliance checking against the specification, the W3C Markup Validator is the right tool. For turning an unreadable template into something a human can read and maintain, the beautifier does exactly the job.

Technical deep dive

HTML, SGML, and the 1990s legacy: why the format is still relevant

  • HTML was created as a subset of SGML (Standard Generalized Markup Language), the ISO document markup standard that scientific organizations like CERN had been using since the 1980s. Berners-Lee simplified SGML to create something anyone could learn in hours — and it worked so well it was never replaced.
  • The first HTML spec (1993) had just 18 elements. HTML5, the current version, has over 100. Over the decades the language accumulated semantic elements like `<article>`, `<section>`, `<nav>`, and `<main>` that help search engines and screen readers understand document structure.
  • HTML parsers are intentionally fault-tolerant. This is a design decision, not a bug: in the 1990s, demanding perfect HTML would have made the web inaccessible to most people. To this day, a browser can render HTML with unclosed tags, unquoted attributes, and incorrect nesting.
  • The `<!DOCTYPE html>` declaration at the top of a document tells the browser to use standards mode rather than quirks mode. Quirks mode emulates the behavior of 1990s browsers and remains active when the DOCTYPE is absent.
  • The WHATWG (Web Hypertext Application Technology Working Group) maintains HTML as a 'living standard' — a continuously evolving specification with no version numbers. HTML5 is the colloquial name for improvements made since 2008; technically there is no 'HTML6'.

When to use a beautifier, a validator, and a linter

  • A beautifier reformats HTML without changing semantics or checking correctness. It adds indentation, line breaks, and visual consistency. Useful for debugging, code review, and reading machine-generated templates.
  • The W3C Markup Validator (validator.w3.org) checks structural conformance with the HTML5 spec: detects unclosed tags, illegal nesting, missing required attributes, and obsolete elements. Use it before publishing pages that require proper accessibility and SEO.
  • HTML linters like HTMLHint go beyond syntax: they warn about `<img>` missing `alt`, absence of the `lang` attribute on `<html>`, incorrect heading order (h1 → h2 → h3), and deprecated attributes. Ideal for integration into CI pipelines.
  • Prettier is the most widely adopted multi-language formatter in the industry and includes HTML support. It integrates with VS Code, Neovim, WebStorm, and most editors, and can run as a pre-commit hook via Husky. For team projects, Prettier eliminates code style debates.
  • The beautifier on this page uses a DOM-based approach: HTML is parsed by the browser's engine and the resulting tree is serialized with indentation. This means it may normalize some fragments according to HTML5 parsing rules — useful for understanding what the browser actually interprets.

Tool guide

  • What it does Formats minified HTML into a readable hierarchical structure.

  • When to use Review templates and debug markup without manually reformatting line by line.

Code Snippets

Before and after the HTML beautifier
<!-- BEFORE: CMS-generated markup, no indentation -->
<main><section class="hero"><h1>Title</h1><p>Paragraph with <strong>bold</strong> and <a href="/link">link</a>.</p></section><section class="cards"><article><h2>Card</h2><p>Card text.</p></article></section></main>

<!-- AFTER: beautified with 2-space indentation -->
<main>
  <section class="hero">
    <h1>Title</h1>
    <p>Paragraph with <strong>bold</strong> and <a href="/link">link</a>.</p>
  </section>
  <section class="cards">
    <article>
      <h2>Card</h2>
      <p>Card text.</p>
    </article>
  </section>
</main>
Format HTML with Prettier (Node.js)
import prettier from 'prettier';
import htmlPlugin from 'prettier/plugins/html';

const html = '<main><section><h1>Title</h1><p>Text</p></section></main>';

const formatted = await prettier.format(html, {
  parser: 'html',
  plugins: [htmlPlugin],
  printWidth: 80,
  tabWidth: 2,
  htmlWhitespaceSensitivity: 'css',
});

console.log(formatted);

// VS Code configuration (settings.json):
// "[html]": {
//   "editor.defaultFormatter": "esbenp.prettier-vscode",
//   "editor.formatOnSave": true
// }

Common input

<main><section><h1>Título</h1><p>Texto</p></section></main>

FAQ

What is this tool for?

It runs fully in your browser: useful to validate, format, or convert data in everyday development.

Are my inputs sent to a server?

Processing happens locally with JavaScript. We do not store what you paste into the text areas.

Can I use this for real production data?

Use at your own risk. For secrets (passwords, tokens), prefer controlled environments and your company policies. And always review the generated contents. Never trust blindly things you see on the internet.