字體排版是視覺設計的 90%。CSS 提供豐富的字體屬性控制字體族、大小、粗細、風格、變體、行高,並提供強大的 font 縮寫屬性。本文系統梳理所有字體相關屬性、取值、繼承行為、縮寫規則、現代最佳實踐(系統字體族、變量字體、流體排版、可變字體),構建專業級排版知識體系。
| 屬性 | 作用 | 常用值 | 初始值 | 繼承 |
|---|---|---|---|---|
font-family | 字體族列表,瀏覽器按序回退 | "PingFang SC", system-ui, sans-serif | 瀏覽器默認 | ✅ 是 |
font-size | 字號大小 | 16px, 1rem, 1.2em, clamp(1rem, 2vw, 2rem) | medium (≈16px) | ✅ 是 |
font-weight | 字重/粗細 | 400(normal), 700(bold), 100-900, lighter/bolder | 400 | ✅ 是 |
font-style | 字體風格 | normal, italic, oblique (可帶角度 oblique 20deg) | normal | ✅ 是 |
font-variant | 字體變體(小型大寫、連字、數字形態等) | normal, small-caps, lining-nums, tabular-nums | normal | ✅ 是 |
line-height | 行高(行框高度) | 1.5 (無單位倍數), 24px, 1.5em, normal | normal (~1.2) | ✅ 是 (倍數繼承計算值) |
font | 縮寫屬性 | 見下文縮寫規則 | 見各子屬性 | ✅ 是 |
font-stretch | 字體寬度(窄/寬變體) | normal, condensed, expanded, 50%-200% | normal | ✅ 是 |
font-optical-sizing | 光學尺寸自動調整(可變字體) | auto, none | auto | ✅ 是 |
font-feature-settings | OpenType 特性低級控制 | "liga" 1, "tnum" 1, "kern" 1 | normal | ✅ 是 |
font-variation-settings | 可變字體軸低級控制 | "wght" 400, "wdth" 100 | normal | ✅ 是 |
/* 現代系統字體族(推薦):零下載、原生渲染、完美支援當前平台 */
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
/* 中文項目優化:優先中文字體,再英文,最後通用族 */
font-family: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
/* 品牌字體 + 回退 */
font-family: "BrandFont", system-ui, sans-serif;
/* 等寬字體 */
font-family: "SF Mono", "Fira Code", "JetBrains Mono", Menlo, Monaco, Consolas, monospace;
💡 原則: 1) 首選系統字體族,性能最優;2) 自定義字體必須提供完整回退鏈;3) 最後必須以通用族(sans-serif/serif/monospace/system-ui)收尾;4) 含空格字體名必須加引號。
/* 根基準:1rem = 10px (html { font-size: 62.5%; }) */
html { font-size: 62.5%; }
/* 組件內部用 rem,保證整體縮放一致性 */
.card { font-size: 1.6rem; } /* 16px */
/* 文本內部用 em,相對父級縮放 */
.card__title { font-size: 1.25em; } /* 相對 .card 的 1.6rem → 20px */
/* 最小 1rem (16px) | 理想 2vw | 最大 2rem (32px) */
h1 { font-size: clamp(1rem, 2vw + 0.5rem, 2rem); }
h2 { font-size: clamp(1.25rem, 1.5vw + 0.5rem, 1.75rem); }
.body-text { font-size: clamp(0.875rem, 1vw + 0.5rem, 1.125rem); }
無需媒體查詢,視口寬度變化時字號平滑縮放,完美適配所有設備。
xx-small x-small small medium large x-large xx-large xxx-large
/* 縮放比例約 1.2 倍,medium ≈ 16px */
| 數值 | 關鍵字 | 常見字體對應 |
|---|---|---|
| 100 | Thin / Hairline | 極細 |
| 200 | ExtraLight / UltraLight | 超細 |
| 300 | Light | 細體 |
| 400 | Normal / Regular | 常規 |
| 500 | Medium | 中黑 |
| 600 | SemiBold / DemiBold | 中粗 |
| 700 | Bold | 粗體 |
| 800 | ExtraBold / UltraBold | 特粗 |
| 900 | Black / Heavy | 超粗 |
💡 注意: 非可變字體僅有離散字重,瀏覽器會映射到最接近的可用字重。可變字體支持連續 1-1000 任意值。
italic:使用字體專門設計的斜體字形(通常較優雅、有書法特徵)oblique:瀏覽器將正體字形機械傾斜(人造斜體)oblique 20deg:指定傾斜角度(可變字體支持)italic,字體無斜體字形時瀏覽器自動回退 oblique/* 高級語法(推薦) */
font-variant-numeric: tabular-nums lining-nums; /* 等寬襯底數字,對齊財務報表 */
font-variant-ligatures: common-ligatures discretionary-ligatures; /* 標準/選用連字 */
font-variant-caps: small-caps; /* 小型大寫字母 */
font-variant-east-asian: traditional; /* 繁體中文變體 */
/* 低級語法(精確控制特性標籤) */
font-feature-settings: "tnum" 1, "liga" 1, "kern" 1, "calt" 1;
/* tnum: 等寬數字, liga: 標準連字, kern: 字距調整, calt: 上下文替換 */
body { line-height: 1.5; } /* 繼承時傳遞倍數,子元素按自身 font-size 計算 */
優勢: 子元素字號變化時行高自動按比例縮放,保持垂直節奏。
/* ❌ 危險 */
.parent { font-size: 16px; line-height: 24px; } /* 計算值 24px */
.child { font-size: 32px; } /* 繼承 24px 固定行高 → 文字重疊 */
/* ✅ 安全 */
.parent { font-size: 16px; line-height: 1.5; } /* 計算值 24px,但傳遞 1.5 */
.child { font-size: 32px; } /* 實際行高 32 * 1.5 = 48px,正常 */
:root {
--baseline: 8px; /* 基線網格單位 */
}
body { line-height: calc(var(--baseline) * 3 / var(--font-size-base)); } /* 24px / 16px = 1.5 */
h1 { line-height: calc(var(--baseline) * 4 / var(--font-size-xl)); } /* 對齊基線網格 */
/* 完整語法順序(不可亂序) */
font: [style] [variant] [weight] [stretch] size/line-height family;
/* 實例 */
font: italic small-caps bold 16px/1.5 "PingFang SC", sans-serif;
font: 700 1.25rem/1.4 system-ui; /* 必須:size/line-height + family */
/* ❌ 常見錯誤 */
font: bold 16px "Arial"; /* 缺 line-height */
font: 16px/1.5; /* 缺 family */
font: "Arial" 16px/1.5; /* 順序錯誤 */
⚠️ 強制要求: font-size 和 font-family 必須同時出現,否則整條聲明無效。
/* 載入單一文件包含所有字重、寬度、斜度 */
@font-face {
font-family: 'InterVariable';
src: url('Inter-Variable.woff2') format('woff2-variations');
font-weight: 100 900;
font-stretch: 75% 125%;
font-style: normal;
}
/* 使用:任意組合字重、寬度、斜度 */
.title { font-family: 'InterVariable'; font-weight: 650; font-stretch: 110%; }
.body { font-family: 'InterVariable'; font-weight: 400; }
.caption { font-family: 'InterVariable'; font-weight: 300; font-stretch: 85%; }
優勢: 單文件 50-100KB 替換 10+ 靜態字體文件,任意插值、動畫過渡、光學尺寸自適應。
--font-sans, --font-mono, --font-size-*, --line-height-*, --font-weight-*)font-size: 62.5% + 組件 rem + 內部 em + 流體 clamp()line-height 無單位倍數,建立垂直節奏font-variant-numeric: tabular-nums 處理數據對齊font-display: optional 或 swap 避免 FOIT/FOUTCSS 字體屬性系統是排版引擎的核心控制面。從 font-family 回退鏈設計、font-size 流體響應式、font-weight 數值映射、line-height 垂直節奏,到 font 縮寫強制順序、OpenType 特性、可變字體革命,每個屬性都藏著深邃的排版智慧。掌握這套體系,配合設計令牌與現代工具鏈,你將擁有像排版大師一樣精準控制文字視覺表現的能力。