跳转到正文
HTM

HTML Formatter

空白の意味を壊さずにHTMLを整形・圧縮。

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>toco.tools Sample</title> <style> .card { color: #333; padding: 8px; } </style> </head> <body> <!-- header --> <div class="card"> <h1>Hello toco.tools</h1> <p>Format HTML without breaking the <a href="https://example.com">space before this link</a>.</p> <pre>function greet() { return "hi"; }</pre> <script> console.log("raw text is never re-parsed as HTML"); </script> </div> </body> </html>

Because whitespace in HTML can be meaningful, this tool avoids DOMParser and uses a dedicated tag-aware tokenizer instead. <pre> and <textarea> contents are kept byte-for-byte, and <script>/<style> contents are never re-parsed as HTML. Paragraphs containing inline elements (<a>, <span>, etc.) stay on one line to avoid introducing unintended spacing. Everything runs entirely in your browser; nothing is sent externally.

关于HTML Formatter

An HTML formatter indents nested tags for readability (format) or strips unnecessary whitespace to shrink payload size (minify). The hard part is that whitespace in HTML is not always meaningless: the newlines and indentation between block-level tags rarely affect rendering, but the whitespace inside `<pre>` and `<textarea>` is displayed or used as the initial value verbatim, and a single space next to an inline element in `<p>text <a>link</a></p>` renders as a real gap between words. A tool that blindly re-indents everything without telling these apart can silently change how a page looks or what a form field starts with. toco.tools's HTML formatter deliberately avoids `DOMParser`, because it normalises the document — inserting html/head/body, relocating misplaced nodes — so the output would no longer be the document you wrote. Instead it tokenizes the source with a character-scanning parser that correctly handles `>` inside quoted attribute values and never mistakes a `<` inside a `<script>`/`<style>` string for a tag, then builds an element tree before rendering. `<pre>` and `<textarea>` contents are never touched, not even a single byte, in either format or minify mode. `<script>`/`<style>` contents are never re-parsed as HTML (format shifts the whole block to the new indent level as a unit; minify only trims leading/trailing blank lines). Blocks whose content is made up entirely of inline elements (`a`, `span`, `strong`, etc.) are kept on one line so that inserting a newline never introduces a rendered space that wasn't there before. Minification only performs changes that are provably safe: stripping ordinary comments (while keeping conditional comments `<!--[if` and bang comments `<!--!`), fully collapsing whitespace between block-level tags, collapsing runs of whitespace inside text to a single space (never to zero, so words never merge), and trimming leading/trailing whitespace from the whole document. It does not remove optional closing tags, strip quotes from attribute values, or reorder/deduplicate attributes — doing so would rewrite structure you wrote on purpose, which conflicts with this tool's promise to never break your markup. Mismatched or unclosed tags are handled gracefully: the tool never crashes and never silently deletes content, even if it can't fully repair the structure. Everything runs entirely in your browser's JavaScript; nothing is ever sent anywhere.

使用方法

  1. Paste your HTML
  2. Choose format or minify
  3. Copy the result

常见问题

How is this different from other HTML formatters?

It avoids DOMParser and uses a dedicated tokenizer that correctly understands attribute values and the contents of script/style. Unlike DOMParser, it won't silently insert html/head/body or drop invalid markup — it just re-indents the document you actually wrote.

Is whitespace inside <pre> and <textarea> preserved?

Yes. In both format and minify mode, the contents of <pre> and <textarea> are never changed by even a single byte, since that whitespace can affect what's displayed or used as an initial value.

Does an inline element like <a> or <span> get broken onto its own line?

No. Content made up entirely of inline elements is kept on a single line, because inserting a newline next to an inline element would introduce an unintended visible gap.

Does it add omitted closing tags or reorder attributes?

No. Adding omitted closing tags, stripping attribute quotes, or reordering/deduplicating attributes would rewrite the structure you wrote, so these optimisations are intentionally left out in favour of never breaking your markup.

Is my HTML sent anywhere?

No. Formatting and minification run entirely in your browser's JavaScript; nothing is sent to toco.tools's servers or anywhere else.