HTML Formatter and Beautifier
Paste messy or minified markup and this HTML formatter indents it cleanly, wraps long lines and attributes, and also formats the CSS and JavaScript inside style and script tags.
Press Ctrl+Enter (⌘+Enter on Mac) to run now.
Format HTML Online
Minified templates, markup copied from browser dev tools and HTML generated by CMS editors often arrive as one long line or with random indentation. This HTML formatter rebuilds the structure so every nested element sits on its own indented line, which makes it easy to spot a missing closing tag, review a diff or edit a template by hand.
It is built on js-beautify, the open-source formatter that also powers the built-in HTML formatting in Visual Studio Code. Inline <style> and <script> blocks are formatted as CSS and JavaScript, while <pre> and <textarea> content is left exactly as it was. Formatting runs in your browser.
How to format HTML
- Paste your HTML into the left box, open an
.htmlfile, or try the sample. Full documents and fragments both work. - Pick the indentation: 2 spaces, 4 spaces or tabs, matching your project’s style.
- Set line and attribute wrapping. Long text wraps at the chosen width; elements with many attributes can wrap only when too long or put every attribute on its own line.
- Choose what else to format: embedded CSS and JavaScript, indentation of
<head>and<body>, and how many blank lines to keep. - Copy the result or download it as
formatted.html. The output updates as you type or change an option.
What the HTML formatter changes (and what it leaves alone)
The formatter reads the markup as a stream of tags and text, then rewrites only the whitespace between them:
- Block elements such as
<div>,<section>,<ul>and<li>start on a new line and are indented one level deeper than their parent. - Inline elements such as
<a>,<strong>and<span>stay inside the text line, because adding a line break there would add a visible space on the page. - Whitespace-sensitive content in
<pre>and<textarea>is copied character for character. - Embedded code in
<style>and<script>is passed to the CSS and JavaScript beautifiers, including JSON-LD blocks. Untick “Format embedded CSS and JS” to keep those blocks unchanged. - Template tags such as
<?php … ?>,{{ name }}and{% if %}are kept intact, so Blade, Twig, Handlebars and PHP templates can be formatted too.
Tag names, attribute values and text are never rewritten, so the formatted page renders the same as the original. The formatter does not validate or repair markup: if a closing tag is missing, the indentation will drift from that point, which is often the quickest way to find the mistake.
HTML formatter options
| Option | What it does | Typical choice |
|---|---|---|
| Indent | Characters used per nesting level | 2 spaces for HTML, or tabs if your editor uses them |
| Wrap lines at | Breaks text and inline content that runs past the width; “No wrapping” keeps each line whole | 120 characters, or off for content that must stay on one line |
| Attributes | “Wrap when too long” keeps short tags on one line; “One per line” suits elements with many classes, ARIA or data attributes | Wrap when too long |
| Blank lines | How many existing empty lines in a row are kept between elements | Keep up to 1 |
| Format embedded CSS and JS | Beautifies <style> and <script> content | On |
| Indent head and body | Indents <head> and <body> one level inside <html> | Off (saves a level of indentation) |
| Blank line before head and body | Adds an empty line before <head>, <body> and </html> | Off |
| End with a newline | Adds a final line break, which Git and most linters expect | On |
Tips for readable HTML
- Find unclosed tags: format the file and look for the place where the indentation stops returning to the left margin; the unclosed element is usually just above it.
- Prefer “One per line, bracket below” for components with long Tailwind class lists or many props; diffs then show exactly which attribute changed.
- Formatting is for editing, not for production. Before deploying, shrink the markup again with the HTML Minifier, which removes the whitespace this tool adds.
- Need the same for other code? Use the CSS Beautifier for stylesheets, the JavaScript Beautifier for scripts or the XML Formatter for XML and SVG files.
Frequently Asked Questions
Common questions about formatting HTML.
Paste the HTML into the left box. The formatted version appears on the right straight away; adjust the indentation and wrapping, then copy it or download it as an .html file.
No in practice. Only whitespace between tags is changed, inline elements stay on their text line and <pre> and <textarea> content is untouched. CSS that depends on whitespace between inline-block elements is the rare exception to check.
Yes. Content of <style> and <script> tags, including JSON-LD, is formatted with the same indentation. Turn the option off to keep it as written.
No. It re-indents the markup but does not add missing closing tags or change the structure. Broken nesting shows up as indentation that does not return to the expected level.
Usually yes. Template tags such as <?php ?>, {{ }} and {% %} are preserved. Very unusual syntax can still confuse the parser, so check the result.
A formatter adds line breaks and indentation so people can read the code. A minifier removes them so browsers download fewer bytes. Format while you work, minify when you publish.
No. The formatting library is downloaded once and all formatting runs in your browser, so the markup stays on your device.
Lines longer than the “Wrap lines at” width are broken at spaces. Choose “No wrapping” to keep each paragraph on one line.