๐งน HTML Beautifier
By Shihab Mia ยท Updated 2026-07-29
This HTML beautifier takes minified or tangled markup and rewrites it with clean, consistent indentation so every tag sits on its own line at the right depth. Paste your code, pick 2 spaces, 4 spaces, or a tab for the indent, and the formatted output appears instantly with a one-click copy button. Everything runs in your browser, so your markup is never uploaded to a server.
What is the HTML Beautifier?
An HTML beautifier reverses minification. Minified HTML strips out the line breaks and indentation that make a document readable, packing everything onto one long line so it downloads faster. That is great for production but terrible for a human trying to read, debug, or learn from the code. The beautifier reads through your markup and rebuilds the visual structure, putting each tag on its own line and indenting child elements one level deeper than their parent. The result is HTML that is byte-for-byte equivalent in meaning but far easier to scan.
The logic behind this html beautifier is a tokenizer plus an indent walker. First the input is split into tokens by scanning for the < and > characters, so every opening tag, closing tag, comment, doctype, and run of text becomes a separate token. Then the tool walks the token list keeping a single depth counter. Before printing a closing tag like </div> it decreases the depth by one, so the close lines up with its matching open. After printing a normal opening tag it increases the depth by one, so the children that follow are indented further. This simple rule is what produces the familiar staircase shape of well-formatted HTML.
Getting the edge cases right is what separates a good html formatter from a broken one. Void elements such as br, hr, img, input, link, and meta have no closing tag, so the beautifier never increases depth after them, otherwise everything below an image would drift one level too far to the right. The same applies to self-closing tags written with a trailing slash, to the doctype declaration, and to HTML comments, none of which change nesting depth. This tool keeps comments and the doctype intact on their own lines without letting them throw off the indentation of the surrounding markup.
This tool intentionally keeps its formatting model simple. It indents by tag nesting depth rather than wrapping individual attributes onto separate lines. Some beautifier engines, including the popular js-beautify project that powers many other online tools, offer a wrap-attributes setting so a tag with ten attributes can be broken one-per-line once a length limit is hit. This html beautifier does not attempt that, which keeps the algorithm predictable and fast even on very long documents, but it does mean a single tag with many attributes stays on one line after beautifying. If your style guide requires attribute-per-line formatting, treat this tool as the structural first pass and finish attribute wrapping by hand or with your editor.
Choosing between spaces and tabs is a matter of team convention rather than correctness. Two spaces is common in front-end style guides such as Airbnb and Google, four spaces shows up in many CMS themes and older codebases, and a tab lets each developer set their own preferred display width in their own editor. This beautifier supports all three so you can match whatever convention your project already uses instead of forcing a new one on the codebase. Keep in mind that a beautifier and a validator do different jobs: this tool reformats whitespace, while a service like the W3C Markup Validator checks whether your HTML is actually well-formed and standards-compliant. Run both when you want markup that is both readable and correct.
A reliable html beautifier is something front-end developers reach for constantly. You paste in markup copied from a page source, from a CMS export, from an email template, or from a build artifact, and you immediately get readable code you can study or edit. Because this prettify html tool is purely client-side with no libraries, it works offline, handles private or proprietary markup safely, and formats even large documents instantly. It is the fastest way to turn a wall of compressed tags back into clean, indented HTML you can actually work with.
When to use it
- Reading the minified page source of a live website by reformatting it into readable, indented HTML.
- Cleaning up markup exported from a CMS, email builder, or page builder before pasting it into your own project.
- Debugging a layout by seeing the true nesting structure once every tag sits on its own indented line.
- Teaching or learning HTML by turning a compressed snippet into a clearly nested example.
- Standardising indentation across a team so everyone commits HTML with the same 2-space or 4-space style.
- Tidying up generated or copy-pasted markup that arrived as one long unreadable line.
How to use the HTML Beautifier
- Paste your minified or messy HTML into the input box, or edit the sample shown.
- Choose your indent size: 2 spaces, 4 spaces, or a tab.
- The beautified HTML appears automatically as you type, or press Beautify HTML to run it.
- Check the formatted output in the lower panel.
- Press Copy to put the clean, indented HTML on your clipboard.
Formula & method
Worked examples
A small minified snippet with a nested paragraph and a void image.
- Input: <div><p>Hi <img src="a.png"></p></div>
- Tokenize into: <div>, <p>, "Hi ", <img src="a.png">, </p>, </div>
- Print <div> at depth 0, then increase depth to 1
- Print <p> at depth 1, then increase depth to 2; print "Hi " and <img...> at depth 2 (img is void, so depth stays 2)
- Before </p> decrease depth to 1 and print it; before </div> decrease depth to 0 and print it
Result: Six clean lines: <div> and </div> at column 0, <p> and </p> at one indent, and the text plus image at two indents.
A list flattened onto one line, formatted with 2-space indentation.
- Input: <ul><li>One</li><li>Two</li></ul>
- Print <ul> at depth 0, then increase depth to 1
- Print <li>, then increase to depth 2 for the text One, then decrease to depth 1 for </li>
- Repeat for the second <li>Two</li> at the same depth
- Before </ul> decrease depth back to 0 and print it
Result: The list renders with <ul> and </ul> at the margin and each <li> indented 2 spaces, making the structure obvious.
A snippet mixing an HTML comment and a self-closing tag with normal nesting.
- Input: <div><!-- start --><svg width="10" height="10" /><span>Icon</span></div>
- Tokenize into: <div>, <!-- start -->, <svg width="10" height="10" />, <span>, "Icon", </span>, </div>
- Print <div> at depth 0, then increase depth to 1
- Print the comment at depth 1 without changing depth, then print the self-closing svg tag at depth 1 without changing depth
- Print <span> at depth 1, increase to depth 2 for "Icon", decrease to depth 1 before </span>, then decrease to depth 0 before </div>
Result: Five lines nested at most one level deep. The comment and the self-closing svg sit inline with their siblings instead of pushing the icon further right.
How each token type affects indentation depth
| Token type | Example | Effect on depth |
|---|---|---|
| Opening tag | <div> | Increase depth by 1 after printing |
| Closing tag | </div> | Decrease depth by 1 before printing |
| Void element | <br>, <img>, <input> | No change to depth |
| Self-closing tag | <svg ... /> | No change to depth |
| Doctype | <!doctype html> | No change to depth |
| Comment | <!-- note --> | No change to depth |
| Text node | Hello world | No change to depth |
Void (self-closing by default) HTML elements that never increase indent
| Element | Purpose |
|---|---|
| area | Clickable region inside an image map |
| base | Base URL for relative links |
| br | Line break |
| col | Column inside a table colgroup |
| embed | External content or plugin |
| hr | Thematic break / horizontal rule |
| img | Image |
| input | Form input control |
| link | Stylesheet or resource link |
| meta | Document metadata |
| source | Media source for picture, audio, or video |
| track | Text track for media |
| wbr | Optional word break point |
Choosing an indent style for your project
| Indent style | Common in | Effect on file size | Best when |
|---|---|---|---|
| 2 spaces | Airbnb and Google style guides, most React and Vue projects | Smallest of the spaced options | Deeply nested markup where extra width matters |
| 4 spaces | WordPress themes, many legacy CMS templates | Twice the bytes of 2-space indenting | A codebase that already standardised on 4 spaces |
| Tab | Linux kernel style, Go tooling, personal editor setups | Smallest possible, one byte per level | Teams who want each developer to control display width locally |
Common mistakes to avoid
- Treating void elements like normal tags. A common bug is increasing indent depth after <br>, <img>, <input>, or <meta>. Because those elements have no closing tag, doing so pushes everything below them one level too far right. A correct html beautifier skips depth changes for all void elements.
- Expecting beautifying to validate your HTML. An html formatter only re-indents what you give it. It does not check for unclosed tags, misspelled attributes, or invalid nesting. If a </div> is missing, the indentation will look off, which is actually a useful hint, but the tool will not insert the missing tag for you.
- Beautifying content where whitespace is significant. Inside <pre>, <textarea>, and <script> blocks, line breaks and spacing can matter. Reformatting them can change how text renders or behaves. Review those regions after beautifying rather than trusting the indentation blindly.
- Shipping beautified HTML to production. Beautified HTML is larger because of all the added whitespace. Use it for reading and editing, then minify before deploying so visitors download the smallest possible file.
- Assuming attributes wrap automatically on long lines. This beautifier indents based on tag nesting depth, it does not break a single tag with many attributes onto separate lines even if the line is very long. A <div> with ten attributes stays on one line after beautifying. If your style guide requires one attribute per line, treat this tool as the structural first pass and wrap long attribute lists by hand.
- Picking an indent style that clashes with the team convention. Switching between 2 spaces, 4 spaces, and tabs on a shared file creates noisy diffs where every line shows as changed even though nothing meaningful moved. Confirm the project convention, often visible in an .editorconfig file, before beautifying and committing.
Glossary
- Beautify
- To reformat code by adding line breaks and indentation so it is easy for a human to read, the opposite of minify.
- Minify
- To strip line breaks, indentation, and comments from code to make the file smaller for faster downloads.
- Token
- A single unit the parser splits the input into, such as one tag, one comment, or one run of text.
- Void element
- An HTML element that has no closing tag and no children, such as br, img, input, link, and meta.
- Indent depth
- How many levels deep a tag is nested. Each level adds one more unit of indentation (spaces or a tab).
- Self-closing tag
- A tag written with a trailing slash, like <svg />, that opens and closes in one token and never nests children.
- Prettify
- Another common name for beautifying code, used interchangeably with beautify to mean reformatting for readability.
- Wrap attributes
- A formatting option, not implemented in this tool, that breaks a tag with many attributes onto multiple lines once it exceeds a length limit.
Frequently asked questions
What is an HTML beautifier?
An HTML beautifier is a tool that reformats minified or messy markup into clean, readable code by placing each tag on its own line and indenting nested elements. It makes the structure of a document obvious without changing what the HTML means or how the browser renders it.
How do I beautify HTML online for free?
Paste your HTML into the input box on this page, choose an indent size of 2 spaces, 4 spaces, or a tab, and the formatted result appears instantly. Press Copy to grab the clean code. It is completely free and runs in your browser, so no sign-up or upload is needed.
Does beautifying HTML change how my page works?
No. Beautifying only adds line breaks and indentation between tags. It does not rename anything, remove tags, or alter attributes, so the browser parses the formatted HTML exactly as it did the original. The page looks and behaves the same.
Why are void elements like br and img not indented further?
Void elements such as br, hr, img, input, link, and meta have no closing tag and cannot contain children. Because nothing nests inside them, the beautifier does not increase the indent depth after them. Indenting past a void element would incorrectly push the following lines too far right.
Is my HTML uploaded to a server?
No. This html beautifier runs entirely in your browser with plain JavaScript and no external libraries. Your markup never leaves your device, which makes it safe to use with private, internal, or proprietary code.
What is the difference between beautifying and minifying HTML?
Beautifying adds whitespace to make code readable for humans, while minifying removes whitespace to make the file smaller for faster downloads. Use a beautifier when reading or editing code, and a minifier when preparing HTML for production.
What is the difference between an HTML beautifier and Prettier?
Prettier is an opinionated code formatter that reformats HTML, CSS, and JavaScript with almost no configuration, while an html beautifier like this one focuses on one job, reindenting HTML tags by nesting depth, and lets you pick the indent size yourself. Both aim for readable code; Prettier applies more rules automatically, this tool keeps the logic simple and transparent.
Should I use 2 spaces, 4 spaces, or tabs to indent HTML?
2 spaces is the most common choice in modern front-end style guides such as Airbnb and Google and keeps deeply nested markup from running too wide. 4 spaces is common in WordPress themes and older codebases. Tabs let each developer choose their own display width. Match whatever your project already uses rather than switching mid-file.
Can this tool beautify HTML that has inline CSS or JavaScript inside style and script tags?
Yes, the style and script tags themselves are indented by nesting depth like any other element, but the code inside them is left exactly as you typed it rather than being reformatted as CSS or JavaScript. Use a dedicated CSS or JavaScript beautifier if you need the code inside those blocks reformatted too.
Does beautifying HTML fix unclosed or mismatched tags?
No. A beautifier only adds indentation and line breaks, it does not parse your HTML for correctness or repair missing and mismatched tags. If the indentation looks wrong after beautifying, for example if everything below a certain point drifts to one side, that is often a sign of an unclosed tag you should fix by hand.