轩辕李的博客 轩辕李的博客
首页
  • Java
  • Spring
  • 其他语言
  • 工具
  • JavaScript
  • TypeScript
  • Node.js
  • Vue.js
  • 前端工程化
  • 浏览器与Web API
  • 分布式
  • 代码质量管理
  • 基础
  • 操作系统
  • 计算机网络
  • 编程范式
  • 安全
  • 中间件
  • 心得
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

轩辕李

勇猛精进,星辰大海
首页
  • Java
  • Spring
  • 其他语言
  • 工具
  • JavaScript
  • TypeScript
  • Node.js
  • Vue.js
  • 前端工程化
  • 浏览器与Web API
  • 分布式
  • 代码质量管理
  • 基础
  • 操作系统
  • 计算机网络
  • 编程范式
  • 安全
  • 中间件
  • 心得
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • JavaScript

  • TypeScript

  • Node.js

  • Vue.js

  • 工程化

  • 浏览器与Web API

    • HTML基础-语义化标签与文档结构
    • HTML基础-文本与排版标签
    • HTML基础-列表与表格
    • HTML表单-Input类型详解
    • HTML表单-表单元素与验证
    • HTML交互-多媒体元素
    • HTML工程化-模板与组件化
    • HTML工程化-性能优化
    • CSS基础-选择器与优先级
    • CSS基础-盒模型与布局基础
    • CSS基础-单位与颜色系统
    • CSS基础-文本与字体
      • 一、字体属性
        • 1.1 font-family(字体族)
        • 基本语法
        • 字体分类
        • 常用字体栈
        • 1.2 font-size(字体大小)
        • 绝对单位
        • 相对单位
        • 关键字
        • 响应式字体
        • 1.3 font-weight(字体粗细)
        • 1.4 font-style(字体风格)
        • 1.5 font-variant(字体变体)
        • 1.6 font(简写属性)
        • 语法规则
      • 二、文本样式
        • 2.1 color(文本颜色)
        • 2.2 text-align(文本对齐)
        • 2.3 text-decoration(文本装饰)
        • 2.4 text-transform(文本转换)
        • 2.5 text-indent(首行缩进)
        • 2.6 text-shadow(文本阴影)
      • 三、文本排版
        • 3.1 line-height(行高)
        • 3.2 letter-spacing(字符间距)
        • 3.3 word-spacing(单词间距)
        • 3.4 white-space(空白处理)
        • 3.5 word-wrap / overflow-wrap(单词换行)
        • 3.6 word-break(单词断行)
        • 3.7 text-overflow(文本溢出)
        • 3.8 writing-mode(书写模式)
      • 四、Web字体
        • 4.1 @font-face(自定义字体)
        • 4.2 font-display(字体加载策略)
        • 4.3 字体格式
        • 4.4 Google Fonts(外部字体服务)
        • 4.5 可变字体(Variable Fonts)
      • 五、实战案例
        • 5.1 响应式字体大小
        • 5.2 单行/多行文本溢出
        • 5.3 文字特效
        • 5.4 优雅的段落排版
      • 六、最佳实践
        • 6.1 字体选择原则
        • 6.2 可读性优化
        • 6.3 性能优化
        • 6.4 无障碍性
        • 6.5 常见错误
      • 七、浏览器兼容性
        • 7.1 现代特性兼容性
        • 7.2 前缀处理
      • 八、总结
        • 核心要点
    • CSS基础-背景、列表与表格样式
    • CSS布局-Flexbox完全指南
    • CSS布局-Grid网格布局
    • CSS布局-响应式设计实践
    • CSS进阶-动画与过渡
    • CSS进阶-渐变与阴影效果
    • CSS进阶-Transform与3D变换
    • CSS进阶-滤镜与混合模式
    • 现代CSS-CSS预处理器对比
    • 现代CSS-CSS-in-JS方案
    • 现代CSS-原子化CSS与Tailwind
    • CSS工程化-架构与规范
    • CSS工程化-性能优化
    • CSS工程化-PostCSS实战指南
    • Web API基础-DOM操作完全指南
    • Web API基础-事件处理与委托
    • Web API基础-BOM与浏览器环境
    • Web API存储-客户端存储方案
    • Web API网络-HTTP请求详解
    • Web API网络-实时通信方案
    • Web API交互-用户体验增强
    • HTML&CSS历代版本新特性
  • 前端
  • 浏览器与Web API
轩辕李
2019-03-08
目录

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;
}
<html>
  <div class="font-family-demo-6yhsrq">
    <div class="font-box-6yhsrq sans-6yhsrq">
      <div class="label-6yhsrq">Sans-serif</div>
      <div class="sample-6yhsrq">AaBbCc 字体示例 1234</div>
    </div>
    <div class="font-box-6yhsrq serif-6yhsrq">
      <div class="label-6yhsrq">Serif</div>
      <div class="sample-6yhsrq">AaBbCc 字体示例 1234</div>
    </div>
    <div class="font-box-6yhsrq mono-6yhsrq">
      <div class="label-6yhsrq">Monospace</div>
      <div class="sample-6yhsrq">AaBbCc 字体示例 1234</div>
    </div>
    <div class="font-box-6yhsrq cursive-6yhsrq">
      <div class="label-6yhsrq">Cursive</div>
      <div class="sample-6yhsrq">AaBbCc 字体示例 1234</div>
    </div>
  </div>
