@font-face定義
是的,通常情況下,
不同的字型家族 (Different Font Families): 如果你有兩種完全不同的字型,比如 "Open Sans" 和 "Roboto",你需要為它們分別定義 @font-face: /* 定義 Open Sans 常規體 */ @font-face { font-family: 'Open Sans'; /* 給這個字型家族定義一個名稱 */ src: url('path/to/opensans-regular.woff2') format('woff2'), url('path/to/opensans-regular.woff') format('woff'); font-weight: normal; /* 或 400 */ font-style: normal; } /* 定義 Roboto 常規體 */ @font-face { font-family: 'Roboto'; /* 給這個字型家族定義一個名稱 */ src: url('path/to/roboto-regular.woff2') format('woff2'), url('path/to/roboto-regular.woff') format('woff'); font-weight: normal; /* 或 400 */ font-style: normal; } /* 使用它們 */ .element-using-open-sans { font-family: 'Open Sans', sans-serif; } .element-using-roboto { font-family: 'Roboto', sans-serif; }在這裡,'Open Sans' 和 'Roboto' 是你在 CSS 中用來引用這些字型的名稱。 同一字型家族,不同的字重 (font-weight) 或樣式 (font-style): 如果你有一個字型家族,但它有多個字重(例如:Light, Regular, Bold)或樣式(例如:Regular, Italic, Bold Italic),理想的做法是為 每個變體 定義一個 @font-face 規則,並使用 font-weight 和 font-style 描述符來指定它們。/* MyCustomFont - Regular (常規體) */ @font-face { font-family: 'MyCustomFont'; /* 統一的字型家族名稱 */ src: url('path/to/mycustomfont-regular.woff2') format('woff2'); font-weight: normal; /* 或 400 */ font-style: normal; } /* MyCustomFont - Bold (粗體) */ @font-face { font-family: 'MyCustomFont'; /* 相同的字型家族名稱 */ src: url('path/to/mycustomfont-bold.woff2') format('woff2'); font-weight: bold; /* 或 700 */ font-style: normal; } /* MyCustomFont - Italic (斜體) */ @font-face { font-family: 'MyCustomFont'; /* 相同的字型家族名稱 */ src: url('path/to/mycustomfont-italic.woff2') format('woff2'); font-weight: normal; font-style: italic; } /* MyCustomFont - Bold Italic (粗斜體) */ @font-face { font-family: 'MyCustomFont'; /* 相同的字型家族名稱 */ src: url('path/to/mycustomfont-bolditalic.woff2') format('woff2'); font-weight: bold; font-style: italic; } /* 使用它們 */ h1 { font-family: 'MyCustomFont', sans-serif; font-weight: bold; /* 瀏覽器會自動尋找 font-weight: bold 的 MyCustomFont */ } p { font-family: 'MyCustomFont', sans-serif; font-weight: normal; /* 瀏覽器會自動尋找 font-weight: normal 的 MyCustomFont */ } em { font-family: 'MyCustomFont', sans-serif; font-style: italic; /* 瀏覽器會自動尋找 font-style: italic 的 MyCustomFont */ }關鍵點: 對於同一字型家族的不同變體,font-family 的值是 相同 的。瀏覽器會根據你稍後在 CSS 規則中指定的 font-weight 和 font-style,去匹配 @font-face 規則中具有相應 font-weight 和 font-style 描述符的字型檔案。 這樣做的好處是,你只需要寫 font-family: 'MyCustomFont';,然後透過 font-weight: bold; 或 font-style: italic; 來改變樣式,瀏覽器就能自動選擇正確的字型檔案,而不是進行「偽粗體」或「偽斜體」的渲染(這種偽渲染通常效果不好)。
不同字型家族 -> 不同 @font-face 規則,不同的 font-family 名稱。 同一字型家族,不同字重/樣式 -> 不同 @font-face 規則,相同的 font-family 名稱,但 font-weight 和/或 font-style 描述符不同。
留言
張貼留言