CSS系列 - flow-root

137 阅读1分钟

无论是内联元素,还是原本就是块级元素,都会变成块级元素,同时这个元素会建立新的块级格式上下文

  • 清除浮动
  • 去除 margin 合并现象
<!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>display: flow-root</title>
    <style>
      p {
        outline: solid deepskyblue;
      }
      img {
        float: left;
        width: 128px;
      }
      .flow-root {
        display: flow-root;
      }
      .box {
        background-color: #f0f3f9;
      }
      .box p {
        margin: 2em;
      }
    </style>
  </head>
  <body>
    <h4>1. 去除浮动</h4>
    <p><img src="https://image.zhangxinxu.com/image/study/s/s256/mm.jpg" /></p>
    <div style="clear: both"></div>
    <p class="flow-root">
      <img src="https://image.zhangxinxu.com/image/study/s/s256/mm.jpg" />
    </p>

    <h4>2. 去除margin合并</h4>
    <div class="box">
      <p>margin: 2em;</p>
    </div>
    <div class="box flow-root">
      <p>margin: 2em;</p>
    </div>
  </body>
</html>