</html>
<style>
.font-family-demo-6yhsrq {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}
.font-box-6yhsrq {
  padding: 15px;
  background: white;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
}
.label-6yhsrq {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
  font-weight: 600;
}
.sample-6yhsrq {
  font-size: 18px;
  color: #333;
}
.sans-6yhsrq .sample-6yhsrq {
  font-family: Arial, Helvetica, sans-serif;
}
.serif-6yhsrq .sample-6yhsrq {
  font-family: "Times New Roman", Georgia, serif;
}
.mono-6yhsrq .sample-6yhsrq {
  font-family: "Courier New", Consolas, monospace;
}
.cursive-6yhsrq .sample-6yhsrq {
  font-family: "Comic Sans MS", cursive;
}
</style>

# 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;
  }
}
<html>
  <div class="font-size-demo-6yhsrq">
    <div class="size-box-6yhsrq">
      <div class="px-14-6yhsrq">14px - 小字号</div>
    </div>
    <div class="size-box-6yhsrq">
      <div class="px-16-6yhsrq">16px - 正常字号</div>
    </div>
    <div class="size-box-6yhsrq">
      <div class="px-18-6yhsrq">18px - 大字号</div>
    </div>
    <div class="size-box-6yhsrq">
      <div class="rem-1-6yhsrq">1rem - 相对单位</div>
    </div>
    <div class="size-box-6yhsrq">
      <div class="rem-1-5-6yhsrq">1.5rem - 相对单位</div>
    </div>
    <div class="size-box-6yhsrq">
      <div class="clamp-6yhsrq">clamp() - 流体字体(调整窗口试试)</div>
    </div>
  </div>
</html>
<style>
.font-size-demo-6yhsrq {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}
.size-box-6yhsrq {
  padding: 15px;
  background: #f5f5f5;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 80px;
}
.px-14-6yhsrq { font-size: 14px; }
.px-16-6yhsrq { font-size: 16px; }
.px-18-6yhsrq { font-size: 18px; }
.rem-1-6yhsrq { font-size: 1rem; }
.rem-1-5-6yhsrq { font-size: 1.5rem; }
.clamp-6yhsrq { 
  font-size: clamp(0.875rem, 2vw, 1.25rem);
  text-align: center;
}
</style>

# 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; /* 精确控制 */
}
<html>
  <div class="font-weight-demo-6yhsrq">
    <div class="weight-item-6yhsrq w-300-6yhsrq">300 - Light</div>
    <div class="weight-item-6yhsrq w-400-6yhsrq">400 - Normal</div>
    <div class="weight-item-6yhsrq w-500-6yhsrq">500 - Medium</div>
    <div class="weight-item-6yhsrq w-600-6yhsrq">600 - Semi-Bold</div>
    <div class="weight-item-6yhsrq w-700-6yhsrq">700 - Bold</div>
    <div class="weight-item-6yhsrq w-900-6yhsrq">900 - Black</div>
  </div>
</html>
<style>
.font-weight-demo-6yhsrq {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}
.weight-item-6yhsrq {
  padding: 12px;
  background: white;
  border: 1px solid #ddd;
  border-radius: 6px;
  text-align: center;
  font-size: 16px;
}
.w-300-6yhsrq { font-weight: 300; }
.w-400-6yhsrq { font-weight: 400; }
.w-500-6yhsrq { font-weight: 500; }
.w-600-6yhsrq { font-weight: 600; }
.w-700-6yhsrq { font-weight: 700; }
.w-900-6yhsrq { font-weight: 900; }
</style>

注意:字体文件必须支持对应的粗细,否则浏览器会模拟(效果可能不佳)。

# 1.4 font-style(字体风格)

/* 正常 */
.normal {
  font-style: normal;
}

/* 斜体(使用字体的斜体版本) */
.italic {
  font-style: italic;
}

/* 倾斜(浏览器模拟倾斜) */
.oblique {
  font-style: oblique;
}

/* 倾斜角度(CSS3) */
.oblique-angle {
  font-style: oblique 10deg;
}
<html>
  <div class="font-style-demo-6yhsrq">
    <div class="style-box-6yhsrq normal-6yhsrq">
      <div class="label-6yhsrq">Normal</div>
      <div class="text-6yhsrq">The quick brown fox jumps</div>
    </div>
    <div class="style-box-6yhsrq italic-6yhsrq">
      <div class="label-6yhsrq">Italic</div>
      <div class="text-6yhsrq">The quick brown fox jumps</div>
    </div>
    <div class="style-box-6yhsrq oblique-6yhsrq">
      <div class="label-6yhsrq">Oblique</div>
      <div class="text-6yhsrq">The quick brown fox jumps</div>
    </div>
  </div>
