轩辕李的博客 轩辕李的博客
首页
  • 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基础-文本与字体
    • CSS基础-背景、列表与表格样式
      • 一、背景属性
        • 1.1 background-color(背景颜色)
        • 1.2 background-image(背景图片)
        • 1.3 background-repeat(背景重复)
        • 1.4 background-position(背景位置)
        • 1.5 background-size(背景尺寸)
        • 1.6 background-attachment(背景附着)
        • 1.7 background-origin(背景原点)
        • 1.8 background-clip(背景裁剪)
        • 1.9 background(简写属性)
        • 1.10 渐变背景
        • 线性渐变(linear-gradient)
        • 径向渐变(radial-gradient)
        • 锥形渐变(conic-gradient)
        • 重复渐变
      • 二、列表样式
        • 2.1 list-style-type(列表标记类型)
        • 2.2 list-style-position(标记位置)
        • 2.3 list-style-image(自定义标记图片)
        • 2.4 list-style(简写属性)
        • 2.5 移除默认列表样式
        • 2.6 自定义列表样式实战
        • 计数器列表
        • Icon 列表
      • 三、表格样式
        • 3.1 border-collapse(边框合并)
        • 3.2 border-spacing(边框间距)
        • 3.3 table-layout(表格布局算法)
        • 3.4 caption-side(标题位置)
        • 3.5 empty-cells(空单元格)
        • 3.6 vertical-align(垂直对齐)
      • 四、实战案例
        • 4.1 现代表格样式
        • 4.2 斑马纹表格
        • 4.3 响应式表格
        • 4.4 自定义列表导航
        • 4.5 渐变背景卡片
      • 五、最佳实践
        • 5.1 背景图片优化
        • 5.2 列表语义化
        • 5.3 表格可访问性
        • 5.4 性能优化
        • 5.5 常见错误
      • 六、浏览器兼容性
        • 前缀处理
      • 七、总结
        • 核心要点
    • 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-10
目录

CSS基础-背景、列表与表格样式

背景、列表和表格是网页内容组织与装饰的重要元素。掌握这些CSS属性能够让页面更加美观、结构化。

本文将系统介绍CSS背景属性、列表样式和表格样式的使用方法与最佳实践。

# 一、背景属性

# 1.1 background-color(背景颜色)

/* 颜色关键字 */
.box {
  background-color: red;
}

/* 十六进制 */
.box {
  background-color: #f0f0f0;
}

/* RGB */
.box {
  background-color: rgb(240, 240, 240);
}

/* RGBA(带透明度) */
.box {
  background-color: rgba(0, 0, 0, 0.5);
}

/* HSL */
.box {
  background-color: hsl(0, 0%, 94%);
}

/* 透明 */
.box {
  background-color: transparent; /* 默认值 */
}

/* CSS变量 */
:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f5f5f5;
}
.box {
  background-color: var(--bg-primary);
}
<html>
  <div class="bg-color-demo-nw4441">
    <div class="bg-box-nw4441 hex-nw4441">#ff6b6b</div>
    <div class="bg-box-nw4441 rgb-nw4441">rgb(78, 205, 196)</div>
    <div class="bg-box-nw4441 rgba-nw4441">rgba(255, 107, 107, 0.5)</div>
    <div class="bg-box-nw4441 hsl-nw4441">hsl(200, 60%, 50%)</div>
  </div>
