ToolDeck

HTML Formatter

Format and prettify HTML with proper indentation

Try an example

HTML Input

Formatted HTML

Runs locally · Safe to paste secrets
Formatted HTML will appear here…

What Is HTML Formatting?

An HTML formatter is a tool that performs HTML formatting (also called beautification or pretty-printing): it adds consistent indentation, line breaks, and spacing to HTML markup so that its nesting structure becomes visible. Browsers ignore whitespace when rendering HTML, so a document written on a single line produces the same output as one with careful indentation. The difference is entirely for humans: formatted HTML is easier to read, debug, and maintain.

The HTML specification (maintained by WHATWG as the HTML Living Standard) defines over 110 elements, each with its own content model. Block-level elements like <div>, <section>, and <article> typically start on a new line and indent their children. Void elements like <br>, <img>, and <input> have no closing tag and no children to indent. A good formatter understands these distinctions and applies indentation rules accordingly, rather than blindly adding whitespace after every tag.

Formatting matters most during development. Minified or machine-generated HTML, output from CMS platforms, and markup copied from browser DevTools often arrive as a single dense line. Without proper indentation, finding a missing closing tag or tracing a deeply nested structure takes significantly longer. An HTML formatter turns that wall of text into a properly indented, readable tree structure in one step.

Why Use This HTML Formatter?

Paste any HTML and get properly indented output instantly. No plugins to install, no configuration files to write, no accounts to create.

Instant Formatting
Output updates as you type. Paste minified HTML from any source and see the indented result without waiting for a build step or CLI command.
🔒
Privacy-First Processing
All formatting runs in your browser using JavaScript. Your HTML never leaves your device, so it is safe to paste markup containing API keys, tokens, or internal URLs.
🎯
Configurable Indentation
Switch between 2-space and 4-space indentation with one click. The formatter applies your choice consistently to every nesting level.
📋
One-Click Copy
Copy the formatted output to your clipboard with a single button. Paste it directly into your editor, PR review, or documentation.

HTML Formatter Use Cases

Frontend Development
Format HTML templates before committing to version control. Consistent indentation reduces diff noise in pull requests and makes code review faster.
Backend Template Debugging
Server-rendered HTML from frameworks like Django, Rails, or PHP often outputs unindented markup. Paste the rendered output here to verify nesting and spot unclosed tags.
DevOps & CI Pipelines
Validate that build tools produce well-structured HTML. Format the output of static site generators or HTML email builders before visual inspection.
QA & Testing
Compare formatted snapshots of HTML output between test runs. Proper indentation makes structural differences obvious when diffing two versions of a page.
Data Engineering
Web scraping pipelines produce raw HTML that needs inspection. Format scraped pages to understand their DOM structure before writing extraction selectors.
Learning HTML
Students can paste any website's source code and see how elements nest inside each other. The formatted tree view makes parent-child relationships between tags clear.

HTML Indentation Rules

How an HTML formatter treats each element type depends on its content model. The table below shows common elements and how they are indented with 2-space and 4-space settings. Both produce identical rendering in the browser; the difference is readability.

TagElement Type2-Space Indent4-Space Indent
<div>Block elementNew line, indent childrenNew line, indent children
<span>Inline elementNew line, indent childrenInline with parent text
<br>Void elementNew line, same levelNew line, same level
<img>Void elementNew line, same levelNew line, same level
<!-- -->CommentNew line, same levelNew line, same level
<!DOCTYPE>Document typeFirst line, no indentFirst line, no indent
<script>Script blockNew line, preserve innerNew line, preserve inner
<pre>PreformattedNew line, preserve innerNew line, preserve inner

Formatter vs. Minifier vs. Linter

These three tools operate on HTML but serve different purposes. Formatting and minification are opposite transformations; linting checks for errors without changing whitespace.

Formatter (This Tool)
Adds indentation and line breaks to make HTML readable. Does not change the DOM structure or remove any content. Output renders identically to the input in a browser.
Minifier
Removes whitespace, comments, and optional closing tags to reduce file size. The opposite of formatting. Use it for production builds, not for reading or editing.
Linter (HTMLHint / W3C)
Checks HTML for errors like missing alt attributes, duplicate IDs, or deprecated tags. Reports problems but does not change indentation or formatting.

Code Examples

How to format HTML programmatically in popular languages and tools:

JavaScript (js-beautify)
import { html as beautify } from 'js-beautify'

const ugly = '<div><p>Hello</p><ul><li>One</li><li>Two</li></ul></div>'

const formatted = beautify(ugly, {
  indent_size: 2,
  indent_char: ' ',
  wrap_line_length: 80,
  preserve_newlines: false,
})
// → <div>
// →   <p>Hello</p>
// →   <ul>
// →     <li>One</li>
// →     <li>Two</li>
// →   </ul>
// → </div>
Python (BeautifulSoup)
from bs4 import BeautifulSoup

ugly = '<div><p>Hello</p><ul><li>One</li><li>Two</li></ul></div>'

soup = BeautifulSoup(ugly, 'html.parser')
print(soup.prettify(formatter='minimal'))
# → <div>
# →  <p>
# →   Hello
# →  </p>
# →  <ul>
# →   <li>One</li>
# →   <li>Two</li>
# →  </ul>
# → </div>
Go (goquery + x/net/html)
package main

import (
    "bytes"
    "fmt"
    "golang.org/x/net/html"
    "strings"
)

func main() {
    ugly := "<div><p>Hello</p></div>"
    doc, _ := html.Parse(strings.NewReader(ugly))

    var buf bytes.Buffer
    html.Render(&buf, doc)
    fmt.Println(buf.String())
    // renders normalized HTML — pair with indent logic for pretty output
}
CLI (Prettier)
# Format a single file in place
npx prettier --write index.html

# Format and print to stdout (pipe-friendly)
npx prettier --parser html index.html

# Format with 4-space indentation
npx prettier --tab-width 4 --parser html index.html

# Format HTML from stdin
echo '<div><p>Hello</p></div>' | npx prettier --parser html

Frequently Asked Questions

What is the difference between HTML formatting and HTML validation?
Formatting adds indentation and line breaks so the markup is readable. Validation checks whether the markup conforms to the HTML specification, reporting errors like missing required attributes or incorrectly nested elements. A formatter does not fix errors; a validator does not change whitespace.
Does formatting HTML change how the page renders?
In most cases, no. Browsers collapse sequences of whitespace into a single space when rendering. The exceptions are elements with white-space: pre or similar CSS rules, and the <pre> and <textarea> elements, where whitespace is significant. A well-built formatter preserves the content of these elements exactly as-is.
How many spaces should I use for HTML indentation?
Two spaces and four spaces are both common. Google's HTML/CSS Style Guide recommends 2 spaces. The Prettier formatter defaults to 2 spaces. Four spaces match Python's PEP 8 convention and are preferred by some teams for consistency across languages. Pick one and enforce it with a formatter or editor config.
Can I format HTML that contains embedded JavaScript or CSS?
This tool formats the HTML structure (tags and attributes). Inline <script> and <style> blocks are preserved as-is. For formatting embedded JavaScript and CSS, use a dedicated formatter like Prettier, which can parse and format all three languages in a single pass.
Is tabs vs. spaces still a debate for HTML?
Most HTML style guides and formatters default to spaces. The 2023 Stack Overflow Developer Survey shows spaces are used by about 55% of respondents for web development. Tabs have the accessibility advantage of letting each developer set their preferred visual width. Both work; consistency within a project matters more than the choice itself.
Why does my HTML look different after formatting?
The formatter adds whitespace characters (spaces and newlines) that were not in the original markup. This changes the source code's appearance but not the rendered output. If you see a visual difference in the browser, it is likely caused by inline elements where added whitespace creates an extra space character between tags, which can be resolved with CSS (font-size: 0 on the parent, or flexbox layout).
How do I enforce consistent HTML formatting across a team?
Use an automated formatter in your CI pipeline. Prettier supports HTML and can be run as a pre-commit hook via Husky or lint-staged. Add a .prettierrc config file to your repository specifying tabWidth, printWidth, and htmlWhitespaceSensitivity. Every commit will then follow the same formatting rules regardless of each developer's editor settings.
What is the difference between an HTML formatter and an HTML beautifier?
They are the same operation. 'Formatter' and 'beautifier' both refer to adding indentation and line breaks to HTML markup. Some tools use 'beautify' (js-beautify), others use 'format' (Prettier). The output is identical: properly indented, human-readable HTML. 'Pretty-print' is another synonym commonly used in documentation and search queries.