</html>
<style>
.font-style-demo-6yhsrq {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}
.style-box-6yhsrq {
  padding: 15px;
  background: #f9f9f9;
  border-radius: 8px;
}
.style-box-6yhsrq .label-6yhsrq {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
  font-weight: 600;
}
.style-box-6yhsrq .text-6yhsrq {
  font-size: 16px;
  color: #333;
}
.normal-6yhsrq .text-6yhsrq { font-style: normal; }
.italic-6yhsrq .text-6yhsrq { font-style: italic; }
.oblique-6yhsrq .text-6yhsrq { font-style: oblique; }
</style>

# 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; /* 等宽数字 */
}
<html>
  <div class="font-variant-demo-k9m2x5">
    <div class="variant-box-k9m2x5">
      <div class="label-k9m2x5">normal(正常)</div>
      <div class="content-k9m2x5 normal-k9m2x5">Hello World 123</div>
    </div>
    <div class="variant-box-k9m2x5">
      <div class="label-k9m2x5">small-caps(小型大写字母)</div>
      <div class="content-k9m2x5 small-caps-k9m2x5">Hello World 123</div>
    </div>
  </div>
</html>
<style>
.font-variant-demo-k9m2x5 {
  display: grid;
  gap: 15px;
}
.variant-box-k9m2x5 {
  background: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
}
.label-k9m2x5 {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}
.content-k9m2x5 {
  font-size: 16px;
  padding: 10px;
  background: white;
  border-radius: 4px;
}
.normal-k9m2x5 {
  font-variant: normal;
}
.small-caps-k9m2x5 {
  font-variant: small-caps;
}
</style>

# 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;
}
<html>
  <div class="text-align-demo-6yhsrq">
    <div class="align-box-6yhsrq left-6yhsrq">左对齐文本</div>
    <div class="align-box-6yhsrq center-6yhsrq">居中对齐文本</div>
    <div class="align-box-6yhsrq right-6yhsrq">右对齐文本</div>
    <div class="align-box-6yhsrq justify-6yhsrq">
      两端对齐文本是排版中常用的一种方式,它能让每一行的左右两端都对齐,看起来更加整齐。
    </div>
  </div>
</html>
<style>
.text-align-demo-6yhsrq {
  display: grid;
  gap: 15px;
}
.align-box-6yhsrq {
  padding: 15px;
  background: #f5f5f5;
  border-radius: 8px;
  border-left: 3px solid #4CAF50;
}
.left-6yhsrq { text-align: left; }
.center-6yhsrq { text-align: center; }
.right-6yhsrq { text-align: right; }
.justify-6yhsrq { text-align: justify; }
</style>

# 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;
}
<html>
  <div class="text-decoration-demo-6yhsrq">
    <div class="deco-item-6yhsrq none-6yhsrq">无装饰文本</div>
    <div class="deco-item-6yhsrq underline-6yhsrq">下划线文本</div>
    <div class="deco-item-6yhsrq overline-6yhsrq">上划线文本</div>
    <div class="deco-item-6yhsrq line-through-6yhsrq">删除线文本</div>
    <div class="deco-item-6yhsrq wavy-6yhsrq">波浪线文本</div>
    <div class="deco-item-6yhsrq double-6yhsrq">双下划线文本</div>
  </div>
</html>
<style>
.text-decoration-demo-6yhsrq {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}
.deco-item-6yhsrq {
  padding: 12px;
  background: white;
  border-radius: 6px;
  text-align: center;
  font-size: 16px;
}
.none-6yhsrq { text-decoration: none; }
.underline-6yhsrq { text-decoration: underline; }
.overline-6yhsrq { text-decoration: overline; }
.line-through-6yhsrq { text-decoration: line-through; }
.wavy-6yhsrq { text-decoration: underline wavy red; }
.double-6yhsrq { text-decoration: underline double blue; }
</style>

# 2.4 text-transform(文本转换)

/* 无转换(默认) */
.none {
  text-transform: none;
}

/* 全部大写 */
.uppercase {
  text-transform: uppercase;
}

/* 全部小写 */
.lowercase {
  text-transform: lowercase;
}

/* 首字母大写 */
.capitalize {
  text-transform: capitalize;
}
<html>
  <div class="text-transform-demo-6yhsrq">
    <div class="transform-box-6yhsrq none-6yhsrq">
      <div class="label-6yhsrq">None</div>
      <div>Hello World 123</div>
    </div>
    <div class="transform-box-6yhsrq uppercase-6yhsrq">
      <div class="label-6yhsrq">Uppercase</div>
      <div>Hello World 123</div>
    </div>
    <div class="transform-box-6yhsrq lowercase-6yhsrq">
      <div class="label-6yhsrq">Lowercase</div>
      <div>Hello World 123</div>
    </div>
    <div class="transform-box-6yhsrq capitalize-6yhsrq">
      <div class="label-6yhsrq">Capitalize</div>
      <div>hello world 123</div>
    </div>
  </div>
