理解 HTML 標籤的分類與顯示特性,是掌握版面配置、CSS 佈局與語意化標記的基礎。本文依照 HTML5 規範,將標籤分為區塊級、行內級、替代元素與特殊類別,並說明各類別的巢狀規則與預設樣式行為。
預設佔滿整行寬度,前後自動換行,可包含行內元素或其他區塊元素(視內容模型而定)。常見標籤:
<div>、<section>、<article>、<aside>、<header>、<footer>、<main><p>、<h1>-<h6>、<blockquote>、<pre>、<hr><ul>、<ol>、<li>、<dl>、<dt>、<dd>、<table>、<thead>、<tbody>、<tr>、<td>、<th><form>、<fieldset>、<legend>不強制換行,寬高由內容決定,不可直接設定 width/height,通常只能包含資料或其他行內元素。常見標籤:
<a>、<strong>、<em>、<cite>、<code>、<span>、<mark>、<time>、<abbr>、<dfn>、<small><b>、<i>、<u>、<s>、<sub>、<sup>、<ruby>、<rt>、<rp><br>、<wbr>、<button>、<label>、<input>、<select>、<textarea>由外部資源取代內容,具有內在尺寸,可設定寬高。屬於行內替代元素:
<img>、<video>、<audio>、<canvas>、<iframe>、<embed>、<object>、<picture>、<svg>、<math>HTML5 不再單純用「區塊/行內」二分法,改採內容模型分類:
| 類別 | 說明 | 代表標籤 |
|---|---|---|
| Metadata content | 文件層級資訊 | base, link, meta, noscript, script, style, title |
| Flow content | 大多數 body 內容 | 幾乎所有可見標籤 |
| Sectioning content | 定義大綱區段 | article, aside, nav, section |
| Heading content | 標題 | h1-h6, hgroup |
| Phrasing content | 段落/文字層級 | 行內元素多數屬此類 |
| Embedded content | 外部資源 | img, video, canvas, iframe... |
| Interactive content | 使用者互動 | a, button, input, select, details... |
<a> 在 HTML5 中例外,可包裹區塊內容)。<p> 不可包含任何區塊元素(含另一個 <p>)。預設 display 值對應:
blockinlineinline-block(或 inline 但具內在尺寸)開發者可透過 CSS 改變顯示類型(如 display: flex、grid、inline-flex),但HTML 巢狀合法性不受 CSS 影響。
優先使用語意化標籤(article, section, nav, aside, header, footer, main, figure, figcaption, time, mark),僅在無適合語意標籤時使用 div 或 span。這有助於 SEO、無障礙與程式碼維護。