Skip to content
CornerTools

HTML Formatter

Format messy HTML with clean indentation, its CSS and JavaScript included, or minify it, and preview the page it makes.

1
Format
Indent
Over
attributes
1
Characters: 0

Paste HTML or open an .html file to get it back indented, one block element per line, with the CSS in <style> and the JavaScript in <script> formatted too. Minified does the opposite: it collapses the whitespace to make the file smaller. Preview shows the page itself, in a sandbox, one click away.

Formatting never reports errors. Unlike JSON or XML, HTML has no invalid input: browsers recover from unclosed tags and stray characters in a defined way, so every input formats. Your HTML is formatted on your device and never uploaded.

Options

  • Format — Pretty (the default) or Minified.
  • Indent — 2 spaces (the default), 4 spaces or a tab, for Pretty.
  • Format CSS and JS — on by default. Off, the code inside <style> and <script> keeps its own lines and is only indented.
  • Attributes on separate lines — puts each attribute on its own line when a tag has more than the number you set (3 by default).
  • Remove comments — for Minified. Conditional comments (<!--[if mso]>), which Outlook reads, are kept.
  • Output — Code (where this page opens) or Preview, with its width, Run scripts and External resources switches. Copy and Download always take the formatted code; in Preview, the Copy button reads Copy formatted HTML.

A <meta> naming an encoding other than UTF-8 is rewritten to UTF-8, the encoding the output is saved in, and a note under the output says so. Your view and options are remembered on this device.

What formatting changes

Pretty puts each block element, such as <div>, <p> or <li>, on its own line, indented by its depth, and keeps at most one blank line between blocks. Text and inline elements such as <b> and <a> stay on their line with their spacing, because a space added or removed there shows on the page. The text inside <pre> and <textarea> is kept exactly as written.

Minified turns every run of spaces, tabs and line breaks into a single space, never none, so words either side of an inline element stay apart. <pre>, <textarea>, <style> and <script> are kept verbatim, and attribute values are never changed.

Examples

A block with inline text, a pre and a style

Input
<div class="note"><h2>Opening hours</h2><p>Open <b>daily</b> from 9 to 5.</p><pre>Mon  9-5
Sat 10-2</pre><style>.note{padding:8px;color:#333}</style></div>
Pretty, default options
<div class="note">
  <h2>Opening hours</h2>
  <p>Open <b>daily</b> from 9 to 5.</p>
  <pre>Mon  9-5
Sat 10-2</pre>
  <style>
    .note {
      padding: 8px;
      color: #333
    }
  </style>
</div>

The heading, paragraph, <pre> and <style> each go on their own line. The <b> stays inside its sentence, the two spaces after “Mon” in the <pre> survive, and the CSS is laid out one rule per line.

Frequently asked questions

Almost never. Line breaks are added only between block elements, where browsers ignore whitespace. A gap can appear where a stylesheet makes those elements inline-block or shows whitespace with white-space: pre, since the formatter does not read CSS. Preview shows the input as written, so compare it with the formatted file opened in a browser if layout matters.
Yes, with Format CSS and JS on: the code in <style> and <script> elements. Code in style and onclick attributes is left as written, and so are linked .css and .js files, which the formatter never fetches. Minified does not minify CSS or JavaScript.
Either renders the same. Two spaces is the most common style for HTML and the default here; a tab lets each reader's editor choose the width. Pick the one your project already uses.

See also