</html>
<style>
.text-transform-demo-6yhsrq {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}
.transform-box-6yhsrq {
  padding: 12px;
  background: #f5f5f5;
  border-radius: 8px;
  text-align: center;
}
.label-6yhsrq {
  font-size: 12px;
  color: #666;
  margin-bottom: 6px;
  font-weight: 600;
}
.none-6yhsrq > div:last-child { text-transform: none; }
.uppercase-6yhsrq > div:last-child { text-transform: uppercase; }
.lowercase-6yhsrq > div:last-child { text-transform: lowercase; }
.capitalize-6yhsrq > div:last-child { text-transform: capitalize; }
</style>

# 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;
}
<html>
  <div class="text-indent-demo-6yhsrq">
    <div class="indent-box-6yhsrq">
      <div class="label-6yhsrq">正常缩进(2em)</div>
      <p class="indent-normal-6yhsrq">
        这是一段有首行缩进的文本。在中文排版中,段落首行缩进2个字符是常见的格式。这样可以让段落之间的分割更加清晰。
      </p>
    </div>
    <div class="indent-box-6yhsrq">
      <div class="label-6yhsrq">悬挂缩进(-2em)</div>
      <p class="indent-hanging-6yhsrq">
        这是一段使用悬挂缩进的文本。首行会向左突出,其余行均缩进。这种样式常用于列表项或引用文字。
      </p>
    </div>
  </div>
</html>
<style>
.text-indent-demo-6yhsrq {
  display: grid;
  gap: 20px;
}
.indent-box-6yhsrq {
  background: #f9f9f9;
  border-radius: 8px;
  padding: 15px;
}
.label-6yhsrq {
  font-size: 12px;
  color: #666;
  margin-bottom: 10px;
  font-weight: 600;
}
.indent-normal-6yhsrq {
  text-indent: 2em;
  line-height: 1.8;
  margin: 0;
}
.indent-hanging-6yhsrq {
  padding-left: 2em;
  text-indent: -2em;
  line-height: 1.8;
  margin: 0;
}
</style>

# 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;
}
<html>
  <div class="text-shadow-demo-6yhsrq">
    <div class="shadow-item-6yhsrq basic-6yhsrq">基础阴影</div>
    <div class="shadow-item-6yhsrq glow-6yhsrq">发光效果</div>
    <div class="shadow-item-6yhsrq d3-6yhsrq">3D效果</div>
  </div>
</html>
<style>
.text-shadow-demo-6yhsrq {
  background: #2c3e50;
  padding: 40px 20px;
  border-radius: 8px;
  display: grid;
  gap: 30px;
  text-align: center;
}
.shadow-item-6yhsrq {
  font-size: 36px;
  font-weight: bold;
}
.basic-6yhsrq {
  color: white;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
}
.glow-6yhsrq {
  color: white;
  text-shadow: 
    0 0 10px #fff,
    0 0 20px #fff,
    0 0 30px #00ffff,
    0 0 40px #00ffff;
}
.d3-6yhsrq {
  color: #ff6b6b;
  text-shadow: 
    1px 1px 0 #e55,
    2px 2px 0 #d44,
    3px 3px 0 #c33,
    4px 4px 0 #b22,
    5px 5px 10px rgba(0, 0, 0, 0.5);
}
</style>

# 三、文本排版

# 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;
}
<html>
  <div class="line-height-demo-6yhsrq">
    <div class="lh-box-6yhsrq">
      <div class="label-6yhsrq">line-height: 1.2 (紧凑)</div>
      <p class="lh-12-6yhsrq">
        这是行高为1.2的文本。行与行之间的间距较小,适合标题。在正文中使用可能会显得拥挤。
      </p>
    </div>
    <div class="lh-box-6yhsrq">
      <div class="label-6yhsrq">line-height: 1.6 (适中)</div>
      <p class="lh-16-6yhsrq">
        这是行高为1.6的文本。这是正文最常用的行高值,能够提供良好的阅读体验。
      </p>
    </div>
    <div class="lh-box-6yhsrq">
      <div class="label-6yhsrq">line-height: 2.0 (宽松)</div>
      <p class="lh-20-6yhsrq">
        这是行高为2.0的文本。行与行之间的间距较大,适合需要空间感的设计。
      </p>
    </div>
  </div>
</html>
<style>
.line-height-demo-6yhsrq {
  display: grid;
  gap: 20px;
}
.lh-box-6yhsrq {
  background: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
}
.label-6yhsrq {
  font-size: 12px;
  color: #666;
  margin-bottom: 10px;
  font-weight: 600;
}
.lh-box-6yhsrq p {
  margin: 0;
  color: #333;
}
.lh-12-6yhsrq { line-height: 1.2; }
.lh-16-6yhsrq { line-height: 1.6; }
.lh-20-6yhsrq { line-height: 2.0; }
</style>

