CSS基础-文本与字体
文本和字体是网页内容呈现的核心,掌握CSS文本与字体属性对于创建美观易读的网页至关重要。
本文将系统介绍CSS中的字体设置、文本样式、排版技巧以及Web字体的使用方法。
# 一、字体属性
# 1.1 font-family(字体族)
font-family 定义文本使用的字体,可以指定多个字体作为备选方案。
# 基本语法
/* 单个字体 */
body {
font-family: Arial;
}
/* 多个备选字体(推荐) */
body {
font-family: "Helvetica Neue", Arial, sans-serif;
}
/* 中文字体 */
body {
font-family: "Microsoft YaHei", "微软雅黑", SimSun, sans-serif;
}
/* 包含空格的字体名需要加引号 */
h1 {
font-family: "Times New Roman", serif;
}
# 字体分类
CSS定义了5个通用字体族,作为最后的备选方案:
/* 无衬线字体(现代、简洁) */
.sans {
font-family: Arial, Helvetica, sans-serif;
}
/* 衬线字体(传统、正式) */
.serif {
font-family: "Times New Roman", Georgia, serif;
}
/* 等宽字体(代码、技术文档) */
.mono {
font-family: "Courier New", Consolas, monospace;
}
/* 手写体 */
.cursive {
font-family: "Comic Sans MS", cursive;
}
/* 装饰字体 */
.fantasy {
font-family: Impact, fantasy;
}
# 常用字体栈
/* 系统字体栈(高性能) */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
Roboto, "Helvetica Neue", Arial, sans-serif;
}
/* 中英文混合 */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
"PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
"微软雅黑", Arial, sans-serif;
}
/* 代码字体 */
code {
font-family: "Fira Code", "Source Code Pro", Menlo, Monaco,
Consolas, "Courier New", monospace;
}
# 1.2 font-size(字体大小)
# 绝对单位
/* 像素(最常用) */
p {
font-size: 16px;
}
/* pt(磅,主要用于打印) */
p {
font-size: 12pt;
}
# 相对单位
/* em(相对于父元素字体大小) */
p {
font-size: 16px;
}
p span {
font-size: 1.5em; /* 16px × 1.5 = 24px */
}
/* rem(相对于根元素字体大小,推荐) */
html {
font-size: 16px;
}
p {
font-size: 1rem; /* 16px */
}
h1 {
font-size: 2rem; /* 32px */
}
/* 百分比 */
p {
font-size: 100%; /* 等于父元素字体大小 */
}
small {
font-size: 80%;
}
# 关键字
/* 绝对大小关键字 */
.xx-small { font-size: xx-small; }
.x-small { font-size: x-small; }
.small { font-size: small; }
.medium { font-size: medium; } /* 默认值 */
.large { font-size: large; }
.x-large { font-size: x-large; }
.xx-large { font-size: xx-large; }
/* 相对大小关键字 */
.larger { font-size: larger; }
.smaller { font-size: smaller; }
# 响应式字体
/* 使用 clamp() 实现流体字体 */
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
/* 最小1.5rem,理想5vw,最大3rem */
}
/* 媒体查询 */
body {
font-size: 14px;
}
@media (min-width: 768px) {
body {
font-size: 16px;
}
}
@media (min-width: 1200px) {
body {
font-size: 18px;
}
}
# 1.3 font-weight(字体粗细)
/* 关键字 */
.normal { font-weight: normal; } /* 400 */
.bold { font-weight: bold; } /* 700 */
.lighter { font-weight: lighter; } /* 比父元素更细 */
.bolder { font-weight: bolder; } /* 比父元素更粗 */
/* 数值(100-900,100为单位) */
.thin { font-weight: 100; }
.extra-light { font-weight: 200; }
.light { font-weight: 300; }
.regular { font-weight: 400; } /* 等同于 normal */
.medium { font-weight: 500; }
.semi-bold { font-weight: 600; }
.bold { font-weight: 700; } /* 等同于 bold */
.extra-bold { font-weight: 800; }
.black { font-weight: 900; }
/* 可变字体 */
.variable {
font-weight: 450; /* 精确控制 */
}
注意:字体文件必须支持对应的粗细,否则浏览器会模拟(效果可能不佳)。
# 1.4 font-style(字体风格)
/* 正常 */
.normal {
font-style: normal;
}
/* 斜体(使用字体的斜体版本) */
.italic {
font-style: italic;
}
/* 倾斜(浏览器模拟倾斜) */
.oblique {
font-style: oblique;
}
/* 倾斜角度(CSS3) */
.oblique-angle {
font-style: oblique 10deg;
}
# 1.5 font-variant(字体变体)
/* 正常 */
p {
font-variant: normal;
}
/* 小型大写字母 */
.small-caps {
font-variant: small-caps;
}
/* CSS3 扩展属性 */
.all-small-caps {
font-variant-caps: all-small-caps;
}
.numbers {
font-variant-numeric: tabular-nums; /* 等宽数字 */
}
# 1.6 font(简写属性)
font 简写属性可以同时设置多个字体属性。
# 语法规则
font: [font-style] [font-variant] [font-weight] font-size[/line-height] font-family;
必需值:font-size 和 font-family
可选值:font-style、font-variant、font-weight、line-height
/* 基础用法 */
p {
font: 16px Arial;
}
/* 完整语法 */
p {
font: italic small-caps bold 16px/1.5 "Helvetica Neue", Arial, sans-serif;
}
/* 等价于 */
p {
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 16px;
line-height: 1.5;
font-family: "Helvetica Neue", Arial, sans-serif;
}
/* 常用组合 */
body {
font: 400 16px/1.6 -apple-system, sans-serif;
}
h1 {
font: bold 2rem/1.2 Georgia, serif;
}
注意:使用 font 简写会重置所有字体相关属性为默认值,未指定的属性会被覆盖。
# 二、文本样式
# 2.1 color(文本颜色)
/* 关键字 */
p { color: red; }
/* 十六进制 */
p { color: #333333; }
p { color: #333; } /* 简写 */
/* RGB */
p { color: rgb(255, 0, 0); }
/* RGBA(带透明度) */
p { color: rgba(0, 0, 0, 0.8); }
/* HSL */
p { color: hsl(0, 100%, 50%); }
/* CSS变量 */
:root {
--text-primary: #212121;
--text-secondary: #757575;
}
p { color: var(--text-primary); }
# 2.2 text-align(文本对齐)
/* 左对齐(默认) */
.left {
text-align: left;
}
/* 右对齐 */
.right {
text-align: right;
}
/* 居中对齐 */
.center {
text-align: center;
}
/* 两端对齐 */
.justify {
text-align: justify;
}
/* 起始位置(LTR语言为左,RTL语言为右) */
.start {
text-align: start;
}
/* 结束位置 */
.end {
text-align: end;
}
# 2.3 text-decoration(文本装饰)
/* 无装饰(常用于移除链接下划线) */
a {
text-decoration: none;
}
/* 下划线 */
.underline {
text-decoration: underline;
}
/* 上划线 */
.overline {
text-decoration: overline;
}
/* 删除线 */
.line-through {
text-decoration: line-through;
}
/* CSS3 详细控制 */
.custom {
text-decoration-line: underline; /* 线条类型 */
text-decoration-color: red; /* 线条颜色 */
text-decoration-style: wavy; /* 线条样式:solid | double | dotted | dashed | wavy */
text-decoration-thickness: 2px; /* 线条粗细 */
}
/* 简写 */
.custom {
text-decoration: underline wavy red 2px;
}
/* 多种装饰 */
.multi {
text-decoration: underline overline;
}
# 2.4 text-transform(文本转换)
/* 无转换(默认) */
.none {
text-transform: none;
}
/* 全部大写 */
.uppercase {
text-transform: uppercase;
}
/* 全部小写 */
.lowercase {
text-transform: lowercase;
}
/* 首字母大写 */
.capitalize {
text-transform: capitalize;
}
# 2.5 text-indent(首行缩进)
/* 像素值 */
p {
text-indent: 32px; /* 缩进2个字符(16px字体) */
}
/* em(推荐) */
p {
text-indent: 2em; /* 缩进2个字符,自适应字体大小 */
}
/* 百分比(相对于父元素宽度) */
p {
text-indent: 10%;
}
/* 负值(悬挂缩进) */
.hanging {
padding-left: 2em;
text-indent: -2em;
}
# 2.6 text-shadow(文本阴影)
/* 基础语法:x偏移 y偏移 模糊半径 颜色 */
h1 {
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
/* 多重阴影 */
.multi-shadow {
text-shadow:
2px 2px 4px rgba(0, 0, 0, 0.3),
-2px -2px 4px rgba(255, 255, 255, 0.5);
}
/* 发光效果 */
.glow {
color: white;
text-shadow:
0 0 10px #fff,
0 0 20px #fff,
0 0 30px #ff00de,
0 0 40px #ff00de;
}
/* 立体效果 */
.3d {
text-shadow:
1px 1px 0 #ccc,
2px 2px 0 #bbb,
3px 3px 0 #aaa,
4px 4px 0 #999;
}
# 三、文本排版
# 3.1 line-height(行高)
行高是控制文本可读性的关键属性。
/* 数值(推荐,无单位,相对于字体大小) */
p {
font-size: 16px;
line-height: 1.6; /* 16px × 1.6 = 25.6px */
}
/* 像素值 */
p {
line-height: 24px;
}
/* 百分比 */
p {
line-height: 150%; /* 相对于字体大小 */
}
/* em */
p {
line-height: 1.5em;
}
/* normal(默认,约1.2) */
p {
line-height: normal;
}
最佳实践:
- 正文使用
1.5-1.8 - 标题使用
1.2-1.4 - 单行文本垂直居中:
line-height等于height
/* 垂直居中 */
.button {
height: 40px;
line-height: 40px;
text-align: center;
}
# 3.2 letter-spacing(字符间距)
/* 正常间距 */
p {
letter-spacing: normal;
}
/* 增加间距 */
.wide {
letter-spacing: 2px;
}
/* 紧凑间距 */
.tight {
letter-spacing: -1px;
}
/* em单位(推荐) */
.heading {
letter-spacing: 0.1em;
}
/* 标题常用技巧 */
h1 {
letter-spacing: 0.05em;
text-transform: uppercase;
}
# 3.3 word-spacing(单词间距)
/* 正常间距 */
p {
word-spacing: normal;
}
/* 增加单词间距 */
.wide {
word-spacing: 5px;
}
/* em单位 */
.space {
word-spacing: 0.5em;
}
# 3.4 white-space(空白处理)
控制如何处理元素内的空白字符和换行。
/* 正常(合并空白,自动换行) */
p {
white-space: normal;
}
/* 不换行 */
.nowrap {
white-space: nowrap;
}
/* 保留空白和换行 */
.pre {
white-space: pre;
}
/* 保留空白,正常换行 */
.pre-wrap {
white-space: pre-wrap;
}
/* 合并空白,保留换行 */
.pre-line {
white-space: pre-line;
}
/* 不换行,可滚动 */
.break-spaces {
white-space: break-spaces;
}
| 值 | 空白符 | 换行符 | 自动换行 |
|---|---|---|---|
normal | 合并 | 忽略 | 换行 |
nowrap | 合并 | 忽略 | 不换行 |
pre | 保留 | 保留 | 不换行 |
pre-wrap | 保留 | 保留 | 换行 |
pre-line | 合并 | 保留 | 换行 |
# 3.5 word-wrap / overflow-wrap(单词换行)
/* 正常换行(默认) */
p {
word-wrap: normal;
}
/* 强制换行(长单词) */
.break-word {
word-wrap: break-word;
/* 或使用标准属性 */
overflow-wrap: break-word;
}
/* anywhere(更激进的换行) */
.anywhere {
overflow-wrap: anywhere;
}
# 3.6 word-break(单词断行)
/* 正常断行规则 */
p {
word-break: normal;
}
/* 允许在任意字符间断行 */
.break-all {
word-break: break-all;
}
/* 保持单词完整(CJK文本) */
.keep-all {
word-break: keep-all;
}
/* 中文优化 */
.chinese {
word-break: break-all; /* 允许断行 */
word-wrap: break-word; /* 长单词换行 */
}
# 3.7 text-overflow(文本溢出)
处理溢出文本的显示方式,通常与 overflow 和 white-space 配合使用。
/* 单行文本溢出显示省略号 */
.ellipsis {
width: 200px;
white-space: nowrap; /* 不换行 */
overflow: hidden; /* 隐藏溢出 */
text-overflow: ellipsis; /* 显示省略号 */
}
/* 裁剪(不显示省略号) */
.clip {
overflow: hidden;
text-overflow: clip;
}
/* 多行文本溢出(-webkit-) */
.multiline-ellipsis {
display: -webkit-box;
-webkit-line-clamp: 3; /* 显示3行 */
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}
# 3.8 writing-mode(书写模式)
/* 水平方向,从左到右(默认) */
p {
writing-mode: horizontal-tb;
}
/* 垂直方向,从右到左(传统中文、日文) */
.vertical-rl {
writing-mode: vertical-rl;
}
/* 垂直方向,从左到右 */
.vertical-lr {
writing-mode: vertical-lr;
}
/* 侧边栏标题 */
.sidebar-title {
writing-mode: vertical-rl;
text-orientation: mixed;
}
# 四、Web字体
# 4.1 @font-face(自定义字体)
/* 基础语法 */
@font-face {
font-family: 'MyCustomFont';
src: url('fonts/my-font.woff2') format('woff2'),
url('fonts/my-font.woff') format('woff');
font-weight: normal;
font-style: normal;
}
/* 使用自定义字体 */
body {
font-family: 'MyCustomFont', Arial, sans-serif;
}
/* 完整示例 */
@font-face {
font-family: 'Open Sans';
src: url('fonts/OpenSans-Regular.woff2') format('woff2'),
url('fonts/OpenSans-Regular.woff') format('woff'),
url('fonts/OpenSans-Regular.ttf') format('truetype');
font-weight: 400;
font-style: normal;
font-display: swap; /* 字体加载策略 */
}
@font-face {
font-family: 'Open Sans';
src: url('fonts/OpenSans-Bold.woff2') format('woff2');
font-weight: 700;
font-style: normal;
font-display: swap;
}
/* 使用 */
body {
font-family: 'Open Sans', sans-serif;
}
h1 {
font-weight: 700; /* 会加载粗体字体文件 */
}
# 4.2 font-display(字体加载策略)
控制字体加载时的显示行为。
@font-face {
font-family: 'MyFont';
src: url('font.woff2') format('woff2');
font-display: swap; /* 推荐值 */
}
| 值 | 说明 | 使用场景 |
|---|---|---|
auto | 浏览器默认行为 | 默认 |
block | 短暂隐藏文本,然后交换字体 | 字体很重要 |
swap | 立即显示备用字体,然后交换 | 推荐,最佳性能 |
fallback | 极短隐藏期,交换期有限 | 平衡性能和体验 |
optional | 极短隐藏期,可选交换 | 性能优先 |
# 4.3 字体格式
@font-face {
font-family: 'MyFont';
src: url('font.woff2') format('woff2'), /* 现代浏览器,最优 */
url('font.woff') format('woff'), /* 兼容性好 */
url('font.ttf') format('truetype'), /* 旧浏览器 */
url('font.eot') format('embedded-opentype'); /* IE9 */
}
推荐策略:
- 仅使用
woff2+woff(覆盖99%+浏览器) - 优先加载
woff2(体积更小)
# 4.4 Google Fonts(外部字体服务)
<!-- HTML引入 -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
/* CSS引入 */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
/* 使用 */
body {
font-family: 'Roboto', sans-serif;
}
性能优化:
<!-- 预连接 -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- 预加载关键字体 -->
<link rel="preload" href="fonts/MyFont.woff2" as="font" type="font/woff2" crossorigin>
# 4.5 可变字体(Variable Fonts)
可变字体允许在单个字体文件中包含多个变体。
@font-face {
font-family: 'Inter';
src: url('Inter.var.woff2') format('woff2');
font-weight: 100 900; /* 支持100-900所有粗细 */
font-display: swap;
}
/* 使用任意粗细 */
h1 {
font-family: 'Inter', sans-serif;
font-weight: 650; /* 精确控制 */
}
/* 自定义轴 */
.custom {
font-variation-settings:
'wght' 450, /* 粗细 */
'wdth' 80, /* 宽度 */
'slnt' -10; /* 倾斜 */
}
# 五、实战案例
# 5.1 响应式字体大小
# 5.2 单行/多行文本溢出
# 5.3 文字特效
# 5.4 优雅的段落排版
# 六、最佳实践
# 6.1 字体选择原则
中文网站:
body {
font-family:
-apple-system, BlinkMacSystemFont,
"PingFang SC", "Hiragino Sans GB",
"Microsoft YaHei", "微软雅黑",
"Segoe UI", Roboto,
sans-serif;
}
英文网站:
body {
font-family:
-apple-system, BlinkMacSystemFont,
"Segoe UI", Roboto, "Helvetica Neue",
Arial, sans-serif;
}
代码显示:
code, pre {
font-family:
"Fira Code", "Source Code Pro",
Menlo, Monaco, Consolas,
"Courier New", monospace;
}
# 6.2 可读性优化
/* 正文 */
.content {
font-size: 16px; /* 基础字体大小 */
line-height: 1.6; /* 舒适的行高 */
max-width: 65ch; /* 控制每行字符数 */
color: #333; /* 不要纯黑 */
letter-spacing: 0.01em; /* 微调字符间距 */
}
/* 标题 */
h1, h2, h3 {
line-height: 1.2; /* 标题行高小一些 */
margin-top: 1.5em; /* 上下间距 */
margin-bottom: 0.5em;
font-weight: 600; /* 适中的粗细 */
}
/* 长篇阅读优化 */
.article {
font-size: 18px; /* 稍大的字号 */
line-height: 1.8; /* 更宽松的行距 */
word-spacing: 0.05em; /* 单词间距 */
}
# 6.3 性能优化
/* 1. 使用系统字体(最快) */
body {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}
/* 2. 字体子集化(减小文件大小) */
@font-face {
font-family: 'MyFont';
src: url('font-latin.woff2') format('woff2');
unicode-range: U+0000-00FF; /* 仅包含拉丁字符 */
}
/* 3. 预加载关键字体 */
/* 在HTML中: */
/* <link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin> */
/* 4. 使用font-display */
@font-face {
font-family: 'MyFont';
src: url('font.woff2') format('woff2');
font-display: swap; /* 避免FOIT(不可见文本闪烁) */
}
/* 5. 避免过多字体变体 */
/* ❌ 不推荐:加载太多粗细 */
/* font-weight: 100, 200, 300, 400, 500, 600, 700, 800, 900 */
/* ✅ 推荐:只加载需要的 */
/* font-weight: 400, 700 */
# 6.4 无障碍性
/* 1. 保证足够的对比度(WCAG AA: 4.5:1) */
.text {
color: #333; /* 深灰色 */
background: #fff; /* 白色背景 */
}
/* 2. 可调整文本大小 */
html {
font-size: 100%; /* 尊重用户浏览器设置 */
}
body {
font-size: 1rem; /* 使用相对单位 */
}
/* 3. 避免纯大写 */
/* ❌ 不推荐 */
.all-caps {
text-transform: uppercase;
}
/* ✅ 推荐:仅装饰性使用 */
.decorative {
text-transform: uppercase;
letter-spacing: 0.1em; /* 增加可读性 */
}
/* 4. 充足的行高 */
p {
line-height: 1.5; /* 最小1.5 */
}
/* 5. 适当的段落间距 */
p + p {
margin-top: 1em;
}
# 6.5 常见错误
❌ 错误做法:
/* 1. 使用绝对单位 */
body {
font-size: 16px; /* 无法响应用户浏览器设置 */
}
/* 2. 行高使用单位 */
p {
font-size: 16px;
line-height: 24px; /* 子元素继承24px,而非1.5倍 */
}
/* 3. 过小的字体 */
.small {
font-size: 10px; /* 难以阅读 */
}
/* 4. 过度使用!important */
p {
color: red !important; /* 难以覆盖 */
}
/* 5. 忽略回退字体 */
body {
font-family: 'MyCustomFont'; /* 字体加载失败时无备选 */
}
✅ 正确做法:
/* 1. 使用相对单位 */
html {
font-size: 100%; /* 16px */
}
body {
font-size: 1rem; /* 响应用户设置 */
}
/* 2. 行高使用无单位数值 */
p {
font-size: 1rem;
line-height: 1.5; /* 子元素继承比例 */
}
/* 3. 最小12px */
.small {
font-size: 0.875rem; /* 14px,仍可读 */
}
/* 4. 提高选择器优先级 */
.container p {
color: red; /* 无需!important */
}
/* 5. 始终提供回退字体 */
body {
font-family: 'MyCustomFont', -apple-system, sans-serif;
}
# 七、浏览器兼容性
# 7.1 现代特性兼容性
| 特性 | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
font-family | ✅ 全部 | ✅ 全部 | ✅ 全部 | ✅ 全部 |
@font-face | ✅ 4+ | ✅ 3.5+ | ✅ 3.1+ | ✅ 9+ |
font-display | ✅ 60+ | ✅ 58+ | ✅ 11.1+ | ✅ 79+ |
| 可变字体 | ✅ 62+ | ✅ 62+ | ✅ 11+ | ✅ 79+ |
text-overflow | ✅ 4+ | ✅ 7+ | ✅ 3.1+ | ✅ 12+ |
-webkit-line-clamp | ✅ 6+ | ✅ 68+ | ✅ 5+ | ✅ 79+ |
clamp() | ✅ 79+ | ✅ 75+ | ✅ 13.1+ | ✅ 79+ |
# 7.2 前缀处理
/* 文本填充颜色(渐变文字) */
.gradient-text {
background: linear-gradient(45deg, red, blue);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
/* 多行溢出 */
.multiline {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* 文字描边 */
.stroke {
-webkit-text-stroke: 1px #000;
text-stroke: 1px #000;
}
# 八、总结
# 核心要点
字体属性:
font-family:优先使用系统字体,提供回退方案font-size:使用rem实现响应式font-weight:注意字体文件支持font:简写属性方便但会重置其他值
文本样式:
color:注意对比度,保证可读性text-align:合理使用对齐方式text-decoration:常用于链接样式text-shadow:可实现丰富的视觉效果
文本排版:
line-height:无单位值,1.5-1.8最佳letter-spacing:微调提升视觉效果white-space:控制空白和换行text-overflow:处理溢出文本
Web字体:
- 优先使用
woff2格式 - 使用
font-display: swap提升性能 - 考虑字体加载对性能的影响
- 可变字体可减少文件数量
- 优先使用
最佳实践:
- 保证可读性(字号、行高、对比度)
- 优化性能(系统字体、字体子集、预加载)
- 注意无障碍性(对比度、可缩放)
- 提供合理的字体栈
掌握文本与字体的CSS属性,能够让你的网页内容更加美观、易读,提升用户体验。
祝你变得更强!
编辑 (opens new window)
上次更新: 2025/11/21