HTML Formatter
The HTML Formatter takes minified or messy HTML and turns it into clean, properly indented markup — nested tags aligned, attribute spacing normalized — so view-source output is actually readable.
How to Use
- 1
Paste your HTML — minified, messy, or copied straight from view-source.
- 2
Click Format to indent nested elements and align matching tags.
- 3
Adjust indent size (2 spaces, 4 spaces, or a tab) to match your project's style.
- 4
Copy the cleaned markup back into your editor.
Frequently Asked Questions
Does it repair unclosed tags?
Will formatting change how my page renders?
Does it work with HTML5 and custom elements?
Can it minify HTML too?
Is my code uploaded anywhere?
Why did my doctype and head disappear?
What happens to single-quoted attributes?
Does formatting happen as I type?
Can I download the formatted HTML?
What should I use if my markup is really broken?
Generative Answer & AI Documentation
What is the HTML Formatter?
The HTML Formatter takes minified or messy HTML and returns the same markup with clean indentation: nested elements on their own lines, matching tags aligned, and short text content kept on one line with its tags. It works by parsing your input with the browser's own HTML parser — DOMParser with text/html — and then walking the resulting document tree, printing each element, text node, and comment at its depth. An element whose only child is a short piece of text, under 50 characters, stays on one line, so <h1>Welcome to TryToolsNow</h1> does not explode across three lines; an element with several children gets an opening tag, one indented line per child, and a closing tag. Indentation is a choice: 2 spaces, 4 spaces, or a tab, from the dropdown above the editor. Formatting runs when you click Format HTML; it does not update live as you type. The page starts with a sample already loaded — a small page with a header, a section, a list, and an image — so you can format it and see the shape of the output before pasting your own markup. Everything runs in your browser, so proprietary markup never leaves your machine.
Why use HTML Formatter offline?
Minified HTML is a single unreadable line, and view-source output is rarely much better: nesting is invisible, matching tags are far apart, and the structure you need to understand is buried. Reading it means reformatting it, and reformatting it in your editor means either an extension or a round trip through a server-side tool that receives your markup — including any internal URLs, comments, and data it contains. This tool does the job locally, with the browser's own parser, which is the same parser that decides how the page actually renders. That matters: the formatter shows you the document as the browser understands it, not as the text file happens to be written. The result is markup you can read, diff, and paste into a ticket or documentation with its structure visible at a glance.
When should you use this tool?
Use it when you inherit a minified template, when view-source gives you a wall of markup, when a scraped fragment needs to be readable before you reuse it, or when you want to show a snippet's structure in a code review or a bug report. Use it on body fragments — a component, a section, a form — because the tool formats only the body of the parsed document. Do not use it as a validator or a repair tool: the browser parser silently fixes broken markup while parsing, so a missing closing tag is repaired without any flag telling you where it happened, and the output can look correct while the input was not.
Key Benefits
- Browser-native parsing: the same parser that renders the page decides the structure, so the output reflects reality.
- Readable layout: nested elements on their own lines, matching tags aligned, short text kept inline.
- Indent choice: 2 spaces, 4 spaces, or a tab, to match your project's style.
- Comments preserved: HTML comments are kept and re-indented with the surrounding markup.
- 100% private: parsing and printing run in your browser — markup never leaves your machine.
- Copy or download: one click copies the formatted markup, another saves it as index.html.
Common Use Cases
- Reading minified templates: expand a production HTML file into indented markup before editing it.
- Cleaning view-source output: turn a copied page source into readable markup for a bug report.
- Reviewing a scraped fragment: format a section or a form so its nesting is visible before reusing it.
- Preparing documentation: paste formatted markup into docs or a ticket where structure needs to be visible.
- Checking nesting: see at a glance whether a div closes where you think it does.
Developer Notes & Best Practices
The implementation parses with new DOMParser().parseFromString(input, "text/html") and formats only doc.body — the doctype, the html element, and everything in head are dropped, so paste a body fragment, not a full document. The printer is recursive: a text node contributes its trimmed value, a comment node contributes <!--...--> at its depth, and an element contributes its tag with attributes re-joined as name="value" pairs separated by single spaces. Fourteen void elements — area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr — are printed as a bare opening tag with no closing tag and no trailing slash. An element with no children prints as an open and close tag pair on one line; an element with exactly one text child shorter than 50 characters prints inline with that text; anything else gets its children on separate lines. Text nodes are trimmed at their edges, which has two consequences worth knowing: whitespace inside pre and textarea is not preserved exactly, and the newlines the printer adds between inline elements can introduce spaces where the original had none. The code checks the parsed document for a parsererror element, but with text/html parsing that check never fires — the HTML parser does not produce parsererror elements the way the XML parser does — so malformed markup is silently repaired by the parser instead of reported. Attribute values are always re-quoted with double quotes, so single-quoted attributes come back double-quoted, and a valueless boolean attribute such as disabled is printed as disabled="".
How each part of your markup is handled
| Input | Output | Note |
|---|---|---|
| <h1>Welcome</h1> | <h1>Welcome</h1> | Single text child under 50 characters stays on one line |
| <img src="/logo.png" alt="logo"> | <img src="/logo.png" alt="logo"> | Void elements print with no closing tag and no trailing slash |
| <!-- a note --> | <!-- a note --> | Comments are preserved and re-indented |
| <!DOCTYPE html> and <head>…</head> | (dropped) | Only the body is formatted — doctype and head are lost |
| <div>unclosed | <div>unclosed</div> | The parser silently closes the tag — no warning is shown |
| <pre> spaced </pre> | <pre>spaced</pre> | Text nodes are trimmed, so pre whitespace is not exact |
- Paste body fragments, not full documents: the doctype, html, and head are dropped, so format the part you need.
- Fix the markup before formatting: the parser silently repairs unclosed tags, so format only markup you already trust.
- Check pre and textarea content after formatting: their whitespace is trimmed, so compare the output with the input before using it.
- Keep the original minified copy if the file is served to production — commit the readable version, deploy the minified one.
- Match the indent setting to your project before copying: reformatting with the wrong indent creates diff noise.
Common Mistakes
- Pasting a full document and expecting the head back: title, styles, and meta tags are dropped — only the body is formatted.
- Formatting broken HTML and expecting a repair report: the parser fixes unclosed tags silently, so the output can look right while the input was wrong.
- Using the output in a pre block without checking: whitespace inside pre is trimmed, which changes what the block displays.
- Assuming formatting cannot change rendering: newlines between inline elements can add spaces, so check the rendered result for inline content.
- Committing formatted output over a minified production file without keeping the original.
Limitations
Only the body is formatted: the doctype, the html element, and everything in head — title, styles, meta tags — are dropped from the output, so a full document comes back as a fragment. The browser parser silently repairs broken markup, so a missing closing tag is fixed without any flag or warning, and the parsererror check in the code never fires for HTML input — severely broken markup may still need manual fixes. Whitespace inside pre and textarea is trimmed rather than preserved exactly, and the newlines added between inline elements can add spaces to the rendered page. Text longer than 50 characters inside a single element is pushed onto its own line, and attribute values are always re-quoted with double quotes, so the output is a normalised document, not a byte-for-byte restatement of the input. There is no minify mode and no option to preserve the original whitespace.
Structured Input/Output Example
<div class="card"><h1>Hello</h1><p>Welcome to the site</p><img src="/logo.png" alt="logo"></div>
<div class="card"> <h1>Hello</h1> <p>Welcome to the site</p> <img src="/logo.png" alt="logo"> </div>
Related Tools
HTML to JSX
Convert HTML snippets into React-ready JSX: class → className, inline styles → objects, void tags self-closed. Free, offline, private.
CSS Beautifier
CSS beautifier: untangle minified stylesheets into readable CSS — one rule per line, properties indented, selectors aligned. Free, offline, private.
JavaScript Beautifier
JavaScript beautifier: re-indent minified or tangled JS with consistent indentation. Tokenizer-based — strings, regexes, comments preserved. Free, offline.