早期 HTML 使用 <font>, <b>, <i> 等標籤控制字體,現代開發**完全改用 CSS**,但理解 HTML 原生標籤語意與 CSS 字體層級機制,對語意化、無障礙、維護性至關重要。
| 標籤 | 語意 | 預設樣式 | CSS 替代 |
|---|---|---|---|
<h1>-<h6> | 標題層級 | 粗體、遞減字級 | font-size, font-weight |
<p> | 段落 | 上下外距 | margin-block |
<strong> | 重要性強調 | 粗體 | font-weight:bold |
<em> | 語氣強調 | 斜體 | font-style:italic |
<mark> | 高亮標記 | 黃底 | background:yellow |
<small> | 細則/附註 | 較小字 | font-size:smaller |
<cite> | 作品標題 | 斜體 | font-style:italic |
<code> | 行內代碼 | 等寬字體 | font-family:monospace |
<kbd> | 鍵盤輸入 | 等寬字體 | font-family:monospace |
<samp> | 程式輸出 | 等寬字體 | font-family:monospace |
<var> | 變數 | 斜體 | font-style:italic |
<dfn> | 定義術語 | 斜體 | font-style:italic |
<abbr> | 縮寫 | 底點線 | text-decoration:underline dotted |
<sub>, <sup> | 下標/上標 | 縮小上下偏移 | vertical-align:sub/super |
<ruby>, <rt>, <rp> | 注音/拼音 | — | 中日韓直排標註 |
<bdi>, <bdo> | 雙向文字隔離/覆寫 | — | RTL/LTR 混排 |
<b> 粗體 → <strong> 或 CSS。<i> 斜體 → <em> 或 CSS。<u> 底線 → text-decoration:underline (語意:標註錯誤/專有名詞)。<s> 刪除線 → <del> (語意刪除) 或 CSS。<big> 放大 → CSS font-size:larger (已移除)。<tt> 打字機 → CSS font-family:monospace (已移除)。<font> 字體/大小/顏色 → CSS (已移除)。<basefont> 基準字體 → CSS (已移除)。<center> 置中 → CSS text-align:center (已移除)。<strike> 刪除線 → <del>/<s> (已移除)。<blink> 閃爍 → CSS animation (非標準移除)。<marquee> 跑馬燈 → CSS animation (非標準移除)。font-family, font-size, color, line-height 等可繼承;margin, border 不繼承。em (相對父元素字級), rem (相對根元素 html 字級), %。建議根元素設 font-size: 100% (通常 16px),後續用 rem 保持比例彈性。font: [style] [variant] [weight] [size/line-height] [family];
/* 範例 */
font: italic small-caps bold 1rem/1.5 "Noto Sans TC", sans-serif;
/* 順序固定,size/line-height 必寫,其餘可省略 (用預設值) */
font-family:字體堆疊,英文字體前、中文字體後,末端加通用族系 sans-serif/serif/monospace。font-size:rem (推薦), px (固定), clamp() (流體排版)。font-weight:100-900 或 normal/bold,變數字體支援任意數值。font-style:normal, italic, oblique。font-variant:small-caps, ligatures, numeric 等 OpenType 功能。line-height:無單位數值 (倍率) 最佳,避免繼承陷阱。color:文字顏色,支援十六進位、rgb、hsl、關鍵字、系統色彩。@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2'),
url('myfont.woff') format('woff');
font-display: swap; /* 避免字體載入期間文字隱藏 */
unicode-range: U+4E00-9FFF; /* 只載入中文範圍,減少檔案 */
}
font-display: swap 確保文字立即顯示 (FOUT),體驗優於 FOIT。unicode-range 按需載入,大幅減少中文字體頻寬。:root {
--font-base: 100%; /* 16px */
--line-height: 1.6;
}
html { font-size: var(--font-base); }
body { font-family: system-ui, "Noto Sans TC", sans-serif; line-height: var(--line-height); }
h1 { font-size: clamp(2rem, 5vw, 4rem); font-weight: 700; line-height: 1.2; }
h2 { font-size: clamp(1.5rem, 4vw, 3rem); font-weight: 600; }
p { font-size: 1rem; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
掌握語意標籤與 CSS 字體機制,打造可維護、無障礙、效能優良的文字排版系統。