HTML Formatter
CodeBeautify and indent HTML markup instantly — paste your code, choose indent size, and get clean readable HTML. Runs entirely in your browser, nothing uploaded.
What is a HTML?
An HTML Formatter takes minified, collapsed, or inconsistently indented HTML markup and reformats it into a clean, human-readable structure with consistent indentation. Every opening tag increments the indent depth, every closing tag decrements it, and inline elements like <a>, <strong>, and <span> are kept on the same line as their parent text so the output does not look artificially fragmented.
Minified HTML — output by bundlers, CMSs, or templating engines — collapses all whitespace to reduce file size. While that is ideal for production delivery, it is extremely difficult to read or debug. A formatter like this one restores the visual hierarchy so you can see at a glance which elements are nested inside which, where a <div> begins and ends, and what text content each element contains.
The formatter handles complete HTML5 documents (with <!DOCTYPE html> and <html>/<head>/<body> structure), bare fragments (a single <section> block or a snippet of template code), and everything in between. HTML comments are preserved and indented to match their surrounding context. Self-closing void elements (<br>, <img>, <input>, <meta>, <link>) are placed on their own indented line.
Content inside <pre>, <script>, and <style> tags is preserved verbatim — the formatter does not attempt to reindent code or preformatted text inside those blocks, since doing so would change the rendered output. For CSS inside <style> blocks, use the CSS Formatter after extracting it.
How to use this HTML calculator
- Paste your HTML into the HTML input field. You can paste a complete document or any fragment.
- Choose an Indent size — 2 spaces (default), 4 spaces, or tab.
- The formatted output appears instantly in the Formatted HTML panel below.
- Review the result — nested elements should appear indented one level inside their parents.
- Click the Copy button to copy the formatted HTML to your clipboard.
- Paste directly into your editor, template file, or wherever you need it.
Formula & Methodology
The formatter tokenises the input HTML using a regex that splits the string at each tag boundary, yielding a sequence of tokens: DOCTYPE declarations, comments, opening tags, closing tags, self-closing tags, and text nodes. Indent logic: - Opening tag (non-void, non-inline): output at current depth, then increment depth by 1. - Closing tag (non-inline): decrement depth by 1, then output at new depth. - Void element (br, img, input, etc.): output at current depth, depth unchanged. - Inline element (a, span, strong, etc.): append to the previous output line rather than starting a new one. - Text node: append to the previous output line (keeps text joined with its opening tag). - Comment / DOCTYPE: output at current depth, depth unchanged. Before:html <div><h1>Hello</h1><p>This is a <strong>sample</strong> page.</p></div>After (2-space indent):html <div> <h1>Hello</h1> <p>This is a <strong>sample</strong> page.</p> </div>