← CSS background-position 属性 CSS border-box 属性 →

CSS 伪元素::before与::after详解

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

CSS伪元素::before与::after的动画应用详解

为什么使用动画效果?

在网页设计中添加适当的动画效果能够创建用户与页面之间的微交互体验。精心设计的动画不仅能吸引用户注意力,还能引导用户关注页面核心内容,提升用户体验和参与度。

理解CSS伪元素

伪元素是CSS中的特殊选择器,允许开发者向网页添加装饰性内容而不影响实际HTML结构。常用的伪元素包括::before::after::placeholder::first-letter等。本教程将重点讲解::before::after的应用。

::before伪元素

此伪元素用于在选定元素前插入内容,成为该元素的第一个子元素。

语法结构:

selector::before {
  content: "需要插入的内容或值";
  /* 其他样式属性 */
}

实践示例:装饰标题元素

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - CSS伪元素示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .enhanced-heading {
      position: relative;
      font-size: 32px;
      color: #333;
      padding-left: 30px;
    }
    
    .enhanced-heading::before {
      content: "";
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      width: 8px;
      height: 70%;
      background: linear-gradient(to bottom, #4a90e2, #5bc0de);
      border-radius: 4px;
    }
  </style>
</head>
<body>
  <h1 class="enhanced-heading">欢迎来到代码号学习编程</h1>
</body>
</html>

实现解析:
此示例使用::before伪元素在标题左侧创建了一个垂直渐变条。通过position: absolute实现精确定位,transform: translateY(-50%)确保元素垂直居中。

::after伪元素

此伪元素用于在选定元素后插入内容,成为该元素的一个子元素。

语法结构:

css

selector::after {
  content: ""; /* 此项必需,即使为空 */
  /* 其他样式属性 */
}

实践示例:段落装饰线

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - 装饰性段落示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f9f9f9;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .decorative-paragraph {
      position: relative;
      font-size: 18px;
      color: #444;
      line-height: 1.6;
      margin: 20px;
      padding-bottom: 15px;
    }
    
    .decorative-paragraph::after {
      content: "";
      display: block;
      width: 60px;
      height: 3px;
      background-color: #ff6b6b;
      margin-top: 15px;
      border-radius: 2px;
    }
  </style>
</head>
<body>
  <p class="decorative-paragraph">这是一个带有装饰元素的段落,通过CSS伪元素实现下方装饰线效果。</p>
</body>
</html>

实现解析:
此示例使用::after伪元素在段落下方添加了一条装饰性水平线。display: block确保伪元素独占一行,margin-top控制与文本的间距。

伪元素与伪类的区别

虽然名称相似,但伪元素和伪类在CSS中有明显区别:

  • 伪元素:用于创建并样式化文档中不在DOM中的抽象元素,以双冒号(::)开头

  • 伪类:用于选择处于特定状态的元素,以单冒号(:)开头

伪元素应用示例:首字母装饰

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - 首字母装饰示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f0f0f0;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .decorative-letter {
      font-size: 18px;
      line-height: 1.6;
      color: #333;
      max-width: 500px;
    }
    
    .decorative-letter::first-letter {
      font-size: 48px;
      color: #e74c3c;
      float: left;
      margin: 0 8px 0 0;
      font-weight: bold;
      line-height: 1;
    }
  </style>
</head>
<body>
  <p class="decorative-letter">从前,在一个遥远的编程世界里,开发者们使用CSS伪元素创建精美的视觉效果。通过简单的代码,可以实现令人印象深刻的页面装饰效果。</p>
</body>
</html>

伪类应用示例:图片悬停效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - 图片悬停效果示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .image-wrapper {
      position: relative;
      overflow: hidden;
      width: 300px;
      height: 200px;
      border-radius: 8px;
      box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    
    .image-wrapper img {
      width: 100%;
      height: 100%;
      transition: transform 0.4s ease;
      object-fit: cover;
    }
    
    .image-wrapper:hover img {
      transform: scale(1.08);
    }
  </style>
</head>
<body>
  <div class="image-wrapper">
    <img src="https://www.ebingou.cn/biancheng/images/1.jpg" alt="编程学习示例图片">
  </div>
</body>
</html>

常用伪类分类:

  • 输入元素状态::checked

  • 语言相关状态::lang()

  • 链接访问状态::visited

  • 媒体资源状态::playing

  • 时间维度状态::current

  • DOM位置状态::first-child

  • 用户交互状态::hover

使用伪元素创建动画效果

通过结合CSS伪元素与动画属性,可以创建引人注目的动态效果。实现动画效果需要掌握以下关键技术:

CSS变换(Transform)

此属性允许对元素进行旋转、缩放、倾斜和平移等操作。

CSS过渡(Transition)

用于定义元素从一种状态平滑过渡到另一种状态的动画效果。

定位(Position)

控制伪元素在文档中的位置,通常使用相对或绝对定位。

层叠顺序(Z-index)

控制定位元素的堆叠顺序,数值越大显示层级越高。

CSS变换与过渡详解

CSS变换属性允许对元素进行各种几何变换,结合过渡属性可创建平滑动画效果。

示例:卡片翻转动画

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - 卡片翻转动画示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f0f0f0;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .flip-card {
      perspective: 1000px;
      width: 250px;
      height: 350px;
    }
    
    .flip-card-inner {
      width: 100%;
      height: 100%;
      transition: transform 0.6s;
      transform-style: preserve-3d;
      position: relative;
    }
    
    .flip-card:hover .flip-card-inner {
      transform: rotateY(180deg);
    }
    
    .flip-card-front, .flip-card-back {
      position: absolute;
      width: 100%;
      height: 100%;
      backface-visibility: hidden;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 12px;
      box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    }
    
    .flip-card-front {
      background-color: #4a90e2;
      color: white;
    }
    
    .flip-card-back {
      background-color: #f39c12;
      color: white;
      transform: rotateY(180deg);
    }
  </style>
</head>
<body>
  <div class="flip-card">
    <div class="flip-card-inner">
      <div class="flip-card-front">
        <h2>正面内容</h2>
      </div>
      <div class="flip-card-back">
        <h2>背面内容</h2>
      </div>
    </div>
  </div>
</body>
</html>

相对定位与绝对定位实战

CSS提供了多种定位方式控制元素在文档中的位置,其中相对定位和绝对定位最为常用。

相对定位实践

相对定位使元素相对于其正常位置进行偏移,但不影响其他元素的布局。

示例:相对定位元素偏移

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - 相对定位示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .container {
      width: 300px;
      height: 250px;
      border: 2px dashed #ccc;
      position: relative;
      padding: 20px;
    }
    
    .position-box {
      width: 100px;
      height: 100px;
      background-color: #3498db;
      color: white;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
      top: 30px;
      left: 40px;
      border-radius: 6px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="position-box">相对定位元素</div>
  </div>
</body>
</html>

实现解析:
此示例使用position: relative使蓝色方块相对于其原始位置向下偏移30像素,向右偏移40像素。

绝对定位实践

绝对定位将元素从正常文档流中移除,相对于最近的非静态定位祖先元素进行定位。

示例:图片文字叠加效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - 绝对定位示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f0f0f0;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .image-container {
      position: relative;
      width: 320px;
      height: 240px;
      overflow: hidden;
      border-radius: 8px;
    }
    
    .image-container img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    
    .image-overlay {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      background-color: rgba(0, 0, 0, 0.6);
      color: white;
      padding: 15px;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="image-container">
    <img src="https://www.ebingou.cn/biancheng/images/2.jpg" alt="编程学习示例">
    <div class="image-overlay">
      <h3>代码号学习编程示例</h3>
      <p>使用绝对定位实现文字叠加效果</p>
    </div>
  </div>
</body>
</html>

实现解析:
此示例使用position: absolute将文字叠加层定位在图片底部,通过半透明背景增强可读性。

使用Z-index控制元素层叠顺序

Z-index属性控制定位元素的堆叠顺序,数值越大显示层级越高。

示例:元素层叠效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - Z-index示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f9f9f9;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .stack-container {
      position: relative;
      width: 300px;
      height: 250px;
    }
    
    .stack-item {
      width: 120px;
      height: 120px;
      color: white;
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      border-radius: 8px;
      font-weight: bold;
      box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    }
    
    .item-1 {
      background-color: #3498db;
      top: 20px;
      left: 20px;
      z-index: 3;
    }
    
    .item-2 {
      background-color: #e74c3c;
      top: 60px;
      left: 80px;
      z-index: 2;
    }
    
    .item-3 {
      background-color: #27ae60;
      top: 100px;
      left: 140px;
      z-index: 1;
    }
  </style>
</head>
<body>
  <div class="stack-container">
    <div class="stack-item item-1">层级 3</div>
    <div class="stack-item item-2">层级 2</div>
    <div class="stack-item item-3">层级 1</div>
  </div>
</body>
</html>

实现解析:
此示例通过z-index属性控制三个方块的堆叠顺序,数值越大的元素显示在越上层。

创建动画按钮:伪元素实战

下面是一个使用伪元素创建动画按钮的完整示例:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>代码号学习编程 - 动画按钮示例</title>
  <style>
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .animated-btn {
      position: relative;
      background-color: #4a90e2;
      border: none;
      color: white;
      padding: 12px 28px;
      font-size: 16px;
      cursor: pointer;
      overflow: hidden;
      border-radius: 6px;
      transition: background-color 0.3s;
      z-index: 1;
    }
    
    .animated-btn:hover {
      background-color: #357abd;
    }
    
    .animated-btn::before {
      content: "";
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
      transition: left 0.5s;
      z-index: -1;
    }
    
    .animated-btn:hover::before {
      left: 100%;
    }
  </style>
</head>
<body>
  <button class="animated-btn">悬停查看效果</button>
</body>
</html>

实现解析:
此示例创建了一个带有流光效果的按钮。使用::before伪元素生成一个渐变遮罩层,通过悬停时的位置变化实现流光动画效果。

本节课程知识要点

  1. 伪元素允许向页面添加装饰性内容而不影响HTML结构

  2. ::before::after分别用于在元素前后插入内容

  3. 结合CSS变换和过渡属性可以创建平滑动画效果

  4. 正确定位和z-index控制是实现复杂布局的关键

  5. 动画效果应适度使用,以增强用户体验而非分散注意力

← CSS background-position 属性 CSS border-box 属性 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号