HTML 擁有超過 100 個標籤,但開發者常只用 20 個。本文精選 7 組「高價值、低使用率、廣泛支援」的標籤,能提升語義、易用性、維護性,甚至省去 JS 處理。
<base>:全頁連結基準與目標統一控制<head>
<base href="https://example.com/" />
<base target="_blank" />
</head>
href:所有相對連結(<a>、<img>、<link>、<script>)自動以此為基準解析target:全頁連結預設開啟方式(_blank、_self、_parent、_top)<base>,必須在 <head> 中,會影響錨點連結行為<caption>:表格標題,語義與無障礙雙贏<table>
<caption>2024 年 Q1 營收統計</caption>
<thead>...</thead>
<tbody>...</tbody>
</table>
<table> 之後,預設置中顯示於表格上方caption-side: bottom 移至底部<thead>/<tbody>/<tfoot>:表格結構化三部曲<table>
<thead><tr><th>科目</th><th>分數</th></tr></thead>
<tfoot><tr><td>總分</td><td>159</td></tr></tfoot>
<tbody>
<tr><td>國文</td><td>99</td></tr>
<tr><td>數學</td><td>60</td></tr>
</tbody>
</table>
thead → tfoot → tbody(瀏覽器可優先渲染表頭表尾)tbody { display: block; max-height: 300px; overflow-y: auto; } 實現表頭固定、內容捲動<fieldset> + <legend>:表單分組語義化<form>
<fieldset>
<legend>基本資料</legend>
<label>姓名:<input name="name"></label>
<label>性別:<input name="gender"></label>
</fieldset>
<fieldset>
<legend>聯絡方式</legend>
...
</fieldset>
</form>
border: 0; padding: 0;)再自訂風格<sub> / <sup>:下標/上標,科學數學必備H<sub>2</sub>O
E = mc<sup>2</sup>
第<sup>1</sup>季 & 第<sub>1</sub>項
vertical-align: sub/super + font-size: smaller 精確控制<label>:擴大點擊區域,表單易用性關鍵<!-- 寫法一:for 對應 input id -->
<label for="male">男性</label>
<input type="radio" name="gender" id="male" value="M">
<!-- 寫法二:直接包裹(隱式關聯) -->
<label>女性 <input type="radio" name="gender" value="F"></label>
checkbox、radio、file 等小型控制項<optgroup>:<select> 選項分組<select name="tour">
<optgroup label="自駕遊">
<option>省內</option>
<option>省外</option>
</optgroup>
<optgroup label="旅行社">
<option>國內團</option>
<option>出國團</option>
<option>郵輪遊</option>
</optgroup>
</select>
label 屬性為分組標題(不可選中)optgroup { font-weight: bold; })這些標籤無需 Polyfill、無兼容性負擔,卻能從語義、無障礙、維護性三個維度質變代碼品質。下次動筆前,先問:「有沒有更語義化的標籤?」