<>

HTML Minifier

Remove comments and unnecessary whitespace from HTML

HTML Minifier — what it does

Strip comments and collapse redundant whitespace from HTML, with each behaviour toggled separately so you can be conservative where it matters. The saving is real on markup-heavy pages, and on top of gzip it still trims bytes from every response your server sends.

Worked example

An indented invoice snippet with an ordinary comment, a conditional comment and text that wraps across lines, minified with both options ticked.

Input — indented HTML

<!-- Invoice card, generated 2026-10-20 -->
<div class="card">
    <h2>Invoice 1042</h2>
    <p>
        Due   <strong>2026-11-05</strong>.
        Pay by bank transfer.
    </p>
    <!--[if IE]><p>Legacy browser</p><![endif]-->
    <a href="pay.html">Pay now</a> <a href="help.html">Help</a>
</div>

Output — status 299 → 214

<div class="card"><h2>Invoice 1042</h2><p> Due <strong>2026-11-05</strong>. Pay by bank transfer. </p> <!--[if IE]><p>Legacy browser</p><![endif]--> <a href="pay.html">Pay now</a> <a href="help.html">Help</a></div>

Only the plain comment is removed; the conditional comment is kept. Each line break became one space, so ". Pay by" stays readable and the space between the two links survives; spaces were removed only between two tags where one is block-level (<div><h2>), so a space remains just inside <p> and next to the conditional comment.

When this tool helps

  • Reducing page weight before deploying a static site or landing page.
  • Removing developer comments from markup that will be publicly visible in view-source.
  • Compacting an HTML email template where total message size matters.

How to use it

  1. Paste the HTML into the input box.
  2. Choose whether to remove comments and collapse whitespace.
  3. Process the markup.
  4. Copy the minified output.

Accuracy, limits, and good practice

  • Collapsing whitespace can close up intentional spacing between inline elements — check nav links and buttons after minifying.
  • Keep conditional comments if you still support legacy environments that depend on them.
  • Minify at build or deploy time and keep the readable source in version control; committing minified HTML makes reviews painful.
  • Measure before and after. If the saving is under a kilobyte, the risk of collapsing meaningful whitespace is not worth taking.

Frequently asked questions

How much does it actually save?

Typically 10 to 25 percent before compression on hand-written markup, and less once gzip or brotli is already applied.

Is it safe for pre and textarea content?

Whitespace inside those elements is content. Verify their contents in the output rather than assuming they were skipped.

Does it minify inline CSS and JavaScript?

This tool focuses on markup. Run the CSS and JavaScript formatters on those blocks separately for full compression.

Does removing comments break anything?

Only if a comment is functional, such as a conditional comment or a marker a build tool looks for. Check for those before enabling removal.

What do the numbers in the status line mean?

Character counts of the pasted text and of the result (299 → 214 above), not bytes. With only Remove comments ticked the same input shows 299 → 255.

What exactly does Collapse whitespace change?

Every run of spaces, tabs and line breaks becomes a single space, also inside attribute values (title="Due on⏎ Friday" becomes "Due on Friday"). A space is then removed only when it sits between two tags and one of them is block-level, such as div, p, li, tr or h1–h6; the space between </span> and <span> is kept. The contents of <pre>, <textarea>, <script> and <style> are left untouched, line breaks included.

Related tools on this site

Everything above runs inside this page. HTML Minifier needs no account, no upload, and no server round trip — close the tab and nothing is left behind.

HTML 壓縮工具能做什麼

移除 HTML 註解並收攏多餘空白,兩種行為可分別開關,讓你在關鍵處保持保守。標記量大的頁面省下的量是實在的,即使在 gzip 之上,也仍能為每個回應再削掉一些位元組。

實際範例

一段縮排過的發票片段,含一般註解、條件註解與跨行的文字,兩個選項都勾選後壓縮。

原文:縮排過的 HTML

<!-- Invoice card, generated 2026-10-20 -->
<div class="card">
    <h2>Invoice 1042</h2>
    <p>
        Due   <strong>2026-11-05</strong>.
        Pay by bank transfer.
    </p>
    <!--[if IE]><p>Legacy browser</p><![endif]-->
    <a href="pay.html">Pay now</a> <a href="help.html">Help</a>
</div>

壓縮後:狀態列 299 → 214

<div class="card"><h2>Invoice 1042</h2><p> Due <strong>2026-11-05</strong>. Pay by bank transfer. </p> <!--[if IE]><p>Legacy browser</p><![endif]--> <a href="pay.html">Pay now</a> <a href="help.html">Help</a></div>

只有一般註解被拿掉,條件註解保留。每個換行都變成一個空格,所以「. Pay by」仍可正常閱讀,兩個連結之間的空格也還在;只有兩個標籤之間、且其中一個是區塊元素(<div><h2>)時才會刪掉空格,因此 <p> 內側與條件註解旁仍留有一個空格。

什麼時候用得上

  • 部署靜態網站或到達頁前,先減少頁面重量。
  • 移除會在檢視原始碼時公開可見的開發註解。
  • 壓縮 HTML 郵件範本,因為信件總大小很重要。

使用步驟

  1. 把 HTML 貼進輸入框。
  2. 選擇是否移除註解、是否收攏空白。
  3. 執行處理。
  4. 複製壓縮後的輸出。

精確度、限制與實務建議

  • 收攏空白可能會把行內元素間刻意留的間距吃掉——壓縮後請檢查導覽連結與按鈕。
  • 若仍需支援依賴條件式註解的舊環境,請保留那些註解。
  • 請在建置或部署時壓縮,版本控制中保留可讀原始碼;提交壓縮後的 HTML 會讓程式碼審查很痛苦。
  • 壓縮前後都量一次。若省不到 1 KB,冒著壓掉有意義空白的風險就不值得。

常見問題

實際能省多少?

手寫標記在壓縮前通常省 10% 至 25%,若已啟用 gzip 或 brotli,額外效益會更少。

pre 與 textarea 的內容安全嗎?

這些元素內的空白就是內容。請在輸出中實際檢查,不要預設它們被跳過。

會壓縮內嵌的 CSS 與 JavaScript 嗎?

本工具專注在標記本身。那些區塊請分別用 CSS 與 JavaScript 工具處理。

移除註解會弄壞東西嗎?

只有當註解具有功能時才會,例如條件式註解或建置工具會尋找的標記。啟用前請先確認有沒有這類註解。

狀態列的數字是什麼意思?

分別是貼上文字與壓縮後文字的字元數(上例為 299 → 214),不是位元組數;只勾選去註解時,同一份原文會顯示 299 → 255。

收合空白到底改了什麼?

連續的空格、Tab 與換行一律變成一個空格,屬性值裡也一樣(title="Due on⏎ Friday" 會變成 "Due on Friday")。接著只有夾在兩個標籤之間、且其中一個是 div、p、li、tr、h1–h6 這類區塊元素時,那個空格才會被刪掉;</span> 與 <span> 之間的空格會保留。<pre>、<textarea>、<script> 與 <style> 的內容完全不動,換行也照樣保留。

本站相關工具

以上步驟全部在這個頁面內完成,HTML 壓縮工具 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。