font 綜合屬性可合併 font-style、font-variant、font-weight、font-size/line-height、font-family,大幅精簡代碼。
font: [style] [variant] [weight] size/line-height family;
範例:
/* 完整寫法 */
font: italic small-caps bold 1em/1.5 "Microsoft JhengHei", sans-serif;
/* 等效展開 */
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 1em;
line-height: 1.5;
font-family: "Microsoft JhengHei", sans-serif;
font-size、font-family(缺一不可,否則整行無效)。font-style、font-variant、font-weight、line-height(省略時採用預設值 normal / normal / normal / normal)。line-height 必須緊跟 font-size 並以斜線分隔:1em/1.5。| 錯誤寫法 | 問題 | 正確寫法 |
|---|---|---|
font: 14px "Arial"; | 缺行高斜線,部分瀏覽器解析失敗 | font: 14px/1.5 "Arial"; |
font: bold 14px/1.5; | 缺 font-family | font: bold 14px/1.5 "Arial", sans-serif; |
font: italic 14px/1.5 "Arial"; | 順序錯誤(weight 應在 variant 後) | font: italic normal bold 14px/1.5 "Arial"; |
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Microsoft JhengHei", sans-serif;
優先使用系統原生 UI 字體,無需載入 Web Font,效能最佳。
熟練 font 縮寫,配合 CSS 變數管理字體標度,能讓排版系統既精簡又具擴展性。