# 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;
}
<html>
  <div class="letter-spacing-demo-6yhsrq">
    <div class="ls-box-6yhsrq normal-6yhsrq">Normal Spacing</div>
    <div class="ls-box-6yhsrq tight-6yhsrq">Tight Spacing</div>
    <div class="ls-box-6yhsrq wide-6yhsrq">Wide Spacing</div>
    <div class="ls-box-6yhsrq extra-wide-6yhsrq">EXTRA WIDE</div>
  </div>
</html>
<style>
.letter-spacing-demo-6yhsrq {
  display: grid;
  gap: 15px;
}
.ls-box-6yhsrq {
  padding: 15px;
  background: #f5f5f5;
  border-radius: 8px;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
}
.normal-6yhsrq { letter-spacing: normal; }
.tight-6yhsrq { letter-spacing: -1px; }
.wide-6yhsrq { letter-spacing: 2px; }
.extra-wide-6yhsrq { 
  letter-spacing: 0.2em;
  text-transform: uppercase;
}
</style>

# 3.3 word-spacing(单词间距)

/* 正常间距 */
p {
  word-spacing: normal;
}

/* 增加单词间距 */
.wide {
  word-spacing: 5px;
}

/* em单位 */
.space {
  word-spacing: 0.5em;
}
<html>
  <div class="word-spacing-demo-p7t4w8">
    <div class="spacing-box-p7t4w8">
      <div class="label-p7t4w8">normal(正常间距)</div>
      <div class="content-p7t4w8 normal-p7t4w8">Hello World CSS Demo</div>
    </div>
    <div class="spacing-box-p7t4w8">
      <div class="label-p7t4w8">5px(增加间距)</div>
      <div class="content-p7t4w8 wide-p7t4w8">Hello World CSS Demo</div>
    </div>
    <div class="spacing-box-p7t4w8">
      <div class="label-p7t4w8">10px(更大间距)</div>
      <div class="content-p7t4w8 wider-p7t4w8">Hello World CSS Demo</div>
    </div>
    <div class="spacing-box-p7t4w8">
      <div class="label-p7t4w8">-2px(负值间距)</div>
      <div class="content-p7t4w8 negative-p7t4w8">Hello World CSS Demo</div>
    </div>
  </div>
</html>
<style>
.word-spacing-demo-p7t4w8 {
  display: grid;
  gap: 15px;
}
.spacing-box-p7t4w8 {
  background: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
}
.label-p7t4w8 {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}
.content-p7t4w8 {
  font-size: 16px;
  padding: 10px;
  background: white;
  border-radius: 4px;
}
.normal-p7t4w8 {
  word-spacing: normal;
}
.wide-p7t4w8 {
  word-spacing: 5px;
}
.wider-p7t4w8 {
  word-spacing: 10px;
}
.negative-p7t4w8 {
  word-spacing: -2px;
}
</style>

# 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 合并 保留 换行
<html>
  <div class="white-space-demo-6yhsrq">
    <div class="ws-box-6yhsrq">
      <div class="label-6yhsrq">normal(默认)</div>
      <div class="content-6yhsrq normal-6yhsrq">This   is    a   text with    multiple    spaces   and
newlines.</div>
    </div>
    <div class="ws-box-6yhsrq">
      <div class="label-6yhsrq">nowrap</div>
      <div class="content-6yhsrq nowrap-6yhsrq">This is a very long text that will not wrap to the next line no matter how long it gets.</div>
    </div>
    <div class="ws-box-6yhsrq">
      <div class="label-6yhsrq">pre</div>
      <div class="content-6yhsrq pre-6yhsrq">This   is    a   text with    multiple    spaces   and
newlines preserved.</div>
    </div>
    <div class="ws-box-6yhsrq">
      <div class="label-6yhsrq">pre-wrap</div>
      <div class="content-6yhsrq pre-wrap-6yhsrq">This   is    a   text with    multiple    spaces   preserved and it will wrap when needed.</div>
    </div>
  </div>
</html>
<style>
.white-space-demo-6yhsrq {
  display: grid;
  gap: 15px;
}
.ws-box-6yhsrq {
  background: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
}
.label-6yhsrq {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
  font-weight: 600;
}
.content-6yhsrq {
  background: white;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}
.normal-6yhsrq { white-space: normal; }
.nowrap-6yhsrq { white-space: nowrap; }
.pre-6yhsrq { white-space: pre; }
.pre-wrap-6yhsrq { white-space: pre-wrap; }
</style>

