美文网首页
css float left换行不正常问题

css float left换行不正常问题

作者: 菜菜___ | 来源:发表于2022-05-05 16:34 被阅读0次

理想效果如下:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .block {
      width: 25%;
      padding: 10px;
      float: left;
      box-sizing: border-box;

      }
      .block div {
      background-color: pink;
      width: 100%;
      height: 280px;
      }
      #special {
      height: 280px;
      background-color: green;
    }
  </style>
</head>
<body>
  <div class="block">
    <div></div>
  </div>
  <div class="block">
      <div></div>
  </div>
  <div class="block">
      <div></div>
  </div>
  <div class="block" >
      <div id="special"></div>
  </div>
  <div class="block">
      <div></div>
  </div>
  <div class="block">
      <div></div>
  </div>
</body>
</html>

但当special的height小于280px时就会变成这样



当第一个div的height大于280px时就会变成这样


image.png
float的定义:浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动的边框为止。
也就是本来第二行的想float到左边(第一个div下方),然后碰上了第三个div,然后就停下了。第二行后面的就被挤到第三行了。

相关文章

网友评论

      本文标题:css float left换行不正常问题

      本文链接:https://www.haomeiwen.com/subject/ivfoyrtx.html