| 舊屬性 | CSS 屬性 | 說明與現代寫法 |
size="n" (1-7) | font-size | 單位:rem/em/px/%。1rem = 根元素字體大小(預設 16px) |
face="font1, font2" | font-family | 字體堆疊:font-family: "PingFang TC", "Microsoft JhengHei", sans-serif; |
color="#rrggbb" | color | 支援 #rgb、#rrggbb、rgb()、hsl()、currentColor |
| — | font-weight | normal(400)/bold(700)/100-900,視字體檔支援 |
| — | font-style | normal/italic/oblique |
| — | font-variant | small-caps 小型大寫字母 |
| — | line-height | 行高,**建議無單位數值** 1.5(相對字體大小倍數) |
| — | font (簡寫) | font: italic bold 1rem/1.5 "PingFang TC", sans-serif; 順序:style weight size/height family |
現代字體載入最佳實踐
1. 系統字體堆疊(零下載、極速渲染)
:root {
--font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
--font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}
body { font-family: var(--font-sans); }
2. Web Font 自託管 + font-display: swap
@font-face {
font-family: 'Noto Sans TC';
src: url('/fonts/NotoSansTC-VariableFont_wght.woff2') format('woff2');
font-weight: 100 900;
font-display: swap; /* 文字立即顯示 fallback,字體載入後無縫切換 */
}
@font-face {
font-family: 'Noto Sans TC';
src: url('/fonts/NotoSansTC-Italic-VariableFont_wght.woff2') format('woff2');
font-weight: 100 900;
font-style: italic;
font-display: swap;
}
3. 變數字體 替換多檔字重,減少請求
單一 .woff2 含 100-900 字重 + 斜體,較傳統 10+ 檔案減 90% 體積。
4. 預載關鍵字體
<link rel="preload" as="font" type="font/woff2" crossorigin href="/fonts/NotoSansTC-VariableFont_wght.woff2">
響應式字體縮放系統
:root {
--fs-300: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem); /* 小字 */
--fs-400: clamp(0.875rem, 0.8rem + 0.375vw, 1rem); /* 正文 */
--fs-500: clamp(1rem, 0.9rem + 0.5vw, 1.125rem); /* 大字 */
--fs-600: clamp(1.25rem, 1.1rem + 0.75vw, 1.5rem); /* 副標 */
--fs-700: clamp(1.5rem, 1.25rem + 1.25vw, 2rem); /* 標題 */
--fs-800: clamp(2rem, 1.5rem + 2.5vw, 3rem); /* 主標 */
}
html { font-size: 100%; } /* 尊重使用者瀏覽器預設字體大小設定 */
遷移檢查清單
- 全專案搜尋
<font>、<basefont>、<center>、color=""、bgcolor=""、align="" 等表現屬性
- 建立
_typography.css 或 CSS 變數系統集中管理字體規範
- 引入
normalize.css 或 modern-normalize 統一跨瀏覽器基準
- 設定
Content-Security-Policy: style-src 'self'; 禁止內聯樣式(配合 Nonce/Hash)
- 使用
stylelint 規則 font-family-no-missing-generic-family-keyword、declaration-block-no-duplicate-properties 攔截問題
徹底告別 <font>,擁抱 CSS 字體系統,讓排版既優雅又高效能、可維護。
copyright ©
萬盛學電腦網 all rights reserved