BFC

什么是BFC?

BFC(Block formatting context),直译为“块级格式化上下文”。它是一个独立的渲染区域,使内部元素不会影响到外部元素,也避免外部元素影响内部元素,可以说,这个区域如何布局与外部区域毫无关系。

如何创建BFC呢?

  1. float 的值不是 none
  2. 设置 overflow 的值为 hidden / auto / scroll
  3. position 的值不是 static / relative
  4. 设置 display 的值为 block / table-cell / flex / table-caption / inline-flex

BFC常用来解决什么问题呢?

  1. 解决子元素浮动导致父元素高度坍塌问题

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title></title>
    		<style type="text/css">
    			.container{
    				background-color: yellow;
    				padding: 10px;
    			}
    			.item{
    				float: left;
    				height: 300px;
    				width: 300px;
    			}
    			.item1{
    				background-color: red;
    			}
    			.item2{
    				background-color: blue;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="container">
    			<div class="item item1"></div>
    			<div class="item item2"></div>
    		</div>
    	</body>
    </html>
    

    高度坍塌图:
    在这里插入图片描述

    创建 BFC 布局解决该问题:

    1. 设置 float 的值不为 none

      .container{
      	float: left;
      	background-color: yellow;
      	padding: 10px;
      }
      

      在这里插入图片描述

    2. 设置 overflow 的值为 hidden / auto / scroll

      .container{
      	overflow: scroll;
      	background-color: yellow;
      	padding: 10px;
      }
      

      在这里插入图片描述

    3. position 的值不是 static / relative

      .container{
      	position: absolute;
      	background-color: yellow;
      	padding: 10px;
      }
      

      在这里插入图片描述

    4. 设置 display 的为 table-cell / flex / table-caption / inline-flex

      .container{
      	display: table-cell;
      	background-color: yellow;
      	padding: 10px;
      }
      

      在这里插入图片描述


  1. 相邻元素的外边距重合

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title></title>
    		<style type="text/css">
    			.item{
    				margin: 30px;
    				height: 100px;
    				width: 100px;
    			}
    			.item1{
    				background-color: red;
    			}
    			.item2{
    				background-color: blue;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="container">
    			<div class="item item1"></div>
    			<div class="item item2"></div>
    		</div>
    	</body>
    </html>
    

    相邻元素外边距重合图:
    在这里插入图片描述

    创建 BFC 布局解决该问题:

    设置 overflow的值为 hidden / scroll / auto

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title></title>
    		<style type="text/css">
    			.item{
    				margin: 30px;
    				height: 100px;
    				width: 100px;
    			}
    			.item1{
    				background-color: red;
    			}
    			.item2{
    				background-color: blue;
    			}
    			.container2{
    				overflow: hidden;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="container">
    			<div class="item item1"></div>
    			<div class="container2">
    				<div class="item item2"></div>
    			</div>
    		</div>
    	</body>
    </html>
    

    在这里插入图片描述


  1. 自适应两栏布局

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title></title>
    		<style type="text/css">
    			.container{
    				color: white;
    			}
    			.left{
    				float: left;
    				width: 200px;
    				height: 100px;
    				background-color: red;
    			}
    			.right{
    				height: 100vh;
    				background-color: blue;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="container">
    			<div class="left">left</div>
    			<div class="right">right</div>
    		</div>
    	</body>
    </html>
    

    问题图:
    在这里插入图片描述
    创建 BFC 布局解决该问题:

    1. 设置overflow 的值为 hidden / scroll / auto

      .right{
      	overflow: hidden;
      	height: 100vh;
      	background-color: blue;
      }
      

      在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值