<!-- 單行註解 -->
<!--
多行註解
可跨越多行
-->
| 場景 | 位置 | 說明 |
|---|---|---|
| HTML 結構註解 | .html 檔案、模板 | <!-- header start --> 標示區塊邊界 |
| CSS/JS 內嵌註解 | <style>、<script> 內部 | CSS 用 /* */,JS 用 // 或 /* */,不可用 HTML 註解語法 |
<!--[if IE]>
<link rel="stylesheet" href="ie-fix.css">
<![endif]-->
<!--[if gt IE 9]>>!-->
<link rel="stylesheet" href="modern.css">
<!--<![endif]-->
@supports)、Polyfill、漸進式增強 替代。| 工具/階段 | 預設行為 | 保留版權註解 |
|---|---|---|
| Vite (prod) | 移除 HTML/JS/CSS 註解 | build: { minify: 'terser', terserOptions: { compress: { comments: false } } } 設為 comments: '/@license|@preserve/' |
| Webpack (TerserPlugin) | 移除 | extractComments: false 或 /(^!|@license|@preserve)/ |
| HTMLMinifier | removeComments: true | removeComments: { safe: true } 保留 !--> 開頭 |
| Vue SFC 編譯 | 移除模板註解 | 無選項保留,建議寫在 <script> 區塊 JS 註解 |
| React JSX | 編譯時移除 {/* */} | 同左 |
curl 取得。<!-- region: Header -->
<header>...</header>
<!-- endregion -->
<!-- region: Main Content -->
<main>...</main>
<!-- endregion -->
VS Code、WebStorm、Sublime Text 等編輯器支援 // #region / // #endregion 或 HTML 註解摺疊,大幅提升大型模板可讀性。
HTML 註解語法簡單但有陷阱(不可巢狀、雙連字號、洩露資訊)。現代工作流中,註解應作為開發期文檔,經由建置工具自動剔除後再部署。善用編輯器摺疊標記、語義化區塊註解,讓團隊協作更高效。