字體是網頁 95% 的視覺內容。好的字體選取能提升閱讀體驗、強化品牌辨識度、支援多語系;錯誤的選取則導致版面崩壞、載入緩慢、版權風險。本文涵蓋 font-family 堆疊策略、Web Fonts 載入最佳化、可變字體應用、中文字體特別處理與 Core Web Vitals 優化。
/***** 經典系統字體堆疊 (System UI Font Stack) *****/ /* 讓網站在各平台使用原生 UI 字體,零下載、極速渲染 */ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; /***** 繁體中文友善堆疊 *****/ font-family: "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; /***** 等寬字體堆疊 (程式碼/終端機) *****/ font-family: "SF Mono", "Fira Code", "Fira Mono", Menlo, Consolas, "Liberation Mono", monospace;
sans-serif, serif, monospace)@font-face {
font-family: 'MyBrandFont';
src: url('/fonts/mybrand.woff2') format('woff2'),
url('/fonts/mybrand.woff') format('woff'); /* 兼容舊瀏覽器 */
font-display: swap; /* 關鍵:避免 FOIT,顯示備援字體再切換 */
font-weight: 400 700; /* 可變字體範圍 */
font-stretch: 75% 125%; /* 可變字體寬度範圍 */
unicode-range: U+0000-00FF, U+4E00-9FFF; /* 只下載拉丁+中文區段 */
}
| 值 | 行為 | 適用場景 |
|---|---|---|
swap | 立即顯示備援字體,字體載入完成後切換 | 推薦預設,平衡效能與體驗 |
optional | 極短等待 (100ms),超時則永久用備援字體 | 非關鍵裝飾字體、圖示字體 |
fallback | 短暫等待 (100ms),再顯示備援,載入後切換 | 次要文字內容 |
block | 無限期等待 (FOIT),隱形文字 | 極少用,Logo/標語等品牌關鍵處 |
auto | 瀏覽器預設 (通常類似 block) | 不指定時 |
<link rel="preload" as="font" type="font/woff2"
href="/fonts/mybrand-regular.woff2" crossorigin>
效果: 將字體下載提前到關鍵請求鏈,顯著改善 LCP (Largest Contentful Paint)。
可變字體 將多字重合併為單一檔案,透過 font-weight、font-stretch、font-optical-sizing 等軸線動態調整。
@font-face {
font-family: 'InterVariable';
src: url('/fonts/Inter-Variable.woff2') format('woff2-variations');
font-weight: 100 900;
font-stretch: 75% 125%;
font-display: swap;
}
:root {
--font-sans: 'InterVariable', system-ui, sans-serif;
}
/* 使用 */
h1 { font-weight: 700; } /* Bold */
h2 { font-weight: 600; } /* Semi-bold */
body { font-weight: 400; } /* Regular */
.small { font-weight: 300; } /* Light */
.tight { font-stretch: 85%; } /* Condensed */
優勢: 檔案體積通常 < 3 個靜態字重總和、支援細微動畫過渡、減少字體請求數。
| 挑戰 | 解決方案 |
|---|---|
| 字體檔案巨大 (10-30MB) | 子集化:只保留常用 3000-6000 字 + 專案專用字 |
| 無法預載全字集 | 動態子集化:fontsubset、Font Splitter、CDN 動態切片 |
| 系統字體差異大 (Win/Mac/Android) | 堆疊優先 PingFang TC / Microsoft JhengHei / Noto Sans TC |
| 授權成本高 | 優先使用開源字體:Noto Sans TC、思源黑體、江城圓體、Ma Shan Zheng |
:root {
/* 類型比例 Type Scale (Major Third 1.25) */
--text-xs: 0.75rem; /* 12px */
--text-sm: 0.875rem; /* 14px */
--text-base: 1rem; /* 16px */
--text-lg: 1.125rem; /* 18px */
--text-xl: 1.25rem; /* 20px */
--text-2xl: 1.5rem; /* 24px */
--text-3xl: 1.875rem; /* 30px */
--text-4xl: 2.25rem; /* 36px */
/* 行高系統 */
--leading-none: 1;
--leading-tight: 1.25;
--leading-snug: 1.375;
--leading-normal: 1.5;
--leading-relaxed: 1.625;
--leading-loose: 2;
}
/* 應用 */
body { font-size: var(--text-base); line-height: var(--leading-normal); }
h1 { font-size: var(--text-4xl); line-height: var(--leading-tight); }
.small { font-size: var(--text-sm); line-height: var(--leading-relaxed); }
| 指標 | 字體相關優化 | 工具驗證 |
|---|---|---|
| LCP | Preload 關鍵字體、font-display: swap、減少字體檔案大小 (子集化/可變字體) | Lighthouse, WebPageTest |
| CLS | 設定 size-adjust, ascent-override, descent-override, line-gap-override 讓備援字體佔位精確 | Chrome DevTools Layout Shift regions |
| FID/INP | 避免主執行緒解析大型字體、使用 font-display: optional 非關鍵字體 | Web Vitals extension |
@font-face {
font-family: 'Noto Sans TC Fallback';
src: local('PingFang TC'), local('Microsoft JhengHei');
size-adjust: 92.5%; /* 縮放備援字體寬度匹配 Web Font */
ascent-override: 90%; /* 調整上部高度 */
descent-override: 25%; /* 調整下部深度 */
line-gap-override: 0%; /* 行間隙 */
}
字體選取不只是挑好看,而是系統工程:堆疊策略保證可用性、Web Fonts 最佳化保證效能、可變字體減少請求、中文子集化解決體積、size-adjust 消除 CLS、授權合規避免法律風險。建立專案字體規範文件,納入 Design System,讓每位團隊成員都能正確使用字體,這才是成熟前端團隊的標配。