# 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;
}
<html>
  <div class="word-wrap-demo-j3n8q6">
    <div class="wrap-box-j3n8q6">
      <div class="label-j3n8q6">normal(正常换行)</div>
      <div class="content-j3n8q6 normal-j3n8q6">This is a verylongwordthatwillnotbreakbydefault and will overflow the container.</div>
    </div>
    <div class="wrap-box-j3n8q6">
      <div class="label-j3n8q6">break-word(强制换行)</div>
      <div class="content-j3n8q6 break-word-j3n8q6">This is a verylongwordthatwillbreakwhenneeded and will not overflow the container.</div>
    </div>
    <div class="wrap-box-j3n8q6">
      <div class="label-j3n8q6">anywhere(任意位置换行)</div>
      <div class="content-j3n8q6 anywhere-j3n8q6">This is a verylongwordthatcanbreakanywhereinthemiddle of the word.</div>
    </div>
  </div>
</html>
<style>
.word-wrap-demo-j3n8q6 {
  display: grid;
  gap: 15px;
}
.wrap-box-j3n8q6 {
  background: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
}
.label-j3n8q6 {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}
.content-j3n8q6 {
  font-size: 14px;
  padding: 10px;
  background: white;
  border-radius: 4px;
  width: 250px;
  border: 1px solid #e0e0e0;
}
.normal-j3n8q6 {
  overflow-wrap: normal;
}
.break-word-j3n8q6 {
  overflow-wrap: break-word;
}
.anywhere-j3n8q6 {
  overflow-wrap: anywhere;
}
</style>

# 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;  /* 长单词换行 */
}
<html>
  <div class="word-break-demo-r5v2h9">
    <div class="break-box-r5v2h9">
      <div class="label-r5v2h9">normal(正常断行)</div>
      <div class="content-r5v2h9 normal-r5v2h9">This is a verylongwordthatwillnotbreak at character boundaries.</div>
    </div>
    <div class="break-box-r5v2h9">
      <div class="label-r5v2h9">break-all(任意字符断行)</div>
      <div class="content-r5v2h9 break-all-r5v2h9">This is a verylongwordthatwillbreak at any character position.</div>
    </div>
    <div class="break-box-r5v2h9">
      <div class="label-r5v2h9">keep-all(保持单词完整)</div>
      <div class="content-r5v2h9 keep-all-r5v2h9">这是一段中文文本,将不会在中文字符之间断行,保持单词完整性。</div>
    </div>
  </div>
</html>
<style>
.word-break-demo-r5v2h9 {
  display: grid;
  gap: 15px;
}
.break-box-r5v2h9 {
  background: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
}
.label-r5v2h9 {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}
.content-r5v2h9 {
  font-size: 14px;
  padding: 10px;
  background: white;
  border-radius: 4px;
  width: 250px;
  border: 1px solid #e0e0e0;
}
.normal-r5v2h9 {
  word-break: normal;
}
.break-all-r5v2h9 {
  word-break: break-all;
}
.keep-all-r5v2h9 {
  word-break: keep-all;
}
</style>

# 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;
}
<html>
  <div class="text-overflow-demo-w7k3m9">
    <div class="box-w7k3m9">
      <div class="label-w7k3m9">ellipsis(省略号)</div>
      <div class="text-w7k3m9 ellipsis-w7k3m9">这是一段很长的文本内容,用来演示text-overflow属性的ellipsis值效果</div>
    </div>
    <div class="box-w7k3m9">
      <div class="label-w7k3m9">clip(裁剪)</div>
      <div class="text-w7k3m9 clip-w7k3m9">这是一段很长的文本内容,用来演示text-overflow属性的clip值效果</div>
    </div>
    <div class="box-w7k3m9 multi-w7k3m9">
      <div class="label-w7k3m9">多行省略(3行)</div>
      <div class="text-w7k3m9 multiline-w7k3m9">这是一段很长的文本内容,用来演示多行文本溢出的效果。通过使用-webkit-line-clamp属性,我们可以限制显示的行数,超出的部分会显示省略号。这种效果在移动端和卡片布局中非常常见。这是一段很长的文本内容,用来演示多行文本溢出的效果。通过使用-webkit-line-clamp属性,我们可以限制显示的行数,超出的部分会显示省略号。这种效果在移动端和卡片布局中非常常见。</div>
    </div>
    <div class="box-w7k3m9 multi-w7k3m9">
      <div class="label-w7k3m9">多行省略(2行)</div>
      <div class="text-w7k3m9 multiline2-w7k3m9">这是一段很长的文本内容,用来演示两行文本溢出的效果。超出两行的部分会被隐藏并显示省略号。这是一段很长的文本内容,用来演示两行文本溢出的效果。超出两行的部分会被隐藏并显示省略号。</div>
    </div>
  </div>
