CSS3 的 Web 字体功能彻底改变了网页设计的字体使用方式。通过
@font-face规则,我们可以在网页中使用自定义字体,不再局限于用户系统安装的字体。这为网页设计提供了无限的创意可能性和品牌一致性。
@font-face 规则用于在网页中定义和加载自定义字体。它允许我们指定字体的来源、格式、样式和权重,然后在 CSS 中像使用系统字体一样使用这些自定义字体。
@font-face {
font-family: 'FontName';
src: url('font.woff2') format('woff2'),
url('font.woff') format('woff');
font-weight: normal;
font-style: normal;
}
font-family:字体名称src:字体文件来源和格式font-weight:字体粗细font-style:字体样式font-stretch:字体拉伸unicode-range:Unicode 范围font-display:字体显示策略@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
@font-face {
font-family: 'MyFont';
src: url('myfont.woff') format('woff');
}
@font-face {
font-family: 'MyFont';
src: url('myfont.ttf') format('truetype');
}
@font-face {
font-family: 'MyFont';
src: url('myfont.eot');
}
/* 推荐的字体格式组合 */
@font-face {
font-family: 'MyFont';
src:
url('myfont.woff2') format('woff2'),
url('myfont.woff') format('woff'),
url('myfont.ttf') format('truetype');
}
/* 加载单个字体文件 */
@font-face {
font-family: 'Open Sans';
src: url('opensans.woff2') format('woff2');
}
/* 加载多个字体文件 */
@font-face {
font-family: 'Open Sans';
src:
url('opensans-regular.woff2') format('woff2'),
url('opensans-regular.woff') format('woff');
font-weight: 400;
font-style: normal;
}
@font-face {
font-family: 'Open Sans';
src:
url('opensans-bold.woff2') format('woff2'),
url('opensans-bold.woff') format('woff');
font-weight: 700;
font-style: normal;
}
/* auto - 浏览器默认行为 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: auto;
}
/* block - 字体加载前隐藏文本 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: block;
}
/* swap - 使用后备字体,加载后替换 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: swap;
}
/* fallback - 使用后备字体,超时后放弃 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: fallback;
}
/* optional - 字体加载失败不影响显示 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: optional;
}
/* 使用自定义字体 */
body {
font-family: 'Open Sans', 'Microsoft YaHei', sans-serif;
}
/* 标题字体 */
h1, h2, h3 {
font-family: 'Roboto', sans-serif;
}
/* 代码字体 */
code, pre {
font-family: 'Fira Code', 'Courier New', monospace;
}
/* 定义不同粗细的字体 */
@font-face {
font-family: 'MyFont';
src: url('myfont-light.woff2') format('woff2');
font-weight: 300;
}
@font-face {
font-family: 'MyFont';
src: url('myfont-regular.woff2') format('woff2');
font-weight: 400;
}
@font-face {
font-family: 'MyFont';
src: url('myfont-bold.woff2') format('woff2');
font-weight: 700;
}
/* 使用不同粗细 */
.light-text {
font-weight: 300;
}
.normal-text {
font-weight: 400;
}
.bold-text {
font-weight: 700;
}
/* 定义不同样式的字体 */
@font-face {
font-family: 'MyFont';
src: url('myfont-italic.woff2') format('woff2');
font-style: italic;
}
@font-face {
font-family: 'MyFont';
src: url('myfont-oblique.woff2') format('woff2');
font-style: oblique;
}
/* 使用不同样式 */
.italic-text {
font-style: italic;
}
.oblique-text {
font-style: oblique;
}
/* 定义不同拉伸的字体 */
@font-face {
font-family: 'MyFont';
src: url('myfont-condensed.woff2') format('woff2');
font-stretch: condensed;
}
@font-face {
font-family: 'MyFont';
src: url('myfont-expanded.woff2') format('woff2');
font-stretch: expanded;
}
/* 使用不同拉伸 */
.condensed-text {
font-stretch: condensed;
}
.expanded-text {
font-stretch: expanded;
}
/* 字体定义 */
@font-face {
font-family: 'Inter';
src:
url('inter-regular.woff2') format('woff2'),
url('inter-regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'Inter';
src:
url('inter-medium.woff2') format('woff2'),
url('inter-medium.woff') format('woff');
font-weight: 500;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'Inter';
src:
url('inter-bold.woff2') format('woff2'),
url('inter-bold.woff') format('woff');
font-weight: 700;
font-style: normal;
font-display: swap;
}
/* 字体使用 */
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
h1, h2, h3 {
font-weight: 700;
}
.button {
font-weight: 500;
}
/* 中文字体定义 */
@font-face {
font-family: 'Noto Sans SC';
src:
url('noto-sans-sc-regular.woff2') format('woff2'),
url('noto-sans-sc-regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'Noto Sans SC';
src:
url('noto-sans-sc-bold.woff2') format('woff2'),
url('noto-sans-sc-bold.woff') format('woff');
font-weight: 700;
font-style: normal;
font-display: swap;
}
/* 字体使用 */
body {
font-family: 'Noto Sans SC', 'Microsoft YaHei', 'PingFang SC', sans-serif;
}
.title {
font-weight: 700;
}
/* 图标字体定义 */
@font-face {
font-family: 'Font Awesome';
src: url('fontawesome.woff2') format('woff2'),
url('fontawesome.woff') format('woff');
font-weight: normal;
font-style: normal;
font-display: block;
}
/* 图标使用 */
.icon {
font-family: 'Font Awesome';
font-style: normal;
font-weight: normal;
font-feature-settings: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.icon-home::before {
content: "\f015";
}
.icon-user::before {
content: "\f007";
}
/* 代码字体定义 */
@font-face {
font-family: 'Fira Code';
src:
url('firacode-regular.woff2') format('woff2'),
url('firacode-regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap;
}
/* 代码使用 */
code, pre, .code {
font-family: 'Fira Code', 'Courier New', monospace;
font-size: 14px;
line-height: 1.6;
}
/* 品牌字体定义 */
@font-face {
font-family: 'BrandFont';
src:
url('brand-regular.woff2') format('woff2'),
url('brand-regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'BrandFont';
src:
url('brand-bold.woff2') format('woff2'),
url('brand-bold.woff') format('woff');
font-weight: 700;
font-style: normal;
font-display: swap;
}
/* 品牌使用 */
.logo {
font-family: 'BrandFont', sans-serif;
font-weight: 700;
font-size: 24px;
}
.headline {
font-family: 'BrandFont', sans-serif;
font-weight: 400;
font-size: 32px;
}
/* 推荐 - 使用现代字体格式 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
/* 不推荐 - 使用过时的字体格式 */
@font-face {
font-family: 'MyFont';
src: url('myfont.ttf') format('truetype');
}
/* 推荐 - 使用 swap 策略 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: swap;
}
/* 不推荐 - 不使用 font-display */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
/* 推荐 - 使用子集化字体 */
@font-face {
font-family: 'MyFont';
src: url('myfont-latin.woff2') format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* 中文字符 */
@font-face {
font-family: 'MyFont';
src: url('myfont-chinese.woff2') format('woff2');
unicode-range: U+4E00-9FFF;
}
/* 推荐 - 提供后备字体 */
body {
font-family: 'MyFont', 'Arial', sans-serif;
}
/* 不推荐 - 不提供后备字体 */
body {
font-family: 'MyFont';
}
Web 字体在所有现代浏览器中都有良好的支持:
/* 推荐 - 使用 WOFF2 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
/* 不推荐 - 使用 TTF */
@font-face {
font-family: 'MyFont';
src: url('myfont.ttf') format('truetype');
}
/* 推荐 - 提供完整的后备字体 */
body {
font-family: 'MyFont', 'Arial', 'Helvetica', sans-serif;
}
/* 不推荐 - 后备字体不完整 */
body {
font-family: 'MyFont';
}
/* 推荐 - 使用 swap 策略 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: swap;
}
/* 不推荐 - 不使用 font-display */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
/* 推荐 - 优化字体加载 */
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: swap;
unicode-range: U+0000-00FF;
}
/* 不推荐 - 不优化字体加载 */
@font-face {
font-family: 'MyFont';
src: url('myfont.ttf') format('truetype');
}
CSS3 Web 字体功能为网页设计提供了强大的字体控制能力:
主要功能:
@font-face 规则:加载自定义字体实际应用:
最佳实践:
优势:
注意事项:
掌握 CSS3 Web 字体功能,能够让我们在网页中使用自定义字体,创建更加独特和专业的网页设计,大大提升网页的视觉效果和品牌形象。