font: [font-style] [font-variant] [font-weight] font-size[/line-height] font-family;
順序固定,不可亂序。 中括號為可選,但 font-size 與 font-family 為必填。
/ * 完整寫法 * /
font: italic small-caps bold 1em/1.5em "Microsoft YaHei", sans-serif;
/ * 對應展開 * /
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 1em;
line-height: 1.5em;
font-family: "Microsoft YaHei", sans-serif;
| 需求 | 簡寫範例 |
|---|---|
| 基本字體大小與字體 | font: 16px "PingFang TC", sans-serif; |
| 加粗 + 行高 | font: bold 14px/1.5 Arial, sans-serif; |
| 斜體 + 小型大寫字母 | font: italic small-caps 12px/1.6 Georgia, serif; |
| 系統字體堆疊(iOS/macOS/Windows/Android) | font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; |
font-size、font-family(缺一不可,否則整條無效)。font-style: normalfont-variant: normalfont-weight: normalline-height: normal(約 1.2,視瀏覽器而定)font: bold 14px/1.5; → 無效。font: 14px bold Arial; → 瀏覽器視為 font-size=14px, font-family=bold, 缺字體 → 無效。font: 1.5/14px Arial; → 語法錯誤。font: inherit; 可單獨使用,但不能混入簡寫中。font 簡寫或展開寫法,避免混用造成維護困難。:root { --font-base: 1rem/1.5 "Noto Sans TC", sans-serif; }body { font: var(--font-base); }font-size(或根元素 font-size),line-height 保持無單位倍數自動縮放。