你以为的BUG--BFC给你解决!

一.BFC 定义

BFC(Block formatting context)直译为”块级格式化上下文”。它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的Block-level Box如何布局,并且与这个区域外部毫不相干。

 

二.哪些元素会生成BFC?

  1. 根元素
  2. float属性不为none
  3. position为absolute或fixed
  4. display为inline-block, table-cell, table-caption, flex, inline-flex
  5. overflow不为visible

三、BFC的作用及原理

  1.清除内部浮动

    

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    .div1{width: 100px;height: 100px;background: #fcc; }
    .div2{width: 50px;height: 50px;margin-top: 20px;background: #007ACC}
</style>
<body>
        <div class="div1"><div class="div2"></div></div>
        
</body>
</html>
<script>

  

 

  给父元素设置overflow:hidden

  

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <style>
 8     .div1{width: 100px;height: 100px;background: #fcc;overflow:hidden }
 9     .div2{width: 50px;height: 50px;margin-top: 20px;background: #007ACC}
10 </style>
11 <body>
12         <div class="div1"><div class="div2"></div></div>
13         <div class="div3"></div>
14 </body>
15 </html>
16 <script>

 

    2.自适应两栏布局

    

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    .div1{width: 100px;height: 100px;background: #fcc;float: left }
    .div2{width: 500px;height: 500px;margin-top: 20px;background: #007ACC}
</style>
<body>
        <div class="div1"></div>
        <div class="div2"></div>
</body>
</html>
<script>

  给div2加overflow:hidden;

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    .div1{width: 100px;height: 100px;background: #fcc;float: left }
    .div2{width: 500px;height: 500px;margin-top: 20px;background: #007ACC;overflow: hidden}
</style>
<body>
        <div class="div1"></div>
        <div class="div2"></div>
</body>
</html>
<script>

3.防止垂直margin重叠

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    .div1{width: 100px;height: 100px;background: #fcc;margin-bottom: 10px; }
    .div2{width: 50px;height: 50px;margin-top: 20px;background: #007ACC}
    .div3{margin-top: 10px;background: #fcc;width: 100px;height: 100px;position: absolute}
</style>
<body>
        <div class="div1"><div class="div2"></div></div>
        <div class="div3"></div>
</body>
</html>
<script>

 

  

 

 

 

 

4.我们可以在p外面包裹一层容器,并触发该容器生成一个BFC。那么两个P便不属于同一个BFC,就不会发生margin重叠了。

 

 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 
 8 <style>
 9     *{margin: 0;padding: 0px;}
10     .div1{overflow: hidden }
11     p{margin: 50px;background: #fcc;width: 100px;height: 100px;}
12 </style>
13 <body>
14         <div class="div1"><p></p></div>
15         <p></p>
16 </body>
17 </html>
18 <script>

 

四.BFC布局规则:

  1. 内部的Box会在垂直方向,一个接一个地放置。
  2. Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠
  3. 每个元素的margin box的左边, 与包含块border box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。
  4. BFC的区域不会与float box重叠。
  5. BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
  6. 计算BFC的高度时,浮动元素也参与计算

 

 

 

 

                                未完待续..........2018-08-09 11:02:01

 

posted @ 2018-08-09 11:03  芥末的糖  阅读(274)  评论(2编辑  收藏  举报