HTML Formatter

Format and beautify your HTML code

Paste your HTML here...
Loading editor...
Loading editor...

HTML Formatter & Beautifier — Pretty Print HTML Code Online

Understanding the Basics

HTML (HyperText Markup Language) is the standard markup language for web pages, defining their structure and content using nested elements represented by tags. Modern HTML documents, particularly those generated by server-side templating engines, JavaScript frameworks, or CMS systems, are frequently delivered as unformatted single-line strings that are impossible to manually review. HTML from email templates, WYSIWYG editors, and web scraping is often inconsistently indented or heavily nested. Formatting HTML improves readability dramatically — a single-line 10 KB HTML document expands to several hundred readable lines with proper indentation showing the hierarchy of elements. This is essential for debugging template rendering issues, auditing third-party widget HTML for security concerns, reviewing CMS-generated output, and understanding the DOM structure of scraped pages. Pretty-printing HTML files also reduces code diff noise in team Git flows. HTML formatting is more complex than JSON or CSS formatting because HTML has semantic indentation considerations: inline elements (span, a, em, strong) should not be broken across lines unnecessarily, void elements (br, hr, img, input) must not have closing tags, and pre-formatted content (within pre and textarea tags) must preserve its whitespace exactly. Our formatter uses Prettier's HTML parser with configurable options for these scenarios.

How to Use the Tool

  1. Paste your unformatted or minified HTML into the input editor.
  2. The formatter parses the HTML and applies consistent indentation to all nested elements.
  3. Inline elements (span, a, em) are kept on the same line as their parent text where appropriate.
  4. Block elements (div, section, article, header) are each placed on their own line with proper indentation.
  5. Copy or download the formatted HTML for review, documentation, or debugging.

Key Features & Specifications

  • Formats HTML using Prettier's HTML parser with proper nested indentation
  • Preserves content within pre, textarea, and script tags exactly as-is
  • Configurable indentation width (2 or 4 spaces) and print width (80 or 120 characters)
  • Handles HTML5 void elements (br, hr, img, input) correctly without closing tags
  • Normalizes attribute formatting: wraps long attribute lists across multiple lines

Frequently Asked Questions (FAQ)

Q:Will the formatter fix invalid HTML (like unclosed tags)?
Prettier's HTML parser attempts to recover from minor HTML errors. However, severely malformed HTML may cause parse errors. For fixing broken HTML, use an HTML validator or the browser's DOMParser which applies the HTML5 error recovery algorithm.
Q:My script tag content is being reformatted — how do I prevent this?
Prettier formats embedded JavaScript inside script tags. If you don't want the JS reformatted (e.g., it contains intentional unusual formatting), you can add a Prettier ignore comment: <!-- prettier-ignore --> before the script block.
Q:Can this format HTML email templates correctly?
Yes. HTML email templates (which often use nested table layouts and inline styles) are formatted correctly. Note that email templates frequently require precise whitespace control in specific areas — review the formatted output carefully before using in email campaigns.