HTML Formatter & Beautifier
Paste HTML and it is beautified on the spot: each block element goes on its own line, nested content is indented by 2 spaces, 4 spaces or a tab, and the text itself is not changed. The example is 469 bytes of compact HTML; formatted with 2 spaces it is 603 bytes, and its <pre> and <script> content comes out exactly as typed. Void elements, comments, the doctype and quoted attributes are handled, and <script>, <style>, <pre> and <textarea> are copied verbatim. Choose Minify to shrink it instead, then copy or download the result. The HTML is only read as text: it is never run or rendered, and it never leaves your browser.
Updated Runs in your browser: nothing you enter is uploadedHow this tool works
Replace the example: paste or type HTML. It is formatted as you type.
Your HTML is processed on this device and never uploaded. It is not saved either: reload the page and it is gone.
Result
Formatted: 18 elements
- 3 script, style, pre and textarea elements were copied exactly as written.
Size and structure
- Before (UTF-8)
- 469 bytes
- After (UTF-8)
- 603 bytes
- Change
- +134 bytes (+28.6%)
- Depth
- 5
- Elements
- 18
- Attributes
- 8
- Comments
- 1
What each layout produces
| Layout | Result | Effect |
|---|---|---|
| 2 spaces | <div> ⏎ <h2>Title</h2> ⏎ <p>Some <b>bold</b> text</p> ⏎ </div> | The default: one nesting level per two spaces. |
| 4 spaces | <div> ⏎ <h2>Title</h2> ⏎ <p>Some <b>bold</b> text</p> ⏎ </div> | One nesting level per four spaces. |
| Tab | <div> ⏎ →<h2>Title</h2> ⏎ →<p>Some <b>bold</b> text</p> ⏎ </div> | One tab per nesting level (shown here as →). |
| Minify | <div><h2>Title</h2><p>Some <b>bold</b> text</p></div> | Whitespace between blocks removed and comments dropped: the smallest text that renders the same. |
What is kept and what changes
| Case | You type | The tool outputs | Why |
|---|---|---|---|
| Script | <script>if (a < b) { ⏎ go(); ⏎ }</script> | <script>if (a < b) { ⏎ go(); ⏎ }</script> | The content is never read as HTML, so its spacing, “<” and “>” stay as typed. |
| Style | <style>a > b { ⏎ color: red; ⏎ }</style> | <style>a > b { ⏎ color: red; ⏎ }</style> | Copied exactly; use the CSS minifier to shrink it. |
| Preformatted text | <div><pre> a ⏎ b</pre></div> | <div> ⏎ <pre> a ⏎ b</pre> ⏎ </div> | Spaces and line breaks inside <pre> are part of what the page shows. |
| Textarea | <p>Say <textarea> hi ⏎ there</textarea></p> | <p>Say <textarea> hi ⏎ there</textarea></p> | Its content is the field’s default value. |
| Comment | <div><!-- note --><p>x</p></div> | <div> ⏎ <!-- note --> ⏎ <p>x</p> ⏎ </div> | Kept, on its own line. Minify drops it. |
| Text spacing | <p>one two ⏎ three four</p> | <p>one two three four</p> | Runs of ASCII whitespace become one space (as a browser shows them); and U+00A0 stay. |
| Attributes | <a href = "x?a=1&b=2" title='say "hi"' hidden>t</a> | <a href="x?a=1&b=2" title='say "hi"' hidden>t</a> | Names, values and quote marks as typed; only the blanks between them change. |
| Void elements | <div><br><img src=a.png><hr></div> | <div> ⏎ <br> ⏎ <img src=a.png> ⏎ <hr> ⏎ </div> | No end tag is added and no extra level is opened. |
| End tag left out | <ul><li>One<li>Two</ul> | <ul> ⏎ <li>One ⏎ <li>Two ⏎ </ul> | Browsers close the first <li> themselves, so nothing is added. |
How elements are laid out
| Kind | Elements | Treatment |
|---|---|---|
| Inline | <a> <abbr> <acronym> <audio> <b> <bdi> <bdo> <big> <br> <button> <canvas> <cite> <code> <data> <del> <dfn> <em> <embed> <font> <i> <iframe> <img> <input> <ins> <kbd> <label> <map> <mark> <meter> <noscript> <object> <output> <picture> <progress> <q> <rp> <rt> <ruby> <s> <samp> <slot> <small> <span> <strike> <strong> <sub> <sup> <textarea> <time> <tt> <u> <var> <video> <wbr> | Stay inside the line of text they are in, so no space is added or removed around them. |
| Void | <area> <base> <br> <col> <embed> <hr> <img> <input> <link> <meta> <param> <source> <track> <wbr> | Have no end tag and no content; no level is opened. |
| Kept verbatim | <pre> <script> <style> <textarea> | Everything up to the matching end tag is copied exactly. |
| Everything else | div, p, ul, li, table, section, custom elements… | Block-level: each starts a line. A block holding only inline content stays on one line when it fits in 100 characters. |
What it reports and what it accepts
| Input | Message | Position | Fix |
|---|---|---|---|
| <p>Hello | accepted | none | Not an error: the paragraph is simply left open, as a browser would treat it. |
| <div class="a>x</div> | The double quote that starts this attribute value is never closed. | line 1, column 12 | Close the attribute value with a matching quote. |
| <a title='x>link</a> | The single quote that starts this attribute value is never closed. | line 1, column 10 | Close the attribute value with a matching quote. |
| <p>x</p> ⏎ <div class="a" | The tag <div is never closed with >. | line 2, column 1 | Finish the tag with >. |
| <p>x</p><!-- note | This comment is never closed: it needs --> at the end. | line 1, column 9 | End the comment with -->. |
| <!DOCTYPE html | This declaration is never closed with >. | line 1, column 1 | End the declaration with >. |
| </div | The end tag </div is never closed with >. | line 1, column 1 | Finish the end tag with >. |
How to format HTML online
- Paste your HTML. Replace the example with your own markup. It is formatted as you type; for very large input, press Format instead.
- Choose the layout. Pick 2 spaces, 4 spaces, a tab or Minify. The result updates at once.
- Check the notes. The page lists anything worth knowing: raw-text elements copied exactly, end tags that were left out, or end tags that match nothing.
- Fix an unclosed quote, tag or comment. If the text ends inside one, you get the message, the line and column, and the line with a caret. “Show in input” jumps to it.
- Copy or download. Copy the result to the clipboard or download it as a .html file. Nothing is uploaded at any step.
Frequently asked questions
- How do I beautify HTML?
- Paste it into the box and pick a layout. Each block-level element (div, p, ul, section and so on) is put on its own line and its content is indented one level deeper. Short elements that only hold text and inline tags, such as <p>Hello <b>world</b></p>, stay on one line.
- Will formatting change how my page looks?
- It is built not to. Inline elements such as <a>, <span> and <b> stay inside their line of text, so the spaces around them are not changed, and runs of blank characters are reduced to one space, which is how a browser shows them anyway. The content of <pre>, <textarea>, <script> and <style> is copied exactly. The one thing that can differ is the whitespace around inline-block elements or in CSS that treats whitespace specially (for example white-space: pre on a div), so compare the page if that applies to you.
- What happens to script, style, pre and textarea?
- Their content is found by scanning for the matching end tag and is copied character for character, including indentation, “<” and “>” and anything that looks like a tag. A script that contains the text "</p>" is therefore safe. If the end tag is missing, the content runs to the end of the text, as a browser would read it.
- Does it add or remove closing tags?
- No. Void elements such as <br>, <img> and <meta> are left without an end tag, and an element whose end tag was left out, such as <li> or <p>, is indented as if it ended where the next sibling starts, but no tag is added. An end tag that matches nothing open is kept on its own line and counted in the notes.
- What does Minify do?
- It removes the whitespace between block-level tags, reduces other runs of whitespace to one space, trims the edges of block elements and drops comments. Conditional comments such as <!--[if IE]> are kept, and the content of script, style, pre and textarea is untouched. It is a whitespace-only minifier: it does not shorten attribute values, remove optional tags or compress scripts and styles.
- Why does it say a quote, tag or comment is never closed?
- Browsers read to the end of the text in that case, which usually swallows the rest of the page, and a formatter cannot guess where you meant to stop. The message gives the line and column where the unclosed construct starts. Everything else that is sloppy but readable, such as unquoted attributes, missing end tags and stray end tags, is accepted.
- Does it run my HTML or load images and scripts?
- No. The text is only read character by character and written back into a text box. It is never parsed into a page, inserted into this document or executed, so scripts, images, styles and links in it do nothing.
- How big a file can I paste?
- Up to 2,000,000 characters; longer text is refused before anything is read. Up to 300,000 characters the result updates a moment after you stop typing; above that the page waits for you to press Format so typing stays smooth. Elements nested deeper than 256 levels are refused.
- Does it handle attributes with quotes and special characters?
- Yes. Values in double or single quotes may contain “>”, line breaks and the other kind of quote, and unquoted values are accepted. Attribute names, values and quote marks are kept as typed; only the blanks between attributes become single spaces.
- Is my HTML uploaded or saved?
- No. It is formatted inside your browser tab and is never sent, saved or kept in local storage, so it disappears when you reload. With analytics allowed, the site logs only which button was pressed, never the text.
How it works
The text is read once, front to back, by a small tokenizer written for this tool. It splits the text into start tags (with their attributes), end tags, comments, the doctype and other declarations, and text, and it jumps from the start tag of script, style, pre or textarea straight to the matching end tag so their content is never interpreted. Each step only moves forward and uses plain string searches, with no regular expressions, so the time grows in step with the length of the text however it is built. It does not use the browser’s DOMParser and does not build a page, so nothing in the input can run, load or render, and the example can be shown without scripts. The tokens are written out again. Block-level elements start a line, indented one level per open block element. Inline elements and text stay together on a line, and a block element that holds only such content stays on one line when it fits. Runs of ASCII whitespace (space, tab, line feed, carriage return, form feed) in text become one space; non-breaking spaces are content and stay. Void elements and self-closing tags open no level, and a few end tags HTML lets you leave out (li, dt, dd, tr, td, th, option, p and similar) are treated as ended by the next sibling, without adding a tag. Minify writes the same tokens without line breaks or indentation and without comments, keeping one space only where two pieces of text or inline elements meet. Depth is the most elements open at once; sizes are UTF-8 byte counts of the text before and after. The input is not validated against the HTML specification: only an unclosed quote, tag or comment is an error.
Sources
More free tools
- XML Formatter & ValidatorCheck XML is well-formed with the exact line and column of any error, then pretty-print with 2 spaces, 4 spaces or a tab, or minify. Runs in your browser.
- CSS Minifier & BeautifierMinify CSS safely or beautify it with 2 or 4 space indents. Strings, url() and calc() stay intact. Shows bytes saved and gzip size. Runs in your browser.
- HTML Entity Encoder & DecoderEscape text for safe HTML or decode named, decimal and hex character references the way browsers do, with all 2,231 standard names. Runs in your browser.
- HTML to Text ConverterPaste HTML and get plain text: tags, scripts and comments removed, entities decoded, lists, links and tables kept readable. Runs in your browser.
- Markdown to HTML ConverterConvert Markdown to HTML with a live, safe preview. CommonMark plus tables, strikethrough and task lists. Raw HTML is escaped. Runs in your browser.
- SQL FormatterFormat and beautify SQL: one clause per line, one column per line, indented subqueries and CASE, keywords in your case. Or minify. Runs in your browser.
More: Formatters and data convertersAll developer toolsAll toolsHome & DIY calculators