在HTML中设计字体,常用CSS设置,通过 font-family
指定字体类型, font-size
调整大小, color
改颜色,可引入外部字体(如Google Fonts),用 @import
或` 加载,优先使用系统安全字体(如Arial)确保跨平台兼容,结合
font-weight 控制粗细,
line-
HTML与CSS字体设计详解
基础字体设置
属性 | 说明 | 示例效果 |
font-family | 定义字体系列,优先使用指定字体,后续为备选方案 | body { font-family: Arial, sans-serif; } |
font-size | 设置字体大小(建议用CSS单位替代<font> | p { font-size: 16px; } |
font-weight | 字体粗细(数值400=正常,700=加粗) | h1 { font-weight: bold; } |
字体样式控制
属性 | 说明 | 示例 |
font-style | 斜体/正常体(italic/oblique/normal) | em { font-style: italic; } |
text-decoration | 文本装饰(下划线/删除线等) | a { text-decoration: none; } |
font-variant | 小型大写字母等特殊样式 | sup { font-variant: small-caps; } |
字体颜色与链接
属性 | 说明 | 示例 |
color | 文本颜色(支持多种格式) | .red-text { color: #FF0000; } |
RGB/RGBA | div { color: rgba(0,0,255,0.8); } |
HSL/HSLA | h2 { color: hsl(120,100%,50%); } |
a:link | 未访问链接样式 | a:link { color: blue; } |
a:visited | 已访问链接样式 | a:visited { color: purple; } |
自定义字体加载
方法 | 说明 | 示例代码 |
@font-face | 定义本地字体库 | css { src: url('fonts/custom.woff2') format('woff2'); } |
Google Fonts | 引用在线字体库 | <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> |
Font Display | 字体加载策略(交换/可选/强制) | body { font-display: swap; } |
响应式字体设计
技术 | 说明 | 示例 |
rem单位 | 根元素字体尺寸的倍数 | html { font-size: 16px; } h1 { font-size: 2rem; } |
vw/vh单位 | 视口宽度/高度的百分比 | .hero { font-size: 5vw; } |
媒体查询 | 根据设备特性调整字体 | css } |
字体性能优化
优化策略 | 说明 | 实现方式 |
字体子集 | 仅加载所需字符范围 | 使用unicode-range 声明 |
字体压缩 | 减少文件体积 | 优先使用woff2格式 |
懒加载 | 延迟非关键字体加载 | <link rel="preload" href="style.css" as="style"> |
常见问题与解答
Q1:如何让中文和英文数字使用不同字体?
A:通过font-feature-settings
或组合::before/after
伪元素实现:

body { } / 主字体 / span.number { font-family: 'Arial', sans-serif; } / 数字单独设置 / }
Q2:为什么设置了font-family
后字体没变化?
A:常见原因及解决方案:
- 字体名称拼写错误 → 检查浏览器识别的字体列表(
<select>
标签测试) - 字体未加载完成 → 添加
font-display: swap
或使用@font-face
预加载 - 系统缺少目标字体 → 提供通用备选方案(如`sans-serif