</html>
<style>
.bg-color-demo-nw4441 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}
.bg-box-nw4441 {
  padding: 30px;
  border-radius: 8px;
  text-align: center;
  color: white;
  font-weight: 600;
  font-size: 14px;
}
.hex-nw4441 { background-color: #ff6b6b; }
.rgb-nw4441 { background-color: rgb(78, 205, 196); }
.rgba-nw4441 { 
  background-color: rgba(255, 107, 107, 0.5);
  border: 2px solid #ff6b6b;
}
.hsl-nw4441 { background-color: hsl(200, 60%, 50%); }
</style>

# 1.2 background-image(背景图片)

/* 单张图片 */
.box {
  background-image: url('image.jpg');
}

/* 多张背景图(从上到下叠加) */
.box {
  background-image: 
    url('overlay.png'),
    url('background.jpg');
}

/* 渐变背景 */
.box {
  background-image: linear-gradient(to right, #ff6b6b, #4ecdc4);
}

/* 图片+渐变 */
.box {
  background-image: 
    linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
    url('image.jpg');
}

/* 无背景图 */
.box {
  background-image: none; /* 默认值 */
}

# 1.3 background-repeat(背景重复)

/* 默认:水平和垂直都重复 */
.box {
  background-repeat: repeat;
}

/* 不重复 */
.box {
  background-repeat: no-repeat;
}

/* 仅水平重复 */
.box {
  background-repeat: repeat-x;
}

/* 仅垂直重复 */
.box {
  background-repeat: repeat-y;
}

/* 平铺(尽量不裁剪) */
.box {
  background-repeat: space; /* 背景图之间有间距 */
}

/* 缩放平铺(可能被裁剪) */
.box {
  background-repeat: round; /* 拉伸背景图以填充 */
}

/* 多背景不同重复方式 */
.box {
  background-image: url('pattern.png'), url('bg.jpg');
  background-repeat: repeat, no-repeat;
}
<html>
  <div class="bg-repeat-demo-nw4441">
    <div class="repeat-box-nw4441 no-repeat-nw4441">
      <div class="label-nw4441">no-repeat</div>
    </div>
    <div class="repeat-box-nw4441 repeat-nw4441">
      <div class="label-nw4441">repeat</div>
    </div>
    <div class="repeat-box-nw4441 repeat-x-nw4441">
      <div class="label-nw4441">repeat-x</div>
    </div>
    <div class="repeat-box-nw4441 repeat-y-nw4441">
      <div class="label-nw4441">repeat-y</div>
    </div>
  </div>
</html>
<style>
.bg-repeat-demo-nw4441 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.repeat-box-nw4441 {
  height: 150px;
  border: 2px solid #ddd;
  border-radius: 8px;
  position: relative;
  background-image: linear-gradient(45deg, #ff6b6b 25%, transparent 25%, transparent 75%, #ff6b6b 75%, #ff6b6b), 
                     linear-gradient(45deg, #ff6b6b 25%, transparent 25%, transparent 75%, #ff6b6b 75%, #ff6b6b);
  background-size: 20px 20px;
  background-position: 0 0, 10px 10px;
}
.label-nw4441 {
  position: absolute;
  bottom: 10px;
  left: 10px;
  background: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}
.no-repeat-nw4441 { background-repeat: no-repeat; }
.repeat-nw4441 { background-repeat: repeat; }
.repeat-x-nw4441 { background-repeat: repeat-x; }
.repeat-y-nw4441 { background-repeat: repeat-y; }
</style>

# 1.4 background-position(背景位置)

/* 关键字 */
.box {
  background-position: center;        /* 居中 */
  background-position: top;           /* 顶部居中 */
  background-position: bottom;        /* 底部居中 */
  background-position: left;          /* 左侧居中 */
  background-position: right;         /* 右侧居中 */
}

/* 组合关键字 */
.box {
  background-position: top left;      /* 左上角 */
  background-position: top right;     /* 右上角 */
  background-position: bottom left;   /* 左下角 */
  background-position: bottom right;  /* 右下角 */
  background-position: center center; /* 中心 */
}

/* 像素值 */
.box {
  background-position: 20px 30px; /* x=20px, y=30px(从左上角) */
}

/* 百分比 */
.box {
  background-position: 50% 50%; /* 居中 */
  background-position: 0% 0%;   /* 左上角 */
  background-position: 100% 100%; /* 右下角 */
}

/* 混合使用 */
.box {
  background-position: center 20px;   /* 水平居中,距顶部20px */
  background-position: right 10px bottom 20px; /* 距右10px,距底20px */
}

/* 多背景不同位置 */
.box {
  background-image: url('icon.png'), url('bg.jpg');
  background-position: center, top left;
}
<html>
  <div class="bg-position-demo-k8s7w2">
    <div class="pos-box-k8s7w2 center-k8s7w2">
      <div class="label-k8s7w2">center</div>
      <div class="icon-k8s7w2">★</div>
    </div>
    <div class="pos-box-k8s7w2 top-left-k8s7w2">
      <div class="label-k8s7w2">top left</div>
      <div class="icon-k8s7w2">★</div>
    </div>
    <div class="pos-box-k8s7w2 bottom-right-k8s7w2">
      <div class="label-k8s7w2">bottom right</div>
      <div class="icon-k8s7w2">★</div>
    </div>
    <div class="pos-box-k8s7w2 custom-k8s7w2">
      <div class="label-k8s7w2">20px 30px</div>
      <div class="icon-k8s7w2">★</div>
    </div>
  </div>
</html>
<style>
.bg-position-demo-k8s7w2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.pos-box-k8s7w2 {
  height: 150px;
  border: 2px solid #667eea;
  border-radius: 8px;
  position: relative;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}
.label-k8s7w2 {
  position: absolute;
  top: 10px;
  left: 10px;
  background: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  color: #667eea;
  border: 1px solid #667eea;
  z-index: 1;
}
.icon-k8s7w2 {
  position: absolute;
  font-size: 40px;
  color: #ff6b6b;
  text-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.center-k8s7w2 .icon-k8s7w2 {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.top-left-k8s7w2 .icon-k8s7w2 {
  top: 0;
  left: 0;
}
.bottom-right-k8s7w2 .icon-k8s7w2 {
  bottom: 0;
  right: 0;
}
.custom-k8s7w2 .icon-k8s7w2 {
  top: 30px;
  left: 20px;
}
</style>

# 1.5 background-size(背景尺寸)

/* 关键字 */
.box {
  background-size: auto;   /* 默认:图片原始大小 */
}

/* cover(覆盖,可能裁剪) */
.box {
  background-size: cover;
  /* 图片等比缩放,完全覆盖容器,可能有部分被裁剪 */
}

/* contain(包含,可能留白) */
.box {
  background-size: contain;
  /* 图片等比缩放,完整显示在容器内,可能有留白 */
}

/* 像素值 */
.box {
  background-size: 200px 100px; /* 宽200px,高100px */
  background-size: 200px;       /* 宽200px,高度自动 */
}

/* 百分比(相对于容器) */
.box {
  background-size: 100% 100%; /* 拉伸填充 */
  background-size: 50%;       /* 宽50%,高度自动 */
}

/* 多背景不同尺寸 */
.box {
  background-image: url('icon.png'), url('bg.jpg');
  background-size: 50px, cover;
}
<html>
  <div class="bg-size-demo-nw4441">
    <div class="size-box-nw4441 auto-nw4441">
      <div class="label-nw4441">auto(原始大小50×50)</div>
    </div>
    <div class="size-box-nw4441 cover-nw4441">
      <div class="label-nw4441">cover(覆盖填满)</div>
    </div>
    <div class="size-box-nw4441 contain-nw4441">
      <div class="label-nw4441">contain(完整显示)</div>
    </div>
    <div class="size-box-nw4441 custom-nw4441">
      <div class="label-nw4441">120px 80px(拉伸)</div>
    </div>
  </div>
</html>
<style>
.bg-size-demo-nw4441 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.size-box-nw4441 {
  height: 150px;
  border: 2px solid #667eea;
  border-radius: 8px;
  position: relative;
  background-color: #f0f0f0;
  /* 使用SVG作为背景图,创建一个笑脸图案 */
  background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="45" fill="%23667eea"/><circle cx="35" cy="40" r="5" fill="white"/><circle cx="65" cy="40" r="5" fill="white"/><path d="M 30 60 Q 50 80 70 60" stroke="white" stroke-width="4" fill="none" stroke-linecap="round"/></svg>');
  background-position: center;
  background-repeat: no-repeat;
}
.label-nw4441 {
  position: absolute;
  bottom: 10px;
  left: 10px;
  background: rgba(255, 255, 255, 0.95);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  color: #667eea;
  border: 1px solid #667eea;
}
.auto-nw4441 { 
  background-size: 50px 50px;
}
.cover-nw4441 { 
  background-size: cover;
}
.contain-nw4441 { 
  background-size: contain;
}
.custom-nw4441 { 
  background-size: 120px 80px;
}
</style>

# 1.6 background-attachment(背景附着)

控制背景图片是否随页面滚动。

/* 默认:随内容滚动 */
.box {
  background-attachment: scroll;
}

/* 固定:不随页面滚动(视差效果) */
.box {
  background-attachment: fixed;
  background-image: url('parallax.jpg');
  background-size: cover;
  background-position: center;
}

/* 局部固定:相对于元素固定 */
.box {
  background-attachment: local;
  /* 背景随元素内容滚动 */
}

/* 视差滚动效果 */
.parallax {
  height: 500px;
  background-image: url('mountain.jpg');
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
}
<html>
  <div class="bg-attachment-demo-m3x9q5">
    <div class="attach-box-m3x9q5 scroll-m3x9q5">
      <div class="content-m3x9q5">
        <div class="label-m3x9q5">scroll (默认)</div>
        <p>滚动此区域,背景会随内容滚动</p>
        <div class="spacer-m3x9q5"></div>
      </div>
    </div>
    <div class="attach-box-m3x9q5 local-m3x9q5">
      <div class="content-m3x9q5">
        <div class="label-m3x9q5">local</div>
        <p>滚动此区域,背景会随内容滚动</p>
        <div class="spacer-m3x9q5"></div>
      </div>
    </div>
  </div>
  <div class="note-m3x9q5">注意:fixed 值在页面级别生效,在小容器内演示效果不明显</div>
</html>
<style>
.bg-attachment-demo-m3x9q5 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  margin-bottom: 10px;
}
.attach-box-m3x9q5 {
  height: 200px;
  border: 2px solid #ddd;
  border-radius: 8px;
  overflow-y: scroll;
  background-image: linear-gradient(45deg, #667eea 25%, transparent 25%, transparent 75%, #667eea 75%, #667eea), 
                     linear-gradient(45deg, #667eea 25%, transparent 25%, transparent 75%, #667eea 75%, #667eea);
  background-size: 30px 30px;
  background-position: 0 0, 15px 15px;
  background-color: #f0f0f0;
}
.content-m3x9q5 {
  padding: 15px;
}
.label-m3x9q5 {
  display: inline-block;
  background: white;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 10px;
  border: 1px solid #ddd;
}
.content-m3x9q5 p {
  margin: 10px 0;
  background: white;
  padding: 8px;
  border-radius: 4px;
}
.spacer-m3x9q5 {
  height: 300px;
}
.scroll-m3x9q5 { background-attachment: scroll; }
.local-m3x9q5 { background-attachment: local; }
.note-m3x9q5 {
  font-size: 13px;
  color: #666;
  font-style: italic;
  padding: 8px;
  background: #f8f9fa;
  border-radius: 4px;
}
</style>

# 1.7 background-origin(背景原点)

定义背景图片的定位区域。

/* 从padding区域开始(默认) */
.box {
  background-origin: padding-box;
}

/* 从border区域开始 */
.box {
  background-origin: border-box;
}

/* 从content区域开始 */
.box {
  background-origin: content-box;
}

/* 示例 */
.demo {
  padding: 20px;
  border: 10px solid rgba(0, 0, 0, 0.2);
  background-image: url('pattern.png');
  background-repeat: no-repeat;
  background-origin: border-box; /* 图片从边框开始 */
}
<html>
  <div class="bg-origin-demo-p7n2k4">
    <div class="origin-box-p7n2k4 border-box-p7n2k4">
      <div class="label-p7n2k4">border-box</div>
      <div class="text-p7n2k4">背景从边框区域开始</div>
    </div>
    <div class="origin-box-p7n2k4 padding-box-p7n2k4">
      <div class="label-p7n2k4">padding-box (默认)</div>
      <div class="text-p7n2k4">背景从内边距区域开始</div>
    </div>
    <div class="origin-box-p7n2k4 content-box-p7n2k4">
      <div class="label-p7n2k4">content-box</div>
      <div class="text-p7n2k4">背景从内容区域开始</div>
    </div>
  </div>
</html>
<style>
.bg-origin-demo-p7n2k4 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}
.origin-box-p7n2k4 {
  padding: 20px;
  border: 10px dashed rgba(102, 126, 234, 0.5);
  background-image: radial-gradient(circle, #ff6b6b 40%, transparent 40%);
  background-repeat: no-repeat;
  background-position: top left;
  background-size: 50px 50px;
  background-color: #f0f0f0;
  border-radius: 8px;
}
.label-p7n2k4 {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 5px;
  color: #333;
}
.text-p7n2k4 {
  font-size: 11px;
  color: #666;
}
.border-box-p7n2k4 { background-origin: border-box; }
.padding-box-p7n2k4 { background-origin: padding-box; }
.content-box-p7n2k4 { background-origin: content-box; }
</style>

# 1.8 background-clip(背景裁剪)

定义背景的绘制区域。

/* 绘制到border区域(默认) */
.box {
  background-clip: border-box;
}

/* 绘制到padding区域 */
.box {
  background-clip: padding-box;
}

/* 绘制到content区域 */
.box {
  background-clip: content-box;
}

/* 绘制到文字(渐变文字效果) */
.gradient-text {
  background-image: linear-gradient(45deg, #ff6b6b, #4ecdc4);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}
<html>
  <div class="bg-clip-demo-t5j8r1">
    <div class="clip-container-t5j8r1">
      <div class="clip-box-t5j8r1 border-box-t5j8r1">
        <div class="label-t5j8r1">border-box (默认)</div>
        <div class="text-t5j8r1">背景绘制到边框</div>
      </div>
      <div class="clip-box-t5j8r1 padding-box-t5j8r1">
        <div class="label-t5j8r1">padding-box</div>
        <div class="text-t5j8r1">背景绘制到内边距</div>
      </div>
      <div class="clip-box-t5j8r1 content-box-t5j8r1">
        <div class="label-t5j8r1">content-box</div>
        <div class="text-t5j8r1">背景绘制到内容</div>
      </div>
    </div>
    <div class="gradient-text-demo-t5j8r1">
      <div class="gradient-text-t5j8r1">渐变文字效果 (text)</div>
    </div>
  </div>
</html>
<style>
.bg-clip-demo-t5j8r1 {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.clip-container-t5j8r1 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}
.clip-box-t5j8r1 {
  padding: 20px;
  border: 10px solid transparent;
  background-image: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 8px;
}
.label-t5j8r1 {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 5px;
  color: white;
}
.text-t5j8r1 {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.9);
}
.border-box-t5j8r1 { 
  background-clip: border-box;
  border-color: rgba(255, 255, 255, 0.3);
}
.padding-box-t5j8r1 { 
  background-clip: padding-box;
  border-color: rgba(255, 255, 255, 0.3);
}
.content-box-t5j8r1 { 
  background-clip: content-box;
  border-color: rgba(255, 255, 255, 0.3);
}
.gradient-text-demo-t5j8r1 {
  text-align: center;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}
.gradient-text-t5j8r1 {
  font-size: 36px;
  font-weight: 900;
  background-image: linear-gradient(45deg, #ff6b6b, #4ecdc4, #ffd93d);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}
</style>

# 1.9 background(简写属性)

/* 语法顺序(不强制,但建议):*/
/* color image position/size repeat attachment origin clip */

/* 基础用法 */
.box {
  background: #f0f0f0;
}

.box {
  background: url('image.jpg');
}

/* 完整语法 */
.box {
  background: #f0f0f0 url('bg.jpg') center/cover no-repeat fixed;
}

/* 等价于 */
.box {
  background-color: #f0f0f0;
  background-image: url('bg.jpg');
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

/* 多背景简写 */
.box {
  background: 
    url('overlay.png') center/contain no-repeat,
    linear-gradient(to bottom, rgba(0,0,0,0.3), rgba(0,0,0,0.7)),
    url('bg.jpg') center/cover no-repeat;
}

/* 常用组合 */
.hero {
  background: url('hero.jpg') center/cover no-repeat fixed;
}

.pattern {
  background: #fff url('pattern.png') repeat;
}
<html>
  <div class="bg-shorthand-demo-v9h4m6">
    <div class="shorthand-box-v9h4m6 simple-v9h4m6">
      <div class="label-v9h4m6">简单背景色</div>
    </div>
    <div class="shorthand-box-v9h4m6 full-v9h4m6">
      <div class="label-v9h4m6">完整语法示例</div>
      <div class="desc-v9h4m6">center/cover no-repeat</div>
    </div>
    <div class="shorthand-box-v9h4m6 multi-v9h4m6">
      <div class="label-v9h4m6">多层背景</div>
      <div class="desc-v9h4m6">渐变 + 图案</div>
    </div>
  </div>
</html>
<style>
.bg-shorthand-demo-v9h4m6 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}
.shorthand-box-v9h4m6 {
  height: 150px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
}
.label-v9h4m6 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 5px;
}
.desc-v9h4m6 {
  font-size: 11px;
  opacity: 0.9;
}
.simple-v9h4m6 {
  background: #667eea;
}
.full-v9h4m6 {
  background: linear-gradient(135deg, #667eea, #764ba2) center/cover no-repeat;
}
.multi-v9h4m6 {
  background: 
    linear-gradient(rgba(102, 126, 234, 0.8), rgba(118, 75, 162, 0.8)),
    repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(255, 255, 255, 0.1) 10px, rgba(255, 255, 255, 0.1) 20px);
}
</style>

# 1.10 渐变背景

# 线性渐变(linear-gradient)

/* 基础渐变(从上到下) */
.box {
  background: linear-gradient(#ff6b6b, #4ecdc4);
}

/* 指定方向 */
.box {
  background: linear-gradient(to right, #ff6b6b, #4ecdc4);
  background: linear-gradient(to bottom right, #ff6b6b, #4ecdc4);
}

/* 角度 */
.box {
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
  background: linear-gradient(135deg, #ff6b6b, #4ecdc4);
}

/* 多个颜色 */
.box {
  background: linear-gradient(to right, #ff6b6b, #ffd93d, #4ecdc4);
}

/* 颜色位置 */
.box {
  background: linear-gradient(
    to right,
    #ff6b6b 0%,
    #ffd93d 50%,
    #4ecdc4 100%
  );
}

/* 硬过渡(条纹效果) */
.stripes {
  background: linear-gradient(
    90deg,
    #ff6b6b 0% 25%,
    #4ecdc4 25% 50%,
    #ff6b6b 50% 75%,
    #4ecdc4 75% 100%
  );
}

# 径向渐变(radial-gradient)

/* 基础渐变(圆形) */
.box {
  background: radial-gradient(#ff6b6b, #4ecdc4);
}

/* 椭圆渐变 */
.box {
  background: radial-gradient(ellipse, #ff6b6b, #4ecdc4);
}

/* 指定大小和位置 */
.box {
  background: radial-gradient(
    circle at center,
    #ff6b6b 0%,
    #4ecdc4 100%
  );
  
  background: radial-gradient(
    circle at top left,
    #ff6b6b,
    #4ecdc4
  );
}

/* 指定半径 */
.box {
  background: radial-gradient(
    circle 100px at center,
    #ff6b6b,
    #4ecdc4
  );
}

/* 大小关键字 */
.box {
  background: radial-gradient(
    circle closest-side,  /* 最近边 */
    #ff6b6b,
    #4ecdc4
  );
  /* closest-corner | farthest-side | farthest-corner */
}

# 锥形渐变(conic-gradient)

/* 基础锥形渐变 */
.box {
  background: conic-gradient(#ff6b6b, #4ecdc4, #ff6b6b);
}

/* 指定起始角度 */
.box {
  background: conic-gradient(
    from 45deg,
    #ff6b6b,
    #4ecdc4,
    #ff6b6b
  );
}

/* 色环 */
.color-wheel {
  background: conic-gradient(
    red, yellow, lime, aqua, blue, magenta, red
  );
  border-radius: 50%;
}

/* 饼图效果 */
.pie-chart {
  background: conic-gradient(
    #ff6b6b 0% 40%,
    #4ecdc4 40% 75%,
    #ffd93d 75% 100%
  );
  border-radius: 50%;
}

# 重复渐变

/* 重复线性渐变 */
.box {
  background: repeating-linear-gradient(
    45deg,
    #ff6b6b 0px,
    #ff6b6b 10px,
    #4ecdc4 10px,
    #4ecdc4 20px
  );
}

/* 重复径向渐变 */
.box {
  background: repeating-radial-gradient(
    circle,
    #ff6b6b 0px,
    #ff6b6b 10px,
    #4ecdc4 10px,
    #4ecdc4 20px
  );
}

/* 重复锥形渐变 */
.box {
  background: repeating-conic-gradient(
    #ff6b6b 0deg 15deg,
    #4ecdc4 15deg 30deg
  );
}
<html>
  <div class="gradient-demo-z2w8q3">
    <div class="gradient-section-z2w8q3">
      <div class="section-title-z2w8q3">线性渐变</div>
      <div class="gradient-grid-z2w8q3">
        <div class="gradient-box-z2w8q3 linear-basic-z2w8q3">
          <span>基础渐变</span>
        </div>
        <div class="gradient-box-z2w8q3 linear-angle-z2w8q3">
          <span>45deg</span>
        </div>
        <div class="gradient-box-z2w8q3 linear-multi-z2w8q3">
          <span>多色渐变</span>
        </div>
        <div class="gradient-box-z2w8q3 linear-repeat-z2w8q3">
          <span>重复渐变</span>
        </div>
      </div>
    </div>
    <div class="gradient-section-z2w8q3">
      <div class="section-title-z2w8q3">径向渐变</div>
      <div class="gradient-grid-z2w8q3">
        <div class="gradient-box-z2w8q3 radial-basic-z2w8q3">
          <span>基础径向</span>
        </div>
        <div class="gradient-box-z2w8q3 radial-circle-z2w8q3">
          <span>圆形</span>
        </div>
        <div class="gradient-box-z2w8q3 radial-position-z2w8q3">
          <span>位置控制</span>
        </div>
        <div class="gradient-box-z2w8q3 radial-repeat-z2w8q3">
          <span>重复径向</span>
        </div>
      </div>
    </div>
    <div class="gradient-section-z2w8q3">
      <div class="section-title-z2w8q3">锥形渐变</div>
      <div class="gradient-grid-z2w8q3">
        <div class="gradient-box-z2w8q3 conic-basic-z2w8q3">
          <span>基础锥形</span>
        </div>
        <div class="gradient-box-z2w8q3 conic-wheel-z2w8q3">
          <span>色环</span>
        </div>
        <div class="gradient-box-z2w8q3 conic-pie-z2w8q3">
          <span>饼图</span>
        </div>
        <div class="gradient-box-z2w8q3 conic-repeat-z2w8q3">
          <span>重复锥形</span>
        </div>
      </div>
    </div>
  </div>
</html>
<style>
.gradient-demo-z2w8q3 {
  display: flex;
  flex-direction: column;
  gap: 25px;
}
.gradient-section-z2w8q3 {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.section-title-z2w8q3 {
  font-size: 16px;
  font-weight: 700;
  color: #333;
  padding-left: 5px;
}
.gradient-grid-z2w8q3 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
}
.gradient-box-z2w8q3 {
  height: 120px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}
.gradient-box-z2w8q3 span {
  color: white;
  font-weight: 600;
  font-size: 13px;
  text-shadow: 1px 1px 3px rgba(0,0,0,0.3);
  z-index: 1;
}
/* 线性渐变 */
.linear-basic-z2w8q3 {
  background: linear-gradient(#ff6b6b, #4ecdc4);
}
.linear-angle-z2w8q3 {
  background: linear-gradient(45deg, #667eea, #764ba2);
}
.linear-multi-z2w8q3 {
  background: linear-gradient(to right, #ff6b6b, #ffd93d, #4ecdc4, #667eea);
}
.linear-repeat-z2w8q3 {
  background: repeating-linear-gradient(
    45deg,
    #ff6b6b 0px,
    #ff6b6b 10px,
    #4ecdc4 10px,
    #4ecdc4 20px
  );
}
/* 径向渐变 */
.radial-basic-z2w8q3 {
  background: radial-gradient(#ff6b6b, #4ecdc4);
}
.radial-circle-z2w8q3 {
  background: radial-gradient(circle, #ffd93d, #f39c12);
}
.radial-position-z2w8q3 {
  background: radial-gradient(circle at top left, #667eea, #764ba2);
}
.radial-repeat-z2w8q3 {
  background: repeating-radial-gradient(
    circle,
    #ff6b6b 0px,
    #ff6b6b 10px,
    #4ecdc4 10px,
    #4ecdc4 20px
  );
}
/* 锥形渐变 */
.conic-basic-z2w8q3 {
  background: conic-gradient(#ff6b6b, #ffd93d, #4ecdc4, #ff6b6b);
  border-radius: 50%;
}
.conic-wheel-z2w8q3 {
  background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
  border-radius: 50%;
}
.conic-pie-z2w8q3 {
  background: conic-gradient(
    #ff6b6b 0% 40%,
    #4ecdc4 40% 75%,
    #ffd93d 75% 100%
  );
  border-radius: 50%;
}
.conic-repeat-z2w8q3 {
  background: repeating-conic-gradient(
    #667eea 0deg 15deg,
    #764ba2 15deg 30deg
  );
  border-radius: 50%;
}
</style>

# 二、列表样式

# 2.1 list-style-type(列表标记类型)

/* 无序列表 */
ul {
  list-style-type: disc;        /* 实心圆(默认) */
  list-style-type: circle;      /* 空心圆 */
  list-style-type: square;      /* 实心方块 */
  list-style-type: none;        /* 无标记 */
}

/* 有序列表 */
ol {
  list-style-type: decimal;           /* 数字(默认):1, 2, 3 */
  list-style-type: decimal-leading-zero; /* 补零:01, 02, 03 */
  list-style-type: lower-roman;       /* 小写罗马数字:i, ii, iii */
  list-style-type: upper-roman;       /* 大写罗马数字:I, II, III */
  list-style-type: lower-alpha;       /* 小写字母:a, b, c */
  list-style-type: upper-alpha;       /* 大写字母:A, B, C */
  list-style-type: lower-greek;       /* 小写希腊字母:α, β, γ */
}

/* CSS3 扩展 */
ol {
  list-style-type: cjk-decimal;       /* 中文数字 */
  list-style-type: cjk-ideographic;   /* 中文习惯计数 */
  list-style-type: hiragana;          /* 日文平假名 */
  list-style-type: katakana;          /* 日文片假名 */
}

/* Emoji 标记 */
ul {
  list-style-type: '✓';
  list-style-type: '→';
  list-style-type: '★';
}
<html>
  <div class="list-style-demo-nw4441">
    <div class="list-box-nw4441">
      <div class="title-nw4441">无序列表</div>
      <ul class="disc-nw4441">
        <li>disc - 实心圆</li>
        <li>disc - 实心圆</li>
      </ul>
      <ul class="circle-nw4441">
        <li>circle - 空心圆</li>
        <li>circle - 空心圆</li>
      </ul>
      <ul class="square-nw4441">
        <li>square - 方块</li>
        <li>square - 方块</li>
      </ul>
    </div>
    <div class="list-box-nw4441">
      <div class="title-nw4441">有序列表</div>
      <ol class="decimal-nw4441">
        <li>decimal - 数字</li>
        <li>decimal - 数字</li>
      </ol>
      <ol class="alpha-nw4441">
        <li>lower-alpha - 字母</li>
        <li>lower-alpha - 字母</li>
      </ol>
      <ol class="roman-nw4441">
        <li>lower-roman - 罗马</li>
        <li>lower-roman - 罗马</li>
      </ol>
    </div>
  </div>
</html>
<style>
.list-style-demo-nw4441 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.list-box-nw4441 {
  background: #f9f9f9;
  padding: 20px;
  border-radius: 8px;
}
.title-nw4441 {
  font-weight: 600;
  margin-bottom: 15px;
  color: #333;
}
.list-style-demo-nw4441 ul,
.list-style-demo-nw4441 ol {
  margin: 10px 0;
  padding-left: 30px;
}
.list-style-demo-nw4441 li {
  margin: 5px 0;
}
.disc-nw4441 { list-style-type: disc; }
.circle-nw4441 { list-style-type: circle; }
.square-nw4441 { list-style-type: square; }
.decimal-nw4441 { list-style-type: decimal; }
.alpha-nw4441 { list-style-type: lower-alpha; }
.roman-nw4441 { list-style-type: lower-roman; }
</style>

# 2.2 list-style-position(标记位置)

/* 标记在列表项外部(默认) */
ul {
  list-style-position: outside;
}

/* 标记在列表项内部 */
ul {
  list-style-position: inside;
}

/* 对比示例 */
.outside li {
  list-style-position: outside;
  padding-left: 0;
  /* 标记不占用文本空间,文本对齐 */
}

.inside li {
  list-style-position: inside;
  /* 标记占用文本空间,可能导致对齐问题 */
}
<html>
  <div class="list-position-demo-b6y3n8">
    <div class="position-section-b6y3n8">
      <div class="title-b6y3n8">outside (默认)</div>
      <ul class="outside-b6y3n8">
        <li>标记在列表项外部,文本对齐整齐</li>
        <li>多行文本时,第二行不会和标记对齐</li>
        <li>这是最常用的列表样式</li>
      </ul>
    </div>
    <div class="position-section-b6y3n8">
      <div class="title-b6y3n8">inside</div>
      <ul class="inside-b6y3n8">
        <li>标记在列表项内部,占用文本空间</li>
        <li>多行文本时,第二行会和标记对齐</li>
        <li>可能导致文本对齐问题</li>
      </ul>
    </div>
  </div>
</html>
<style>
.list-position-demo-b6y3n8 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.position-section-b6y3n8 {
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 2px solid #e9ecef;
}
.title-b6y3n8 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: #667eea;
}
.outside-b6y3n8 {
  list-style-position: outside;
  list-style-type: disc;
  margin: 0;
  padding-left: 25px;
}
.inside-b6y3n8 {
  list-style-position: inside;
  list-style-type: disc;
  margin: 0;
  padding-left: 5px;
}
.outside-b6y3n8 li,
.inside-b6y3n8 li {
  margin-bottom: 8px;
  line-height: 1.6;
  color: #333;
  font-size: 14px;
}
</style>

# 2.3 list-style-image(自定义标记图片)

/* 使用图片作为标记 */
ul {
  list-style-image: url('bullet.png');
}

/* 图片 + 备用标记 */
ul {
  list-style-type: disc; /* 图片加载失败时的备用方案 */
  list-style-image: url('bullet.svg');
}

/* 更好的控制:使用伪元素 */
ul {
  list-style: none;
}

ul li::before {
  content: '';
  display: inline-block;
  width: 16px;
  height: 16px;
  background: url('bullet.svg') no-repeat center;
  background-size: contain;
  margin-right: 8px;
  vertical-align: middle;
}
<html>
  <div class="list-image-demo-c4m7k9">
    <div class="image-section-c4m7k9">
      <div class="title-c4m7k9">使用渐变作为标记</div>
      <ul class="gradient-marker-c4m7k9">
        <li>自定义标记项 1</li>
        <li>自定义标记项 2</li>
        <li>自定义标记项 3</li>
      </ul>
    </div>
    <div class="image-section-c4m7k9">
      <div class="title-c4m7k9">使用 emoji 作为标记</div>
      <ul class="emoji-marker-c4m7k9">
        <li>完成的任务</li>
        <li>进行中的任务</li>
        <li>重要的任务</li>
      </ul>
    </div>
  </div>
</html>
<style>
.list-image-demo-c4m7k9 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.image-section-c4m7k9 {
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 2px solid #e9ecef;
}
.title-c4m7k9 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: #667eea;
}
.gradient-marker-c4m7k9 {
  list-style: none;
  margin: 0;
  padding: 0;
}
.gradient-marker-c4m7k9 li {
  padding-left: 30px;
  position: relative;
  margin-bottom: 10px;
  line-height: 1.6;
  color: #333;
  font-size: 14px;
}
.gradient-marker-c4m7k9 li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 4px;
  width: 18px;
  height: 18px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 50%;
}
.emoji-marker-c4m7k9 {
  list-style: none;
  margin: 0;
  padding: 0;
}
.emoji-marker-c4m7k9 li {
  padding-left: 30px;
  position: relative;
  margin-bottom: 10px;
  line-height: 1.6;
  color: #333;
  font-size: 14px;
}
.emoji-marker-c4m7k9 li:nth-child(1)::before {
  content: '✓';
  position: absolute;
  left: 0;
  color: #28a745;
  font-weight: bold;
  font-size: 18px;
}
.emoji-marker-c4m7k9 li:nth-child(2)::before {
  content: '⏳';
  position: absolute;
  left: 0;
  font-size: 16px;
}
.emoji-marker-c4m7k9 li:nth-child(3)::before {
  content: '★';
  position: absolute;
  left: 0;
  color: #ffd93d;
  font-size: 18px;
}
</style>

# 2.4 list-style(简写属性)

/* 语法:type position image */

/* 基础用法 */
ul {
  list-style: square;
}

ul {
  list-style: none;
}

/* 完整语法 */
ul {
  list-style: square inside url('bullet.png');
}

/* 等价于 */
ul {
  list-style-type: square;
  list-style-position: inside;
  list-style-image: url('bullet.png');
}

# 2.5 移除默认列表样式

/* 完全重置列表样式 */
ul, ol {
  list-style: none;   /* 移除标记 */
  margin: 0;          /* 移除外边距 */
  padding: 0;         /* 移除内边距 */
}

/* 导航菜单常用重置 */
nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

nav ul li {
  display: inline-block; /* 或 flex */
}

# 2.6 自定义列表样式实战

# 计数器列表

/* 使用CSS计数器 */
ol {
  list-style: none;
  counter-reset: my-counter;
  padding-left: 0;
}

ol li {
  counter-increment: my-counter;
  padding-left: 40px;
  position: relative;
  margin-bottom: 10px;
}

ol li::before {
  content: counter(my-counter);
  position: absolute;
  left: 0;
  top: 0;
  width: 30px;
  height: 30px;
  background: #007bff;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

/* 自定义格式 */
ol li::before {
  content: "Step " counter(my-counter);
}

/* 罗马数字 */
ol li::before {
  content: counter(my-counter, upper-roman) ".";
}

# Icon 列表

/* 使用Font Awesome图标 */
.checklist {
  list-style: none;
  padding: 0;
}

.checklist li {
  padding-left: 30px;
  position: relative;
  margin-bottom: 10px;
}

.checklist li::before {
  content: '✓';
  position: absolute;
  left: 0;
  color: #28a745;
  font-weight: bold;
  font-size: 1.2em;
}

/* 不同状态 */
.checklist li.pending::before {
  content: '○';
  color: #ffc107;
}

.checklist li.done::before {
  content: '✓';
  color: #28a745;
}

.checklist li.failed::before {
  content: '✗';
  color: #dc3545;
}
<html>
  <div class="custom-list-demo-q8v5n2">
    <div class="section-q8v5n2">
      <h4>计数器列表</h4>
      <ol class="counter-list-q8v5n2">
        <li>打开项目文件夹</li>
        <li>安装项目依赖</li>
        <li>配置开发环境</li>
        <li>启动开发服务器</li>
      </ol>
    </div>
    <div class="section-q8v5n2">
      <h4>Icon列表(不同状态)</h4>
      <ul class="checklist-q8v5n2">
        <li class="done-q8v5n2">需求分析完成</li>
        <li class="done-q8v5n2">设计稿审核通过</li>
        <li class="pending-q8v5n2">前端开发进行中</li>
        <li class="failed-q8v5n2">测试未通过</li>
      </ul>
    </div>
    <div class="section-q8v5n2">
      <h4>罗马数字列表</h4>
      <ol class="roman-list-q8v5n2">
        <li>第一章 概述</li>
        <li>第二章 基础知识</li>
        <li>第三章 进阶技巧</li>
        <li>第四章 实战案例</li>
      </ol>
    </div>
    <div class="section-q8v5n2">
      <h4>彩色标签列表</h4>
      <ul class="tag-list-q8v5n2">
        <li class="tag-primary-q8v5n2">HTML基础</li>
        <li class="tag-success-q8v5n2">CSS进阶</li>
        <li class="tag-warning-q8v5n2">JavaScript</li>
        <li class="tag-danger-q8v5n2">框架应用</li>
      </ul>
    </div>
  </div>
</html>
<style>
.custom-list-demo-q8v5n2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.section-q8v5n2 {
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}
.section-q8v5n2 h4 {
  margin: 0 0 15px 0;
  font-size: 14px;
  color: #667eea;
  font-weight: 600;
}

/* 计数器列表 */
.counter-list-q8v5n2 {
  list-style: none;
  counter-reset: step-counter;
  padding-left: 0;
  margin: 0;
}
.counter-list-q8v5n2 li {
  counter-increment: step-counter;
  padding-left: 45px;
  position: relative;
  margin-bottom: 12px;
  font-size: 14px;
  color: #333;
}
.counter-list-q8v5n2 li::before {
  content: counter(step-counter);
  position: absolute;
  left: 0;
  top: -2px;
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 14px;
}

/* Icon列表 */
.checklist-q8v5n2 {
  list-style: none;
  padding: 0;
  margin: 0;
}
.checklist-q8v5n2 li {
  padding-left: 30px;
  position: relative;
  margin-bottom: 10px;
  font-size: 14px;
  color: #333;
}
.checklist-q8v5n2 li::before {
  position: absolute;
  left: 0;
  font-weight: bold;
  font-size: 18px;
}
.checklist-q8v5n2 li.done-q8v5n2::before {
  content: '✓';
  color: #28a745;
}
.checklist-q8v5n2 li.pending-q8v5n2::before {
  content: '○';
  color: #ffc107;
}
.checklist-q8v5n2 li.failed-q8v5n2::before {
  content: '✗';
  color: #dc3545;
}

/* 罗马数字列表 */
.roman-list-q8v5n2 {
  list-style: none;
  counter-reset: roman-counter;
  padding-left: 0;
  margin: 0;
}
.roman-list-q8v5n2 li {
  counter-increment: roman-counter;
  padding-left: 40px;
  position: relative;
  margin-bottom: 10px;
  font-size: 14px;
  color: #333;
}
.roman-list-q8v5n2 li::before {
  content: counter(roman-counter, upper-roman) ".";
  position: absolute;
  left: 0;
  color: #667eea;
  font-weight: bold;
  font-size: 14px;
}

/* 彩色标签列表 */
.tag-list-q8v5n2 {
  list-style: none;
  padding: 0;
  margin: 0;
}
.tag-list-q8v5n2 li {
  padding-left: 25px;
  position: relative;
  margin-bottom: 10px;
  font-size: 14px;
  color: #333;
}
.tag-list-q8v5n2 li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 6px;
  width: 16px;
  height: 16px;
  border-radius: 3px;
}
.tag-list-q8v5n2 li.tag-primary-q8v5n2::before {
  background: #007bff;
}
.tag-list-q8v5n2 li.tag-success-q8v5n2::before {
  background: #28a745;
}
.tag-list-q8v5n2 li.tag-warning-q8v5n2::before {
  background: #ffc107;
}
.tag-list-q8v5n2 li.tag-danger-q8v5n2::before {
  background: #dc3545;
}
</style>

# 三、表格样式

# 3.1 border-collapse(边框合并)

控制表格边框是否合并。

/* 分离边框(默认) */
table {
  border-collapse: separate;
  border-spacing: 2px; /* 边框间距 */
}

/* 合并边框(推荐) */
table {
  border-collapse: collapse;
  /* 消除双边框,使表格更紧凑 */
}

/* 对比示例 */
.separate-table {
  border-collapse: separate;
  border-spacing: 5px;
}

.collapse-table {
  border-collapse: collapse;
}

.collapse-table th,
.collapse-table td {
  border: 1px solid #ddd;
}
<html>
  <div class="table-collapse-demo-nw4441">
    <div class="table-wrapper-nw4441">
      <div class="label-nw4441">separate - 分离边框</div>
      <table class="separate-nw4441">
        <tr>
          <th>列标题1</th>
          <th>列标题2</th>
          <th>列标题3</th>
        </tr>
        <tr>
          <td>数据1</td>
          <td>数据2</td>
          <td>数据3</td>
        </tr>
        <tr>
          <td>数据4</td>
          <td>数据5</td>
          <td>数据6</td>
        </tr>
      </table>
    </div>
    <div class="table-wrapper-nw4441">
      <div class="label-nw4441">collapse - 合并边框</div>
      <table class="collapse-nw4441">
        <tr>
          <th>列标题1</th>
          <th>列标题2</th>
          <th>列标题3</th>
        </tr>
        <tr>
          <td>数据1</td>
          <td>数据2</td>
          <td>数据3</td>
        </tr>
        <tr>
          <td>数据4</td>
          <td>数据5</td>
          <td>数据6</td>
        </tr>
      </table>
    </div>
  </div>
</html>
<style>
.table-collapse-demo-nw4441 {
  display: grid;
  gap: 30px;
}
.table-wrapper-nw4441 {
  background: #f9f9f9;
  padding: 20px;
  border-radius: 8px;
}
.label-nw4441 {
  font-size: 12px;
  font-weight: 600;
  color: #666;
  margin-bottom: 10px;
}
.table-collapse-demo-nw4441 table {
  width: 100%;
  background: white;
}
.table-collapse-demo-nw4441 th,
.table-collapse-demo-nw4441 td {
  padding: 10px;
  text-align: left;
  border: 2px solid #4CAF50;
}
.table-collapse-demo-nw4441 th {
  background: #f0f8ff;
  font-weight: 600;
}
.separate-nw4441 {
  border-collapse: separate;
  border-spacing: 5px;
}
.collapse-nw4441 {
  border-collapse: collapse;
}
</style>

# 3.2 border-spacing(边框间距)

仅在 border-collapse: separate 时有效。

/* 单个值(水平和垂直间距相同) */
table {
  border-collapse: separate;
  border-spacing: 10px;
}

/* 两个值(水平 垂直) */
table {
  border-collapse: separate;
  border-spacing: 10px 5px;
}

/* 0间距 */
table {
  border-collapse: separate;
  border-spacing: 0;
}
<html>
  <div class="border-spacing-demo-r8p5n2">
    <div class="spacing-section-r8p5n2">
      <div class="title-r8p5n2">border-spacing: 0</div>
      <table class="spacing-table-r8p5n2 spacing-0-r8p5n2">
        <tr>
          <td>单元格 1</td>
          <td>单元格 2</td>
        </tr>
        <tr>
          <td>单元格 3</td>
          <td>单元格 4</td>
        </tr>
      </table>
    </div>
    <div class="spacing-section-r8p5n2">
      <div class="title-r8p5n2">border-spacing: 10px</div>
      <table class="spacing-table-r8p5n2 spacing-10-r8p5n2">
        <tr>
          <td>单元格 1</td>
          <td>单元格 2</td>
        </tr>
        <tr>
          <td>单元格 3</td>
          <td>单元格 4</td>
        </tr>
      </table>
    </div>
    <div class="spacing-section-r8p5n2">
      <div class="title-r8p5n2">border-spacing: 15px 5px</div>
      <table class="spacing-table-r8p5n2 spacing-custom-r8p5n2">
        <tr>
          <td>单元格 1</td>
          <td>单元格 2</td>
        </tr>
        <tr>
          <td>单元格 3</td>
          <td>单元格 4</td>
        </tr>
      </table>
    </div>
  </div>
</html>
<style>
.border-spacing-demo-r8p5n2 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}
.spacing-section-r8p5n2 {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.title-r8p5n2 {
  font-size: 13px;
  font-weight: 600;
  color: #667eea;
}
.spacing-table-r8p5n2 {
  width: 100%;
  border-collapse: separate;
  background: white;
}
.spacing-table-r8p5n2 td {
  padding: 10px;
  border: 2px solid #667eea;
  text-align: center;
  font-size: 13px;
}
.spacing-0-r8p5n2 {
  border-spacing: 0;
}
.spacing-10-r8p5n2 {
  border-spacing: 10px;
}
.spacing-custom-r8p5n2 {
  border-spacing: 15px 5px;
}
</style>

# 3.3 table-layout(表格布局算法)

/* 自动布局(默认) */
table {
  table-layout: auto;
  /* 根据内容自动调整列宽,可能较慢 */
}

/* 固定布局 */
table {
  table-layout: fixed;
  width: 100%;
  /* 根据第一行确定列宽,渲染更快 */
}

/* 固定布局 + 明确列宽 */
table {
  table-layout: fixed;
  width: 100%;
}

table col:nth-child(1) {
  width: 30%;
}

table col:nth-child(2) {
  width: 50%;
}

table col:nth-child(3) {
  width: 20%;
}
<html>
  <div class="table-layout-demo-j9t6w4">
    <div class="layout-section-j9t6w4">
      <div class="title-j9t6w4">auto (默认) - 根据内容调整</div>
      <table class="layout-table-j9t6w4 auto-j9t6w4">
        <tr>
          <td>短</td>
          <td>这是一段比较长的文本内容</td>
          <td>中等</td>
        </tr>
        <tr>
          <td>A</td>
          <td>B</td>
          <td>C</td>
        </tr>
      </table>
    </div>
    <div class="layout-section-j9t6w4">
      <div class="title-j9t6w4">fixed - 固定布局,平均分配</div>
      <table class="layout-table-j9t6w4 fixed-j9t6w4">
        <tr>
          <td>短</td>
          <td>这是一段比较长的文本内容</td>
          <td>中等</td>
        </tr>
        <tr>
          <td>A</td>
          <td>B</td>
          <td>C</td>
        </tr>
      </table>
    </div>
  </div>
</html>
<style>
.table-layout-demo-j9t6w4 {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.layout-section-j9t6w4 {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.title-j9t6w4 {
  font-size: 13px;
  font-weight: 600;
  color: #667eea;
}
.layout-table-j9t6w4 {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border: 2px solid #e9ecef;
}
.layout-table-j9t6w4 td {
  padding: 12px;
  border: 1px solid #e9ecef;
  font-size: 13px;
  text-align: center;
}
.auto-j9t6w4 {
  table-layout: auto;
}
.fixed-j9t6w4 {
  table-layout: fixed;
}
</style>

# 3.4 caption-side(标题位置)

/* 标题在顶部(默认) */
table {
  caption-side: top;
}

/* 标题在底部 */
table {
  caption-side: bottom;
}

/* 样式化标题 */
caption {
  caption-side: top;
  padding: 10px;
  font-size: 1.2em;
  font-weight: bold;
  text-align: left;
  background: #f5f5f5;
}
<html>
  <div class="caption-demo-x7v2q5">
    <div class="caption-section-x7v2q5">
      <table class="caption-table-x7v2q5 top-x7v2q5">
        <caption>标题在顶部 (top)</caption>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
        </tr>
        <tr>
          <td>张三</td>
          <td>25</td>
        </tr>
        <tr>
          <td>李四</td>
          <td>30</td>
        </tr>
      </table>
    </div>
    <div class="caption-section-x7v2q5">
      <table class="caption-table-x7v2q5 bottom-x7v2q5">
        <caption>标题在底部 (bottom)</caption>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
        </tr>
        <tr>
          <td>张三</td>
          <td>25</td>
        </tr>
        <tr>
          <td>李四</td>
          <td>30</td>
        </tr>
      </table>
    </div>
  </div>
</html>
<style>
.caption-demo-x7v2q5 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.caption-table-x7v2q5 {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border: 2px solid #e9ecef;
}
.caption-table-x7v2q5 caption {
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  background: #f8f9fa;
  color: #667eea;
}
.caption-table-x7v2q5 th,
.caption-table-x7v2q5 td {
  padding: 10px;
  border: 1px solid #e9ecef;
  font-size: 13px;
  text-align: center;
}
.caption-table-x7v2q5 th {
  background: #667eea;
  color: white;
  font-weight: 600;
}
.top-x7v2q5 caption {
  caption-side: top;
}
.bottom-x7v2q5 caption {
  caption-side: bottom;
}
</style>

# 3.5 empty-cells(空单元格)

仅在 border-collapse: separate 时有效。

/* 显示空单元格边框(默认) */
table {
  empty-cells: show;
}

/* 隐藏空单元格边框 */
table {
  empty-cells: hide;
}
<html>
  <div class="empty-cells-demo-n3k8m1">
    <div class="empty-section-n3k8m1">
      <div class="title-n3k8m1">show (默认) - 显示空单元格</div>
      <table class="empty-table-n3k8m1 show-n3k8m1">
        <tr>
          <td>有内容</td>
          <td></td>
          <td>有内容</td>
        </tr>
        <tr>
          <td></td>
          <td>有内容</td>
          <td></td>
        </tr>
      </table>
    </div>
    <div class="empty-section-n3k8m1">
      <div class="title-n3k8m1">hide - 隐藏空单元格边框</div>
      <table class="empty-table-n3k8m1 hide-n3k8m1">
        <tr>
          <td>有内容</td>
          <td></td>
          <td>有内容</td>
        </tr>
        <tr>
          <td></td>
          <td>有内容</td>
          <td></td>
        </tr>
      </table>
    </div>
  </div>
</html>
<style>
.empty-cells-demo-n3k8m1 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.empty-section-n3k8m1 {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.title-n3k8m1 {
  font-size: 13px;
  font-weight: 600;
  color: #667eea;
}
.empty-table-n3k8m1 {
  width: 100%;
  border-collapse: separate;
  border-spacing: 5px;
  background: #f8f9fa;
}
.empty-table-n3k8m1 td {
  padding: 15px;
  border: 2px solid #667eea;
  background: white;
  font-size: 13px;
  text-align: center;
}
.show-n3k8m1 {
  empty-cells: show;
}
.hide-n3k8m1 {
  empty-cells: hide;
}
</style>

# 3.6 vertical-align(垂直对齐)

在表格单元格中特别有用。

/* 顶部对齐 */
td {
  vertical-align: top;
}

/* 中间对齐(默认) */
td {
  vertical-align: middle;
}

/* 底部对齐 */
td {
  vertical-align: bottom;
}

/* 基线对齐 */
td {
  vertical-align: baseline;
}
<html>
  <div class="vertical-align-demo-q5h9w7">
    <table class="valign-table-q5h9w7">
      <thead>
        <tr>
          <th>对齐方式</th>
          <th>效果展示</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="label-q5h9w7">top</td>
          <td class="top-q5h9w7">
            <div class="tall-content-q5h9w7">
              顶部对齐的单元格内容。<br>
              这是第二行文本。<br>
              这是第三行文本。
            </div>
          </td>
        </tr>
        <tr>
          <td class="label-q5h9w7">middle</td>
          <td class="middle-q5h9w7">
            <div class="tall-content-q5h9w7">
              中间对齐的单元格内容(默认)。<br>
              这是第二行文本。<br>
              这是第三行文本。
            </div>
          </td>
        </tr>
        <tr>
          <td class="label-q5h9w7">bottom</td>
          <td class="bottom-q5h9w7">
            <div class="tall-content-q5h9w7">
              底部对齐的单元格内容。<br>
              这是第二行文本。<br>
              这是第三行文本。
            </div>
          </td>
        </tr>
        <tr>
          <td class="label-q5h9w7">baseline</td>
          <td class="baseline-q5h9w7">
            <div class="tall-content-q5h9w7">
              基线对齐的单元格内容。<br>
              这是第二行文本。<br>
              这是第三行文本。
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</html>
<style>
.vertical-align-demo-q5h9w7 {
  width: 100%;
}
.valign-table-q5h9w7 {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border: 2px solid #e9ecef;
}
.valign-table-q5h9w7 th {
  padding: 12px;
  background: #667eea;
  color: white;
  font-weight: 600;
  font-size: 14px;
  text-align: left;
  border: 1px solid #5568d3;
}
.valign-table-q5h9w7 td {
  padding: 15px;
  border: 1px solid #e9ecef;
  font-size: 13px;
}
.label-q5h9w7 {
  font-weight: 600;
  color: #667eea;
  width: 120px;
}
.tall-content-q5h9w7 {
  min-height: 80px;
  line-height: 1.6;
}
.top-q5h9w7 {
  vertical-align: top;
}
.middle-q5h9w7 {
  vertical-align: middle;
}
.bottom-q5h9w7 {
  vertical-align: bottom;
}
.baseline-q5h9w7 {
  vertical-align: baseline;
}
</style>

# 四、实战案例

# 4.1 现代表格样式

<html>
  <div class="table-container">
    <table class="modern-table">
      <caption>用户列表</caption>
      <thead>
        <tr>
          <th>ID</th>
          <th>姓名</th>
          <th>邮箱</th>
          <th>角色</th>
          <th>状态</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>001</td>
          <td>张三</td>
          <td>zhangsan@example.com</td>
          <td>管理员</td>
          <td><span class="badge badge-success">活跃</span></td>
        </tr>
        <tr>
          <td>002</td>
          <td>李四</td>
          <td>lisi@example.com</td>
          <td>编辑</td>
          <td><span class="badge badge-success">活跃</span></td>
        </tr>
        <tr>
          <td>003</td>
          <td>王五</td>
          <td>wangwu@example.com</td>
          <td>访客</td>
          <td><span class="badge badge-warning">待审核</span></td>
        </tr>
        <tr>
          <td>004</td>
          <td>赵六</td>
          <td>zhaoliu@example.com</td>
          <td>编辑</td>
          <td><span class="badge badge-danger">禁用</span></td>
        </tr>
      </tbody>
    </table>
  </div>
</html>
<style>
  .table-container {
    overflow-x: auto;
  }
  
  .modern-table {
    width: 100%;
    border-collapse: collapse;
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    overflow: hidden;
  }
  
  .modern-table caption {
    caption-side: top;
    padding: 15px;
    font-size: 1.2rem;
    font-weight: 600;
    text-align: left;
    background: #f8f9fa;
    border-bottom: 2px solid #e9ecef;
  }
  
  .modern-table thead {
    background: linear-gradient(to bottom, #667eea 0%, #764ba2 100%);
    color: white;
  }
  
  .modern-table th {
    padding: 12px 15px;
    text-align: left;
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.85rem;
    letter-spacing: 0.5px;
  }
  
  .modern-table tbody tr {
    border-bottom: 1px solid #e9ecef;
    transition: all 0.3s ease;
  }
  
  .modern-table tbody tr:hover {
    background: #f8f9fa;
    transform: scale(1.01);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  }
  
  .modern-table tbody tr:last-child {
    border-bottom: none;
  }
  
  .modern-table td {
    padding: 12px 15px;
    color: #333;
  }
  
  .badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    text-align: center;
    min-width: 60px;
  }
  
  .badge-success {
    background: #d4edda;
    color: #155724;
  }
  
  .badge-warning {
    background: #fff3cd;
    color: #856404;
  }
  
  .badge-danger {
    background: #f8d7da;
    color: #721c24;
  }
</style>

# 4.2 斑马纹表格

<html>
  <table class="striped-table">
    <thead>
      <tr>
        <th>产品</th>
        <th>价格</th>
        <th>库存</th>
        <th>销量</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>iPhone 15 Pro</td>
        <td>¥8,999</td>
        <td>120</td>
        <td>856</td>
      </tr>
      <tr>
        <td>MacBook Pro</td>
        <td>¥14,999</td>
        <td>45</td>
        <td>324</td>
      </tr>
      <tr>
        <td>AirPods Pro</td>
        <td>¥1,899</td>
        <td>380</td>
        <td>1,567</td>
      </tr>
      <tr>
        <td>iPad Air</td>
        <td>¥4,799</td>
        <td>210</td>
        <td>723</td>
      </tr>
      <tr>
        <td>Apple Watch</td>
        <td>¥3,199</td>
        <td>156</td>
        <td>945</td>
      </tr>
    </tbody>
  </table>
</html>
<style>
  .striped-table {
    width: 100%;
    border-collapse: collapse;
  }
  
  .striped-table thead {
    background: #2c3e50;
    color: white;
  }
  
  .striped-table th {
    padding: 12px;
    text-align: left;
    font-weight: 600;
  }
  
  .striped-table td {
    padding: 12px;
    border-bottom: 1px solid #ecf0f1;
  }
  
  /* 斑马纹 */
  .striped-table tbody tr:nth-child(odd) {
    background: #ffffff;
  }
  
  .striped-table tbody tr:nth-child(even) {
    background: #f8f9fa;
  }
  
  .striped-table tbody tr:hover {
    background: #e3f2fd;
  }
</style>

# 4.3 响应式表格

<html>
  <div class="responsive-table-wrapper">
    <table class="responsive-table">
      <thead>
        <tr>
          <th>日期</th>
          <th>订单号</th>
          <th>商品</th>
          <th>数量</th>
          <th>金额</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td data-label="日期">2024-01-15</td>
          <td data-label="订单号">ORD-2024-001</td>
          <td data-label="商品">无线耳机</td>
          <td data-label="数量">2</td>
          <td data-label="金额">¥398</td>
        </tr>
        <tr>
          <td data-label="日期">2024-01-16</td>
          <td data-label="订单号">ORD-2024-002</td>
          <td data-label="商品">机械键盘</td>
          <td data-label="数量">1</td>
          <td data-label="金额">¥599</td>
        </tr>
        <tr>
          <td data-label="日期">2024-01-17</td>
          <td data-label="订单号">ORD-2024-003</td>
          <td data-label="商品">鼠标垫</td>
          <td data-label="数量">3</td>
          <td data-label="金额">¥89</td>
        </tr>
      </tbody>
    </table>
  </div>
</html>
<style>
  .responsive-table-wrapper {
    overflow-x: auto;
  }
  
  .responsive-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 500px;
  }
  
  .responsive-table thead {
    background: #3498db;
    color: white;
  }
  
  .responsive-table th,
  .responsive-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #ddd;
  }
  
  .responsive-table tbody tr:hover {
    background: #f5f5f5;
  }
  
  /* 小屏幕:卡片式布局 */
  @media (max-width: 600px) {
    .responsive-table {
      min-width: 100%;
    }
    
    .responsive-table thead {
      display: none;
    }
    
    .responsive-table tbody tr {
      display: block;
      margin-bottom: 15px;
      border: 1px solid #ddd;
      border-radius: 8px;
      overflow: hidden;
    }
    
    .responsive-table td {
      display: flex;
      justify-content: space-between;
      padding: 10px 15px;
      border-bottom: 1px solid #f0f0f0;
    }
    
    .responsive-table td:last-child {
      border-bottom: none;
    }
    
    .responsive-table td::before {
      content: attr(data-label);
      font-weight: 600;
      color: #3498db;
    }
  }
</style>

# 4.4 自定义列表导航

<html>
  <nav class="custom-nav">
    <ul class="nav-list">
      <li class="nav-item active">
        <a href="#home">首页</a>
      </li>
      <li class="nav-item">
        <a href="#products">产品</a>
      </li>
      <li class="nav-item">
        <a href="#about">关于</a>
      </li>
      <li class="nav-item">
        <a href="#contact">联系</a>
      </li>
    </ul>
  </nav>
  
  <div class="feature-list">
    <h3>产品特性</h3>
    <ul class="features">
      <li class="feature-item">高性能处理器,运行流畅</li>
      <li class="feature-item">超长续航,持久使用</li>
      <li class="feature-item">专业级相机系统</li>
      <li class="feature-item">5G全网通支持</li>
    </ul>
  </div>
</html>
<style>
  .custom-nav {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 0;
    border-radius: 8px;
    margin-bottom: 30px;
  }
  
  .nav-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    gap: 0;
  }
  
  .nav-item {
    flex: 1;
  }
  
  .nav-item a {
    display: block;
    padding: 15px 20px;
    color: white;
    text-decoration: none;
    text-align: center;
    font-weight: 500;
    transition: all 0.3s ease;
    position: relative;
  }
  
  .nav-item a::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 0;
    height: 3px;
    background: white;
    transform: translateX(-50%);
    transition: width 0.3s ease;
  }
  
  .nav-item:hover a,
  .nav-item.active a {
    background: rgba(255, 255, 255, 0.1);
  }
  
  .nav-item:hover a::after,
  .nav-item.active a::after {
    width: 80%;
  }
  
  /* 特性列表 */
  .feature-list {
    background: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
  
  .feature-list h3 {
    margin: 0 0 15px 0;
    color: #333;
  }
  
  .features {
    list-style: none;
    padding: 0;
    margin: 0;
  }
  
  .feature-item {
    padding: 12px 12px 12px 40px;
    position: relative;
    margin-bottom: 10px;
    background: #f8f9fa;
    border-radius: 6px;
    transition: all 0.3s ease;
  }
  
  .feature-item:last-child {
    margin-bottom: 0;
  }
  
  .feature-item::before {
    content: '✓';
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    background: #28a745;
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
  }
  
  .feature-item:hover {
    background: #e3f2fd;
    transform: translateX(5px);
  }
</style>

# 4.5 渐变背景卡片

<html>
  <div class="gradient-cards">
    <div class="card card-1">
      <h3>线性渐变</h3>
      <p>从左上到右下的渐变效果</p>
    </div>
    <div class="card card-2">
      <h3>径向渐变</h3>
      <p>从中心向外扩散的渐变</p>
    </div>
    <div class="card card-3">
      <h3>多重背景</h3>
      <p>渐变 + 图案组合</p>
    </div>
    <div class="card card-4">
      <h3>动画渐变</h3>
      <p>渐变背景动画效果</p>
    </div>
  </div>
</html>
<style>
  .gradient-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
  }
  
  .card {
    padding: 30px 20px;
    border-radius: 12px;
    color: white;
    text-align: center;
    transition: transform 0.3s ease;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  }
  
  .card:hover {
    transform: translateY(-5px);
  }
  
  .card h3 {
    margin: 0 0 10px 0;
    font-size: 1.2rem;
  }
  
  .card p {
    margin: 0;
    font-size: 0.9rem;
    opacity: 0.9;
  }
  
  /* 线性渐变 */
  .card-1 {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  }
  
  /* 径向渐变 */
  .card-2 {
    background: radial-gradient(circle at top right, #f093fb 0%, #f5576c 100%);
  }
  
  /* 多重背景 */
  .card-3 {
    background: 
      repeating-linear-gradient(
        45deg,
        transparent,
        transparent 10px,
        rgba(255,255,255,.1) 10px,
        rgba(255,255,255,.1) 20px
      ),
      linear-gradient(to bottom, #4facfe 0%, #00f2fe 100%);
  }
  
  /* 动画渐变 */
  .card-4 {
    background: linear-gradient(
      -45deg,
      #ee7752,
      #e73c7e,
      #23a6d5,
      #23d5ab
    );
    background-size: 400% 400%;
    animation: gradient-shift 3s ease infinite;
  }
  
  @keyframes gradient-shift {
    0% {
      background-position: 0% 50%;
    }
    50% {
      background-position: 100% 50%;
    }
    100% {
      background-position: 0% 50%;
    }
  }
</style>

# 五、最佳实践

# 5.1 背景图片优化

/* 1. 使用合适的图片格式 */
.hero {
  /* WebP优先,JPEG备用 */
  background-image: 
    image-set(
      url('hero.webp') type('image/webp'),
      url('hero.jpg') type('image/jpeg')
    );
}

/* 2. 响应式背景图片 */
.hero {
  background-image: url('hero-mobile.jpg');
}

@media (min-width: 768px) {
  .hero {
    background-image: url('hero-tablet.jpg');
  }
}

@media (min-width: 1200px) {
  .hero {
    background-image: url('hero-desktop.jpg');
  }
}

/* 3. 优化性能的完整配置 */
.hero {
  background: url('hero.jpg') center/cover no-repeat fixed;
  background-color: #f0f0f0; /* 备用颜色 */
  min-height: 500px;
  
  /* 图片加载期间显示颜色 */
  will-change: background-image;
}

/* 4. 懒加载背景图片 */
.lazy-bg {
  background-color: #f0f0f0;
  /* JavaScript动态添加背景图片 */
}

.lazy-bg.loaded {
  background-image: url('image.jpg');
  transition: background-image 0.3s ease;
}

# 5.2 列表语义化

/* ✅ 推荐:保留语义,用CSS移除样式 */
nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

/* ❌ 不推荐:使用div破坏语义 */
/* <div class="nav-container">
     <div class="nav-item">...</div>
   </div> */

/* ✅ 推荐:使用ol表示顺序重要的列表 */
.steps {
  list-style: decimal;
}

/* ✅ 推荐:使用ul表示顺序不重要的列表 */
.features {
  list-style: disc;
}

/* ✅ 推荐:使用自定义计数器增强语义 */
.tutorial-steps {
  list-style: none;
  counter-reset: step;
}

.tutorial-steps li::before {
  content: "第 " counter(step) " 步: ";
  counter-increment: step;
  font-weight: bold;
  color: #007bff;
}

# 5.3 表格可访问性

/* 1. 为表格添加标题 */
table {
  caption-side: top;
}

caption {
  padding: 10px;
  font-weight: bold;
  text-align: left;
}

/* 2. 突出表头 */
th {
  background: #f0f0f0;
  font-weight: 600;
  text-align: left;
  padding: 12px;
}

/* 3. 增强可读性 */
td {
  padding: 12px;
  border-bottom: 1px solid #e0e0e0;
}

/* 4. 悬浮高亮 */
tbody tr:hover {
  background: #f5f5f5;
  cursor: pointer;
}

/* 5. 焦点样式(键盘导航) */
table:focus-within tr:focus,
table:focus-within td:focus {
  outline: 2px solid #007bff;
  outline-offset: -2px;
}

/* 6. 确保对比度 */
th {
  color: #1a1a1a;
  background: #e9ecef;
}

td {
  color: #333;
}

# 5.4 性能优化

/* 1. 避免复杂的渐变 */
/* ❌ 性能差 */
.bad {
  background: 
    radial-gradient(circle, red 0%, blue 10%, green 20%),
    linear-gradient(45deg, yellow, pink),
    conic-gradient(orange, purple);
}

/* ✅ 性能好 */
.good {
  background: linear-gradient(to right, #667eea, #764ba2);
}

/* 2. 使用will-change提示浏览器 */
.animated-bg {
  will-change: background-position;
  background: url('pattern.png');
  animation: slide 10s linear infinite;
}

@keyframes slide {
  from { background-position: 0 0; }
  to { background-position: 100px 0; }
}

/* 3. 固定布局提升表格渲染 */
table {
  table-layout: fixed;
  width: 100%;
}

/* 4. 避免background-attachment: fixed在移动端 */
@media (max-width: 768px) {
  .parallax {
    background-attachment: scroll; /* 移动端性能更好 */
  }
}

# 5.5 常见错误

❌ 错误做法:

/* 1. 忘记设置背景色(图片加载失败时无备用) */
.hero {
  background-image: url('hero.jpg');
  color: white; /* 如果图片加载失败,白字白底不可见 */
}

/* 2. 表格边框重复 */
table {
  border: 1px solid #ddd;
}
td {
  border: 1px solid #ddd; /* 造成双边框 */
}

/* 3. 列表样式不一致 */
ul {
  list-style-position: outside;
}
ul ul {
  list-style-position: inside; /* 嵌套列表样式不一致 */
}

/* 4. 背景图片过大 */
.bg {
  background: url('huge-image.jpg'); /* 5MB图片 */
}

✅ 正确做法:

/* 1. 始终提供背景色 */
.hero {
  background-color: #333;
  background-image: url('hero.jpg');
  color: white;
}

/* 2. 使用border-collapse */
table {
  border-collapse: collapse;
  border: 1px solid #ddd;
}
td {
  border: 1px solid #ddd;
}

/* 3. 保持样式一致 */
ul,
ul ul {
  list-style-position: outside;
  padding-left: 2em;
}

/* 4. 优化图片 */
.bg {
  background: url('optimized-image.webp'); /* 压缩后 < 200KB */
  background-size: cover;
}

# 六、浏览器兼容性

特性 Chrome Firefox Safari Edge
background 基础 ✅ 全部 ✅ 全部 ✅ 全部 ✅ 全部
多重背景 ✅ 4+ ✅ 3.6+ ✅ 3.1+ ✅ 12+
background-size ✅ 4+ ✅ 4+ ✅ 4.1+ ✅ 12+
background-clip: text ✅ 3+ (-webkit-) ✅ 49+ ✅ 4+ (-webkit-) ✅ 15+
渐变 ✅ 26+ ✅ 16+ ✅ 6.1+ ✅ 12+
conic-gradient ✅ 69+ ✅ 83+ ✅ 12.1+ ✅ 79+
list-style ✅ 全部 ✅ 全部 ✅ 全部 ✅ 全部
table-layout ✅ 全部 ✅ 全部 ✅ 全部 ✅ 全部
border-collapse ✅ 全部 ✅ 全部 ✅ 全部 ✅ 全部

# 前缀处理

/* 渐变文字(需要前缀) */
.gradient-text {
  background: linear-gradient(45deg, red, blue);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* 旧版本浏览器渐变语法 */
.gradient {
  /* 旧版WebKit */
  background: -webkit-gradient(linear, left top, right top, from(#667eea), to(#764ba2));
  
  /* 旧版标准 */
  background: -webkit-linear-gradient(left, #667eea, #764ba2);
  background: -moz-linear-gradient(left, #667eea, #764ba2);
  
  /* 现代标准 */
  background: linear-gradient(to right, #667eea, #764ba2);
}

# 七、总结

# 核心要点

  1. 背景属性:

    • 使用 background 简写提高代码简洁性
    • background-size: cover 常用于全屏背景
    • 多重背景从上到下叠加
    • 渐变是 background-image 的值
  2. 列表样式:

    • list-style-type 改变标记样式
    • 导航菜单通常 list-style: none
    • 使用伪元素 ::before 实现自定义标记
    • CSS计数器增强列表语义
  3. 表格样式:

    • border-collapse: collapse 消除双边框
    • table-layout: fixed 提升性能
    • 斑马纹使用 :nth-child(odd/even)
    • 响应式表格在小屏幕转为卡片布局
  4. 最佳实践:

    • 背景图片始终提供备用颜色
    • 优化图片大小和格式
    • 保持列表语义化
    • 增强表格可访问性(标题、对比度)
  5. 性能优化:

    • 压缩背景图片
    • 避免过于复杂的渐变
    • 使用 table-layout: fixed
    • 移动端避免 background-attachment: fixed

掌握背景、列表和表格的CSS样式,能够让你的网页内容组织更清晰、视觉效果更出色。

祝你变得更强!

编辑 (opens new window)
#CSS
上次更新: 2025/11/21
CSS基础-文本与字体
CSS布局-Flexbox完全指南

← CSS基础-文本与字体 CSS布局-Flexbox完全指南→

最近更新
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
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式