</html>
<style>
.text-overflow-demo-w7k3m9 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.box-w7k3m9 {
  padding: 15px;
  background: white;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
}
.box-w7k3m9.multi-w7k3m9 {
  grid-column: span 1;
}
.label-w7k3m9 {
  font-size: 12px;
  color: #667eea;
  margin-bottom: 8px;
  font-weight: 600;
}
.text-w7k3m9 {
  font-size: 14px;
  color: #333;
  line-height: 1.6;
}
.ellipsis-w7k3m9 {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.clip-w7k3m9 {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}
.multiline-w7k3m9 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
.multiline2-w7k3m9 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>

# 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;
}
<html>
  <div class="writing-mode-demo-m8k1d4">
    <div class="mode-box-m8k1d4">
      <div class="label-m8k1d4">horizontal-tb(水平,从上到下)</div>
      <div class="content-m8k1d4 horizontal-m8k1d4">这是水平书写模式的文本</div>
    </div>
    <div class="mode-box-m8k1d4">
      <div class="label-m8k1d4">vertical-rl(垂直,从右到左)</div>
      <div class="content-m8k1d4 vertical-rl-m8k1d4">这是垂直书写模式</div>
    </div>
    <div class="mode-box-m8k1d4">
      <div class="label-m8k1d4">vertical-lr(垂直,从左到右)</div>
      <div class="content-m8k1d4 vertical-lr-m8k1d4">这是垂直书写模式</div>
    </div>
  </div>
