一、CSS文字环绕基础

1.1 浮动布局

img {
  float: left;
  margin-right: 10px;
}

1.2 盒子模型

img {
  width: 200px;
  margin-right: 10px;
}

二、CSS文字环绕高级技巧

2.1 使用clear属性

.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

2.2 使用shape-outside属性

shape-outside属性是CSS3中新增的一个属性,它允许我们定义一个形状,文字将围绕这个形状进行排列。

img {
  shape-outside: polygon(50% 0%, 100% 50%, 0% 50%);
  clip-path: polygon(50% 0%, 100% 50%, 0% 50%);
}

2.3 使用column属性

.container {
  column-count: 2;
  column-gap: 20px;
}

三、案例实践

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文字环绕效果示例</title>
  <style>
    .container {
      width: 600px;
      margin: 0 auto;
      font-size: 16px;
      line-height: 1.5;
    }
    img {
      float: left;
      width: 200px;
      margin-right: 20px;
    }
    .clearfix::after {
      content: "";
      display: block;
      clear: both;
    }
  </style>
</head>
<body>
  <div class="container clearfix">
    <img src="example.jpg" alt="示例图片">
    <p>这是一段文字描述,这段文字将环绕图片进行排版,以实现图文并茂的效果。</p>
  </div>
</body>
</html>

四、总结