理解 HTML 標籤分類是寫出結構清晰、樣式可控、語義豐富代碼的基礎。本文從語法結構、顯示類型、語義分類三個維度建立完整知識體系。
| 類型 | 語法 | 別稱 | 常見標籤 | 是否有內容 |
|---|---|---|---|---|
| 單標籤 | <tag> 或 <tag /> |
空元素、Void Element | br, hr, img, input, meta, link, area, base, col, embed, param, source, track, wbr | 無內容、無閉合標籤 |
| 雙標籤 | <tag>內容</tag> |
容器元素、Container | div, p, span, h1-h6, ul, ol, li, table, form, a, button, section, article | 有內容、必須成對 |
關鍵規則:
<br>、<img><br />、<img /><p><div></p></div> ❌| 顯示類型 | 預設標籤 | 佈局特性 | 可設寬高 | 盒模型 |
|---|---|---|---|---|
| 區塊級 | div, h1-h6, p, ul, ol, li, table, form, header, footer, section, article, aside, nav, main | 獨佔一行、垂直堆疊 | ✅ 是 | 完整盒模型 |
| 行內級 | span, a, strong, em, img, input, button, label, select, textarea, cite, code, time | 不換行、水平排列 | ❌ 否 | 僅水平 padding/margin/border |
| 行內區塊 | img, input, button, select, textarea, video | 不換行、可設寬高 | ✅ 是 | 完整盒模型 |
| 表格相關 | table, tr, th, td, thead, tbody, tfoot, caption, colgroup, col | 特殊表格佈局 | 特殊 | 表格盒模型 |
| Flex/Grid 容器 | display:flex/grid 的元素 | 彈性/網格佈局 | ✅ 是 | Flex/Grid 盒模型 |
| 語義類別 | 標籤 | 用途說明 |
|---|---|---|
| 文檔結構 | html, head, body, title, meta, link, style, script, base | 文檔骨架、元資料、資源載入 |
| 頁面區塊 | header, nav, main, aside, footer, section, article, div | 頁面大區塊語義化劃分 |
| 文字結構 | h1-h6, p, hr, pre, blockquote, ol, ul, li, dl, dt, dd, figure, figcaption, div | 內容層級、列表、引用、圖文組合 |
| 文字語義 | a, em, strong, small, s, cite, q, dfn, abbr, ruby, rt, rp, data, time, code, var, samp, kbd, sub, sup, i, b, u, mark, bdi, bdo, span, br, wbr | 行內語義標記 |
| 編輯標記 | ins, del | 內容修訂追蹤 |
| 媒體嵌入 | img, picture, source, video, audio, track, map, area, iframe, embed, object, param, canvas, svg, math | 多媒體內容嵌入 |
| 表格數據 | table, caption, colgroup, col, thead, tbody, tfoot, tr, th, td | 結構化數據表格 |
| 表單互動 | form, label, input, button, select, datalist, optgroup, option, textarea, keygen, output, progress, meter, fieldset, legend | 用戶輸入收集 |
| 互動元件 | details, summary, dialog, command, menu, menuitem, slot, template | 原生互動組件 |
| 腳本支援 | script, noscript, template, slot, canvas | 客戶端腳本相關 |
<!-- 正確寫法 -->
<img src="photo.jpg" alt="風景照片" width="800" height="600" loading="lazy">
<a href="https://example.com" target="_blank" rel="noopener noreferrer">連結</a>
<input type="email" name="email" required autocomplete="email" placeholder="信箱">
<!-- 常見錯誤 -->
<img src=photo.jpg> <!-- 屬性值未加引號 -->
<input type=email name=email> <!-- 屬性值未加引號 -->
<div class=container> <!-- class 值未加引號 -->
< br > <!-- 標籤名與括號間有空格 -->
<div><p>...</div></p> <!-- 交叉嵌套錯誤 -->
<!-- 數值字符引用 -->
<!-- 十進制 --> < (<)
<!-- 十六進制 --> < (<)
<!-- 字符實體引用 -->
< > & " ' © ®
<!-- HTML 註釋 -->
<!-- 這是註釋,不會顯示在頁面 -->
<!--
多行註釋
可跨行
-->
<!--[if IE]> IE 專用代碼 <![endif]--> <!-- 條件註釋(已廢棄) -->
alt、loading="lazy"<label for><caption>、<th scope>建立清晰的標籤分類心智模型,是前端工程師進階的關鍵一步。