HTML Formatter with Pre and Script Preserved
Beautify messy or minified HTML while leaving pre, script, style and textarea content untouched
How this HTML beautifier formats your markup
Paste minified, generated or hand-written HTML and press Beautify HTML. The formatter reads the markup with a small tokenizer that understands tags, attributes (including quoted values that contain > characters), comments and the doctype, then rebuilds it with consistent indentation of 2 spaces, 4 spaces or tabs. It runs in your browser; the HTML is not uploaded.
It follows the way browsers lay out content rather than putting every tag on its own line. Block elements such as div, section, ul, li, table and p start a new line and indent their children. Inline elements such as a, span, strong, em, code and img stay inside the line of text they belong to, so "Read the <a href="/docs">docs</a> first" is not broken into five lines, and runs of spaces and line breaks in text are collapsed to a single space, which is how the browser renders them anyway. Void elements (br, hr, img, input, meta, link and the rest of the HTML list) have no closing tag and never increase the indent. Custom elements and framework components such as <my-widget> are treated as blocks.
When a block element ends up with a single short line of inline content, it is joined back into one line, for example <h1>Hello <em>world</em></h1>. The join width selector sets the limit at 80 or 120 columns, or turns joining off.
What is never touched: pre, script, style and textarea
Whitespace inside <pre> and <textarea> is part of the content, and re-indenting it would visibly change the page or the value of a form field. JavaScript inside <script> and CSS inside <style> are separate languages, and a naive formatter that treats them as text can break a template literal or a regular expression. This beautifier copies the content of all four elements exactly as written, character for character, and only indents the opening tag. Text such as if (a < b) inside a script is therefore safe, and so is JSON inside <script type="application/ld+json">.
Comments are kept verbatim on their own line, including Internet Explorer conditional comments that some email templates still rely on. The tool also copes with HTML that omits optional end tags: a new <li> closes the previous one, a new <td> or <tr> closes the open cell or row, and a block element such as <div> closes an open <p>, which mirrors simplified versions of the HTML parsing rules. An end tag with no matching start tag is kept but does not change the indentation.
Formatter vs minifier, and what this tool does not do
Beautifying is for reading, reviewing and diffing HTML: inspecting a CMS export, untangling an email template, or making server-rendered output readable before you debug it. For production you usually want the opposite, which is what the HTML Minifier linked below the tool does by stripping comments and whitespace between tags. This formatter does not validate HTML or rewrite your markup: it will not add missing closing tags, reorder attributes or fix invalid nesting, and it does not reformat the CSS or JavaScript it leaves untouched. Formatting is idempotent, so running it twice gives the same result. Because whitespace between inline elements is collapsed but never removed, the rendered page looks the same before and after.
How to use the HTML Formatter with Pre and Script Preserved tool
- 1
Paste your HTML
Drop a full document or a fragment into the left panel, or load the sample.
- 2
Pick indentation and join width
Choose 2 spaces, 4 spaces or tabs, and whether short elements are joined onto one line up to 80 or 120 columns.
- 3
Beautify
Press Beautify HTML; blocks are indented, inline tags stay on their text line, and pre, script, style and textarea are left as they were.
- 4
Copy or share
Copy the formatted output, or create a link that restores your input and settings for a teammate.
Frequently asked questions
Will beautifying change how my page renders?
It should not. Whitespace in text is collapsed to single spaces, which matches how browsers render it, and the content of pre, textarea, script and style is preserved exactly. The one edge case is CSS that depends on whitespace between inline-block elements, where adding or removing a line break between tags can shift spacing slightly.
Does it format the CSS and JavaScript inside the page?
No. The contents of style and script elements are kept exactly as written so the formatter can never break them. Use a dedicated CSS or JavaScript formatter for that code.
Can it fix broken or unclosed HTML?
It is a formatter, not a validator. It tolerates common omitted end tags such as li, td and p for indentation purposes, but it does not insert missing tags or correct invalid nesting.
Is my HTML sent to a server?
No. Tokenizing and formatting run in JavaScript in your browser. Share links store the input in the URL's # fragment, which browsers do not send to servers.
Related on TechLogHub
Related Formatter Tools
SQL Formatter with Uppercase Keywords
Format and beautify SQL queries with proper indentation
MongoDB Query Formatter
Format and beautify MongoDB queries
XML Formatter and Validator with Error Lines
Pretty-print, minify and validate XML, keeping comments and CDATA intact
SQL Practice Online with 36 Graded Exercises
Graded SQL exercises and a free-query playground on SQLite in your browser
Get the next tool before you need it
One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.

