← CSS font-size 字体大小 CSS font-weight 字体粗细 →

CSS font-family 字体家族选择

原创 2025-09-05 CSS 已有人查阅

CSS font-family 属性详解:字体家族选择的全面指南

概述

CSS 中的 font-family 属性用于指定 HTML 元素中文本内容的优选字体。该属性是网页排版的核心,直接影响内容的可读性和视觉表现。正确的字体选择能够提升用户体验,传达品牌调性,并确保跨平台的一致性显示。

基础概念与语法

基本语法结构

selector {
    font-family: primary-font, fallback-font, generic-family;
}

字体族名称指定

/* 单个字体指定 */
.body-text {
    font-family: "Microsoft YaHei";
}

/* 字体回退机制 */
.title {
    font-family: "Helvetica Neue", Arial, sans-serif;
}

通用字体族分类

五大通用字体族

1. 衬线字体 (Serif)

.serif-example {
    font-family: "Times New Roman", "SimSun", serif;
}

特征:字符末端带有装饰性衬线,传统且正式
适用场景:印刷出版物、正式文档、传统媒体
中文字体示例:宋体、仿宋、华文中宋
英文字体示例:Times New Roman, Georgia, Garamond

2. 无衬线字体 (Sans-serif)

.sans-serif-example {
    font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

特征:无装饰衬线,现代简洁
适用场景:网页正文、用户界面、数字内容
中文字体示例:微软雅黑、苹方、思源黑体
英文字体示例:Arial, Helvetica, Verdana

3. 等宽字体 (Monospace)

.monospace-example {
    font-family: "Consolas", "Courier New", monospace;
}

特征:每个字符宽度相同,对齐整齐
适用场景:代码编辑、终端显示、表格数据
中文字体示例:等线、等宽黑体
英文字体示例:Consolas, Monaco, Courier New

4. 手写字体 (Cursive)

.cursive-example {
    font-family: "Brush Script MT", cursive;
}

特征:模仿手写笔迹,流畅自然
适用场景:装饰性标题、个性化设计
注意事项:中文字体较少,主要用于英文

5. 幻想字体 (Fantasy)

.fantasy-example {
    font-family: "Impact", fantasy;
}

特征:装饰性强,具有艺术效果
适用场景:游戏界面、创意设计、标题装饰

高级应用技巧

字体栈策略 (Font Stack)

/* 综合字体栈示例 */
.optimal-stack {
    font-family: 
        "PingFang SC",            /* 苹方-简 (iOS/macOS) */
        "Microsoft YaHei",        /* 微软雅黑 (Windows) */
        "Hiragino Sans GB",       /* 冬青黑体 (macOS) */
        "WenQuanYi Micro Hei",    /* 文泉驿微米黑 (Linux) */
        "Noto Sans CJK SC",       /* 思源黑体 (跨平台) */
        sans-serif;               /* 通用回退 */
}

/* 英文优先字体栈 */
.english-priority {
    font-family: 
        "Helvetica Neue",
        "Segoe UI",
        "Roboto",
        "Arial",
        sans-serif;
}

系统字体优化

.system-fonts {
    /* 跨平台系统字体方案 */
    font-family: 
        -apple-system,            /* Apple 系统字体 */
        BlinkMacSystemFont,       /* macOS Chrome/Safari */
        "Segoe UI",               /* Windows */
        "PingFang SC",            /* 中文 macOS */
        "Hiragino Sans GB",       /* 中文 macOS 备用 */
        "Microsoft YaHei",        /* 中文 Windows */
        "WenQuanYi Micro Hei",    /* 中文 Linux */
        sans-serif;
}

Web 字体集成

/* 中文字体子集化示例 */
@font-face {
    font-family: "CustomChinese";
    src: 
        url("https://www.ebingou.cn/fonts/custom-chinese.woff2") format("woff2"),
        url("https://www.ebingou.cn/fonts/custom-chinese.woff") format("woff");
    font-weight: 400;
    font-style: normal;
    font-display: swap; /* 优化加载性能 */
}

/* 英文字体加载 */
@font-face {
    font-family: "CustomEnglish";
    src: 
        local("Helvetica Neue"),  /* 优先使用本地字体 */
        url("https://www.ebingou.cn/fonts/custom-english.woff2") format("woff2");
    font-display: swap;
}

.web-font-usage {
    font-family: "CustomEnglish", "CustomChinese", sans-serif;
}

实际应用示例

完整网页字体系统

:root {
    /* 定义字体变量 */
    --font-primary: "PingFang SC", "Microsoft YaHei", sans-serif;
    --font-secondary: "Helvetica Neue", Arial, sans-serif;
    --font-code: "Consolas", "Courier New", monospace;
    --font-quote: "Georgia", "Times New Roman", serif;
}

/* 应用层样式 */
body {
    font-family: var(--font-primary);
    line-height: 1.6;
}

.article-title {
    font-family: var(--font-secondary);
    font-weight: 700;
    font-size: 2.5rem;
}

.code-block {
    font-family: var(--font-code);
    font-size: 0.9rem;
    background: #f8f9fa;
    padding: 1rem;
    border-radius: 4px;
}

.blockquote {
    font-family: var(--font-quote);
    font-style: italic;
    border-left: 4px solid #e9ecef;
    padding-left: 1rem;
    margin-left: 0;
}

响应式字体方案

/* 移动端优先字体 */
.mobile-first {
    font-family: 
        system-ui,
        -apple-system,
        "PingFang SC",
        "Microsoft YaHei",
        sans-serif;
}

/* 平板设备适配 */
@media (min-width: 768px) {
    .tablet-optimized {
        font-family: 
            "Helvetica Neue",
            "PingFang SC",
            "Microsoft YaHei",
            sans-serif;
    }
}

/* 桌面端优化 */
@media (min-width: 1024px) {
    .desktop-optimized {
        font-family: 
            "Segoe UI",
            "PingFang SC",
            "Microsoft YaHei",
            sans-serif;
    }
}

字体性能优化

字体加载策略

/* 字体加载性能优化 */
.optimized-font-loading {
    font-family: 
        /* 系统字体优先 */
        -apple-system,
        BlinkMacSystemFont,
        /* 中文系统字体 */
        "PingFang SC",
        "Microsoft YaHei",
        /* 通用回退 */
        sans-serif;
}

/* 使用 font-display 控制渲染行为 */
@font-face {
    font-family: 'OptimizedWebFont';
    src: url('font.woff2') format('woff2');
    font-display: swap; /* 避免布局偏移 */
}

/* 字体加载状态处理 */
.font-loading {
    font-family: sans-serif;
    opacity: 0.8;
}

.font-loaded {
    font-family: 'OptimizedWebFont', sans-serif;
    opacity: 1;
    transition: opacity 0.3s ease;
}

本节课程知识要点

  1. 字体栈设计:掌握多字体回退机制,确保跨平台兼容性

  2. 分类应用:理解不同字体族的特性和适用场景

  3. 性能优化:学会使用系统字体和Web字体加载策略

  4. 响应式适配:根据不同设备选择合适的字体方案

  5. 可访问性:确保字体选择符合可访问性标准

  6. 变量管理:使用CSS变量统一管理字体系统

  7. 加载性能:优化字体加载避免布局偏移和性能问题

浏览器兼容性提示

  • 现代浏览器全面支持所有通用字体族

  • 中文字体文件较大,建议使用子集化或系统字体

  • font-display 属性需要新版本的浏览器支持

  • 考虑旧版浏览器的回退方案

通过系统掌握 font-family 属性的各种用法,开发者可以创建出既美观又实用的文本排版系统。更多关于CSS字体的教程示例可查看CSS font 字体查看CSS font-size 字体大小技术文档。

← CSS font-size 字体大小 CSS font-weight 字体粗细 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号