</html>
<style>
.writing-mode-demo-m8k1d4 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}
.mode-box-m8k1d4 {
  background: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.label-m8k1d4 {
  font-size: 12px;
  color: #666;
  margin-bottom: 15px;
  text-align: center;
  writing-mode: horizontal-tb;
}
.content-m8k1d4 {
  font-size: 14px;
  padding: 15px;
  background: white;
  border-radius: 4px;
  border: 1px solid #e0e0e0;
}
.horizontal-m8k1d4 {
  writing-mode: horizontal-tb;
  width: 100%;
}
.vertical-rl-m8k1d4 {
  writing-mode: vertical-rl;
  height: 150px;
}
.vertical-lr-m8k1d4 {
  writing-mode: vertical-lr;
  height: 150px;
}
</style>

# 四、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 响应式字体大小

<html>
  <div class="responsive-demo">
    <h1 class="responsive-title">响应式标题</h1>
    <p class="responsive-text">
      这是一段响应式文本。调整浏览器窗口大小,观察字体大小的变化。
      使用 clamp() 函数可以实现流畅的字体缩放效果。
    </p>
  </div>
</html>
<style>
  .responsive-demo {
    padding: 20px;
    background: #f5f5f5;
    border-radius: 8px;
  }
  
  .responsive-title {
    font-size: clamp(1.5rem, 5vw, 3rem);
    font-weight: 700;
    line-height: 1.2;
    margin: 0 0 1rem 0;
    color: #1a1a1a;
  }
  
  .responsive-text {
    font-size: clamp(1rem, 2.5vw, 1.25rem);
    line-height: 1.6;
    color: #444;
    margin: 0;
  }
</style>

# 5.2 单行/多行文本溢出

<html>
  <div class="overflow-demo">
    <div class="single-line">
      <strong>单行溢出:</strong>这是一段很长很长很长很长很长很长很长很长很长的文本内容
    </div>
    <div class="multi-line">
      <strong>多行溢出(3行):</strong>
      这是一段很长的文本内容,用于演示多行文本溢出效果。
      当文本超过指定行数时,会显示省略号。
      这种效果在卡片列表、新闻标题等场景中非常常用。
      通过-webkit-line-clamp可以限制显示的行数。
    </div>
  </div>
</html>
<style>
  .overflow-demo {
    background: #fff;
    padding: 20px;
    border: 1px solid #ddd;
    border-radius: 4px;
  }
  
  .overflow-demo > div {
    margin-bottom: 20px;
    padding: 10px;
    background: #f9f9f9;
    border-radius: 4px;
  }
  
  .overflow-demo > div:last-child {
    margin-bottom: 0;
  }
  
  /* 单行溢出 */
  .single-line {
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  /* 多行溢出 */
  .multi-line {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.6;
  }
</style>

# 5.3 文字特效

<html>
  <div class="text-effects">
    <h2 class="glow-text">发光文字</h2>
    <h2 class="3d-text">立体文字</h2>
    <h2 class="gradient-text">渐变文字</h2>
    <h2 class="stroke-text">描边文字</h2>
  </div>
</html>
<style>
  .text-effects {
    background: #1a1a1a;
    padding: 40px 20px;
    text-align: center;
    border-radius: 8px;
  }
  
  .text-effects h2 {
    margin: 30px 0;
    font-size: 2.5rem;
    font-weight: 700;
  }
  
  /* 发光效果 */
  .glow-text {
    color: #fff;
    text-shadow: 
      0 0 10px #fff,
      0 0 20px #fff,
      0 0 30px #00ffff,
      0 0 40px #00ffff,
      0 0 50px #00ffff;
    animation: glow 2s ease-in-out infinite alternate;
  }
  
  @keyframes glow {
    from {
      text-shadow: 
        0 0 10px #fff,
        0 0 20px #fff,
        0 0 30px #00ffff,
        0 0 40px #00ffff;
    }
    to {
      text-shadow: 
        0 0 20px #fff,
        0 0 30px #00ffff,
        0 0 40px #00ffff,
        0 0 50px #00ffff,
        0 0 60px #00ffff;
    }
  }
  
  /* 立体效果 */
  .3d-text {
    color: #ff6b6b;
    text-shadow: 
      1px 1px 0 #e55,
      2px 2px 0 #d44,
      3px 3px 0 #c33,
      4px 4px 0 #b22,
      5px 5px 0 #a11,
      6px 6px 10px rgba(0, 0, 0, 0.5);
  }
  
  /* 渐变文字 */
  .gradient-text {
    background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    background-size: 200% 200%;
    animation: gradient 3s ease infinite;
  }
  
  @keyframes gradient {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
  }
  
  /* 描边文字 */
  .stroke-text {
    color: transparent;
    -webkit-text-stroke: 2px #ffd700;
    text-stroke: 2px #ffd700;
  }
</style>

# 5.4 优雅的段落排版

<html>
  <article class="elegant-article">
    <h1>优雅的文本排版</h1>
    <p class="lead">
      这是一段引言文字,通常使用稍大的字号和不同的颜色来突出显示。
      好的排版能够显著提升阅读体验。
    </p>
    <p>
      在网页设计中,<strong>排版</strong>是最重要的元素之一。
      合适的<em>字体大小</em>、<em>行高</em>和<em>字符间距</em>能够
      让内容更易读、更美观。一般来说,正文字体大小建议在16px左右,
      行高建议在1.6-1.8之间。
    </p>
    <p>
      段落之间应该有适当的间距,这样可以帮助读者更好地理解内容的层次结构。
      同时,每行的字符数也很重要,<code>45-75</code>个字符是最佳范围。
    </p>
    <blockquote>
      "Typography is the craft of endowing human language with a durable visual form."
      <cite>— Robert Bringhurst</cite>
    </blockquote>
  </article>
</html>
<style>
  .elegant-article {
    max-width: 650px;
    margin: 0 auto;
    padding: 40px 20px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #fff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    border-radius: 8px;
  }
  
  .elegant-article h1 {
    font-size: 2.5rem;
    font-weight: 700;
    line-height: 1.2;
    margin: 0 0 1.5rem 0;
    color: #1a1a1a;
    letter-spacing: -0.02em;
  }
  
  .elegant-article .lead {
    font-size: 1.25rem;
    line-height: 1.6;
    color: #555;
    margin: 0 0 2rem 0;
  }
  
  .elegant-article p {
    font-size: 1rem;
    line-height: 1.75;
    color: #333;
    margin: 0 0 1.5rem 0;
  }
  
  .elegant-article strong {
    font-weight: 600;
    color: #1a1a1a;
  }
  
  .elegant-article em {
    font-style: italic;
    color: #0066cc;
  }
  
  .elegant-article code {
    font-family: 'Courier New', monospace;
    background: #f5f5f5;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 0.9em;
    color: #e83e8c;
  }
  
  .elegant-article blockquote {
    margin: 2rem 0;
    padding: 1rem 1.5rem;
    border-left: 4px solid #0066cc;
    background: #f8f9fa;
    font-style: italic;
    color: #555;
  }
  
  .elegant-article cite {
    display: block;
    margin-top: 0.5rem;
    font-size: 0.9rem;
    color: #888;
    font-style: normal;
  }
</style>

# 六、最佳实践

# 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;
}

# 八、总结

# 核心要点

  1. 字体属性:

    • font-family:优先使用系统字体,提供回退方案
    • font-size:使用 rem 实现响应式
    • font-weight:注意字体文件支持
    • font:简写属性方便但会重置其他值
  2. 文本样式:

    • color:注意对比度,保证可读性
    • text-align:合理使用对齐方式
    • text-decoration:常用于链接样式
    • text-shadow:可实现丰富的视觉效果
  3. 文本排版:

    • line-height:无单位值,1.5-1.8最佳
    • letter-spacing:微调提升视觉效果
    • white-space:控制空白和换行
    • text-overflow:处理溢出文本
  4. Web字体:

    • 优先使用 woff2 格式
    • 使用 font-display: swap 提升性能
    • 考虑字体加载对性能的影响
    • 可变字体可减少文件数量
  5. 最佳实践:

    • 保证可读性(字号、行高、对比度)
    • 优化性能(系统字体、字体子集、预加载)
    • 注意无障碍性(对比度、可缩放)
    • 提供合理的字体栈

掌握文本与字体的CSS属性,能够让你的网页内容更加美观、易读,提升用户体验。

祝你变得更强!

编辑 (opens new window)
#CSS
上次更新: 2025/11/21
CSS基础-单位与颜色系统
CSS基础-背景、列表与表格样式

← CSS基础-单位与颜色系统 CSS基础-背景、列表与表格样式→

最近更新
01
AI编程时代的一些心得
09-11
02
Claude Code与Codex的协同工作
09-01
03
Claude Code 最佳实践(个人版)
08-01
更多文章>
Theme by Vdoing | Copyright © 2018-2025 京ICP备2021021832号-2 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式