Skip to content
ToolkitHub

HTML Formatter

Format and beautify HTML

Guide

Why formatting HTML markup matters

By Logappradeep · ServiceNow developer

HTML that comes out of a CMS export, an email builder, a template engine or a minified production page is often a single unbroken line. Browsers do not care, but people do: when every tag sits on one line, it is almost impossible to see which <div> closes where, spot a missing </li> or review a change in a pull request. An HTML formatter re-indents the document so that the nesting of elements is visible at a glance.

How the formatter works

The tool splits your markup into tags and text, then walks through them in order. Each opening tag increases the indentation level by the amount you choose (2, 4 or 8 spaces), and each closing tag decreases it. Void elements such as <img>, <br>, <hr>, <input>, <meta> and <link> never have a closing tag, so they do not open a new level. Comments and the <!DOCTYPE> declaration are left flat. For example, this input:

<ul><li>One</li><li>Two</li></ul>

becomes a list where each <li> sits one level deeper than its parent <ul>. The Minify button does the opposite: it removes whitespace between tags and collapses runs of spaces and newlines into a single space, which shrinks the file before it is served.

Common use cases

  • Reading the source of a minified landing page or a third-party embed snippet.
  • Cleaning up HTML pasted from a WYSIWYG editor before committing it to a template.
  • Debugging layout bugs caused by an element that was closed in the wrong place.
  • Shrinking a static HTML email or widget before you paste it into another system.

Pitfalls to watch for

Whitespace in HTML is not always meaningless. Between inline elements such as <span>, <a> or <button>, a newline renders as a visible space, so re-indenting can add or remove a few pixels of gap between buttons or icons. Content inside <pre> and <textarea> is whitespace-sensitive, and minifying it will collapse line breaks that you probably wanted to keep. If a section must stay byte-for-byte identical, format the rest of the document and paste that block back in by hand.

A formatter is not a validator. It will happily indent markup with mismatched tags, and the result will look lopsided rather than throw an error, which is itself a useful hint that something is not closed properly. Inline <script> and <style> blocks are treated as text, so format those with the dedicated JavaScript and CSS tools instead.

All formatting happens locally in your browser with plain JavaScript; your markup is not uploaded anywhere. Your last input is kept in the browser's local storage so it is still there when you come back.

Frequently asked questions

Will formatting change how my page looks?

Usually not, but whitespace between inline elements can render as a small visible space, and minifying collapses line breaks inside pre and textarea elements. Check those areas after formatting.

Does the HTML formatter validate my markup?

No. It re-indents tags but does not check them against the HTML specification. Unbalanced indentation in the output is often a sign of a missing or extra closing tag.

Can I format inline JavaScript and CSS too?

Script and style contents are treated as plain text here. Use the JavaScript Formatter and CSS Formatter tools for those blocks.

Is my HTML sent to a server?

No. Formatting and minifying run in your browser, and the input is only stored locally so it survives a page reload.