文檔結構標籤
| 標籤 | 語義 | 屬性示例 |
<html> | 文檔根元素 | lang="zh-Hant" |
<head> | 元數據容器 | — |
<title> | 文檔標題 | — |
<meta> | 元數據 | charset="UTF-8", name="viewport" content="width=device-width, initial-scale=1" |
<link> | 外部資源關聯 | rel="stylesheet" href="style.css" |
<style> | 內部樣式 | — |
<script> | 腳本 | src="app.js" defer |
<body> | 可見內容容器 | — |
語義化佈局標籤 (HTML5)
| 標籤 | 語義 | 典型用途 |
<header> | 頁面/區塊頭部 | Logo、導航、標題 |
<nav> | 導航鏈接組 | 主導航、側邊欄、麵包屑 |
<main> | 主要內容區 | 頁面核心內容(唯一) |
<article> | 獨立完整內容 | 博客文、新聞、評論 |
<section> | 主題性內容分組 | 章節、標籤頁面板 |
<aside> | 側邊內容 | 側邊欄、廣告、相關鏈接 |
<footer> | 頁面/區塊底部 | 版權、聯系、相關鏈接 |
<address> | 聯系信息 | 作者/機構聯系方式 |
文本內容標籤
| 標籤 | 語義 | 預設樣式 | 備註 |
<h1>-<h6> | 標題層級 | 粗體、字體遞減 | 頁面僅一個 h1 |
<p> | 段落 | 上下邊距 | — |
<pre> | 預格式化文本 | 等寬字體、保留空白換行 | 代碼塊常用 |
<blockquote> | 長引用 | 縮進 | 配合 cite 屬性 |
<q> | 短行內引用 | 自動添加引號 | — |
<hr> | 主題分隔 | 水平線 | 語義非裝飾 |
文本語義/格式化標籤
| 標籤 | 語義 | 預設樣式 | 區別說明 |
<strong> | 重要性強調 | 粗體 | 語義 > <b> |
<em> | 語氣強調 | 斜體 | 語義 > <i> |
<b> | 關鍵字/產品名(無語義強調) | 粗體 | 純視覺 |
<i> | 異質文本(術語、思維、船名) | 斜體 | 純視覺 |
<u> | 非文本標註(拼音、拼寫錯誤) | 下劃線 | HTML5 重新定義 |
<mark> | 高亮/標記 | 黃底 | 搜尋結果高亮 |
<small> | 側註/細則/版權 | 小字 | 非單純縮小字體 |
<del> | 刪除內容 | 刪除線 | 配合 datetime |
<ins> | 插入內容 | 下劃線 | 配合 datetime |
<sub>/<sup> | 下標/上標 | 縮小、垂直偏移 | 化學式、腳註 |
<code> | 代碼片段 | 等寬字體 | 行內代碼 |
<kbd> | 鍵盤輸入 | 等寬、邊框 | 快捷鍵提示 |
<samp> | 程序輸出 | 等寬 | — |
<var> | 變量 | 斜體等寬 | 數學/代碼變量 |
<abbr> | 縮寫 | 底部虛線 | title 屬性展開全稱 |
<dfn> | 定義實例 | 斜體 | 定義術語處 |
<cite> | 作品標題 | 斜體 | 書名、電影、論文 |
<time> | 時間/日期 | — | datetime 機器可讀格式 |
<data> | 機器可讀數據 | — | value 屬性 |
列表標籤
<ul> /* 無序列表 */
<li>項目</li>
</ul>
<ol> /* 有序列表 */
<li>步驟 1</li>
<li>步驟 2</li>
</ol>
<dl> /* 描述列表 */
<dt>術語</dt>
<dd>定義</dd>
</dl>
鏈接與媒體
<a href="url" target="_blank" rel="noopener noreferrer">鏈接</a>
<img src="img.jpg" alt="描述" width="300" height="200" loading="lazy">
<picture>
<source srcset="img.avif" type="image/avif">
<source srcset="img.webp" type="image/webp">
<img src="img.jpg" alt="描述">
</picture>
<video src="video.mp4" controls preload="metadata" poster="poster.jpg"></video>
<audio src="audio.mp3" controls></audio>
<iframe src="https://example.com" title="嵌入頁面" loading="lazy"></iframe>
表單標籤
<form action="/submit" method="post">
<label for="name">姓名</label>
<input type="text" id="name" name="name" required>
<label for="email">信箱</label>
<input type="email" id="email" name="email">
<label for="pwd">密碼</label>
<input type="password" id="pwd" name="pwd">
<fieldset>
<legend>性別</legend>
<label><input type="radio" name="gender" value="m"> 男</label>
<label><input type="radio" name="gender" value="f"> 女</label>
</fieldset>
<label for="bio">簡介</label>
<textarea id="bio" name="bio" rows="4"></textarea>
<label for="country">國家</label>
<select id="country" name="country">
<option value="tw">台灣</option>
<option value="hk">香港</option>
</select>
<button type="submit">提交</button>
<button type="reset">重置</button>
</form>
HTML5 新增輸入類型:tel、url、number、range、date、datetime-local、month、week、time、color、search。
表格標籤
<table>
<caption>數據統計表</caption>
<thead>
<tr><th scope="col">項目</th><th scope="col">數值</th></tr>
</thead>
<tbody>
<tr><th scope="row">收入</th><td>$1000</td></tr>
<tr><th scope="row">支出</th><td>$500</td></tr>
</tbody>
<tfoot>
<tr><th scope="row">淨利</th><td>$500</td></tr>
</tfoot>
</table>
互動與腳本支援
<details>
<summary>點擊展開</summary>
<p>詳細內容...</p>
</details>
<dialog open>
<h3>模態對話框</h3>
<button autofocus>確定</button>
</dialog>
<template id="tpl">
<div class="card">模板內容</div>
</template>
<slot> /* Web Components 影子 DOM 插槽 */ </slot>
廢棄/不推薦標籤(勿用)
<font>、<center>、<strike>、<big>、<tt>、<frame>、<frameset>、<noframes>、<acronym>、<applet>、<isindex>、<dir>、<basefont>
- 替代方案:CSS (
font-size, text-align, text-decoration) + 語義化標籤。
總結
**語義化 HTML 是無障礙、SEO、維護性的基石**。熟記上述標籤語義、屬性、預設行為與嵌套規則,避免「Divitis」(濫用 div/span)。**原則**:先選最具體語義標籤,再考慮通用容器。配合 CSS 實現視覺表現,構建結構清晰、機器可讀、樣式解耦的優質前端代碼庫。