★ BFC / 浮动

2020-06-08  本文已影响0人  行走的蛋白质


概念


触发 BFC


特性及应用

同一个 BFC 下元素外边距会发生折叠
<head>
div{
    width: 100px;
    height: 100px;
    background: lightblue;
    margin: 100px;
}
</head>
<body>
    <div></div>
    <div></div>
</body>
同一个 BFC 下元素外边距发生重叠
BFC 可以包含浮动的元素 ( 清除浮动 )
<div style="border: 1px solid #000;">
    <div style="width: 100px;height: 100px;background: #eee;float: left;"></div>
</div>
BFC 可以包含浮动的元素 ( 清除浮动 )
BFC 可以阻止元素被浮动元素覆盖
<div style="height: 100px;width: 100px;float: left;background: lightblue">我是一个左浮动的元素</div>
<div style="width: 200px; height: 200px;background: #eee">我是一个没有设置浮动, 
也没有触发 BFC 元素, width: 200px; height:200px; background: #eee;</div>
BFC 可以阻止元素被浮动元素覆盖

清除浮动

浮动
清除浮动的方法
.outer {
  background-color: #999;
  /*overflow: hidden;*/
}
.inner {
  float: left;
  background-color: #f9f9f9;
  height: 100px;
  width: 100px;
}
/*.clear {
  clear: both;
}
*/
.outer::after {
  display: block;
  content: '';
  clear: both;
}
</style>
<body>
  <div class="outer">
    <div class="inner"></div>
    <!-- <div class="clear"></div> -->
  </div>
</body>
上一篇 下一篇

猜你喜欢

热点阅读