BFC解决margin折叠问题

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- BFC解决margin折叠问题 -->
    <!-- 问题:margin折叠,如果one,two直接用会处于“同一个” BFC-->
     <!-- 同一个BFC上下相邻的盒子margin会自动折叠 -->
      <!-- 解决方法:创建一个新的BFC container -->
       <!-- 错解:直接在two上加overflow,他只是在two内部创建bfc,自己没有身处新的bfc -->
    <div class="container">
      <div class="one"></div>
    </div>
    <div class="two"></div>
  </body>
  <style>
    .one {
      height: 100px;
      background-color: aqua;
      margin-bottom: 50px;
    }
    .two {
      height: 100px;
      background-color: red;
      margin-top: 50px;
    }
    .container {
      overflow: auto;
    }
  </style>
</html>

 

posted @ 2025-04-19 18:49  zxlstart  阅读(10)  评论(0)    收藏  举报