萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

HTML 字體設定:從棄用 <font> 到現代 CSS 排版系統

已棄用的 <font> 標籤 (勿用)

<font size="2" face="Arial" color="#ff0000">文字</font>

缺點:結構與表現耦合、維護困難、無回應式能力、違反 HTML5 規範。

現代 CSS 字體屬性

屬性說明範例
font-family字體堆疊,備援機制font-family: "PingFang TC", "Microsoft JhengHei", sans-serif;
font-size字體大小,相對單位優先font-size: 1rem; /* 16px */
font-weight粗細font-weight: 400 | 700 | bold;
font-style斜體font-style: italic | normal;
line-height行高 (無單位為倍數)line-height: 1.6;
color文字顏色color: #212529;
font速記屬性font: 1rem/1.6 "PingFang TC", sans-serif;

Web Fonts 載入策略

/* @font-face 自訂字體 */
@font-face {
  font-family: 'MyFont';
  src: url('/fonts/myfont.woff2') format('woff2'),
       url('/fonts/myfont.woff') format('woff');
  font-display: swap; /* 關鍵:避免字體載入期間文字隱形 */
}

/* Google Fonts 预连接 + 预加载 */
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;700&display=swap">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;700&display=swap" media="print" onload="this.media='all'">

回應式字體大小

:root { font-size: 16px; } /* 基準 */
@media (min-width: 768px) { :root { font-size: 18px; } }
@media (min-width: 1200px) { :root { font-size: 20px; } }
/* 全站使用 rem,自動隨根元素縮放 */

可變字體

單一字體檔包含多種字重、寬度、斜度軸 (wght, wdth, slnt, opsz...),大幅減少請求數與檔案體積。支援瀏覽器:Chrome 62+, Firefox 62+, Safari 11+, Edge 79+。

copyright © 萬盛學電腦網 all rights reserved