Block Formatting Contexts (BFC,块级格式化上下文)就是一个块级元素 的渲染显示规则 (能够把 BFC 理解为一个封闭的大箱子,,容器里面的子元素不会影响到外面的元素)
代码以下:html
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>外边距折叠</title> <style> body{ margin:0; } .box{ width:500px; height:500px; margin:0 auto; } .btm1{ width:100px; height:100px; background-color:orange; margin:100px; } .btm2{ width:100px; height:100px; background-color:red; margin:100px ; } </style> </head> <body> <div class="box"> <div class="btm1">上下100margin</div> <div class="btm2">上下100margin</div> </div> </body> </html>
效果图:布局
第一个div的下边距和第二个div的上边距发声了重叠,因此两个盒子之间距离只有100px,而不是200px。flex
解决方法:spa
代码以下:code
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>外边距折叠</title> <style> body{ margin:0; } .box{ width:500px; height:500px; margin:0 auto; } .btm1{ width:100px; height:100px; background-color:orange; margin:100px; } .btm2{ width:100px; height:100px; background-color:red; margin:100px ; } .container{ overflow: hidden; } </style> </head> <body> <div class="box"> <div class="container"> <div class="btm1">上下100margin</div> <div> <div class="container"> <div class="btm2">上下100margin</div> <div> </div> </body> </html>
效果图:orm
此次咱们能够清晰的看清两个中间是200px;并没用重叠。htm
代码以下:图片
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>高度塌陷</title> <style> body{ margin:0; } .box{ width:500px; margin:0 auto; border:1px solid green; } .btm1{ width:100px; height:100px; background-color:orange; float:left; } .btm2{ background-color:red; } </style> </head> <body> <div class="box"> <div class="btm1">浮动</div> <div class="btm2"></div> </div> </body> </html>
效果图:utf-8
因为容器内元素浮动,脱离了文档流,因此容器只剩下 2px 的边距高度。若是使触发容器的 BFC,那么容器将会包裹着浮动元素。文档
代码以下:
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>高度塌陷</title> <style> body{ margin:0; } .box{ width:500px; overflow:hidden; margin:0 auto; border:1px solid green; } .btm1{ width:100px; height:100px; background-color:orange; float:left; } .btm2{ background-color:red; } </style> </head> <body> <div class="box"> <div class="btm1">浮动</div> <div class="btm2"></div> </div> </body> </html>
代码以下:
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>高度塌陷</title> <style> body{ margin:0; } .box{ width:500px; overflow:hidden; margin:0 auto; border:1px solid green; } .btm1{ width:100px; height:100px; background-color:orange; float:left; } .btm2{ width:300px; height:300px; background-color:red; } </style> </head> <body> <div class="box"> <div class="btm1">浮动</div> <div class="btm2"> 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! </div> </div> </body> </html>
效果图:
解决方法:
经过触发btm2的BFC解决
代码以下:
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>高度塌陷</title> <style> body{ margin:0; } .box{ width:500px; overflow:hidden; margin:0 auto; border:1px solid green; } .btm1{ width:100px; height:100px; background-color:orange; float:left; } .btm2{ width:300px; height:300px; background-color:red; } .bb{ overflow:hidden } </style> </head> <body> <div class="box"> <div class="btm1">浮动</div> <div class="bb"> <div class="btm2"> 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! 欢迎你们观看! </div> </div> </div> </body> </html>
效果图: