【 总结 】BFC

BFC

块级格式化上下文 指一个独立的块级渲染区域,该区域拥有一套渲染规则来约束块级盒子的布局
一个父级盒子不设置height 当内容子元素都浮动时 无法撑起高度

如何创建BFC

  • float 不能是none
  • position 不能是static或者relative
  • display : inline-block || flex || inline-flex
  • overflow : hidden || auto

作用

  • 解决盒子高度塌陷的问题
  • 阻止元素被浮动元素覆盖

float

	<div class="father">
		<div class="son"></div>
		<div class="son"></div>
		<div class="son"></div>
	</div>
    
    <style type="text/css">
    	*{
    		margin: 0;
    		padding: 0;
    	}
    	.father{
    		width: 100%;
    		background-color: #000;
    		/******************/
    		float: left
    		/******************/
    	}
    	.son{
    		width: 200px;
    		height: 200px;
    		background-color: red;
    	    float: left;
    	}
    </style>

position

	<div class="father">
		<div class="son"></div>
		<div class="son"></div>
		<div class="son"></div>
	</div>
    
    <style type="text/css">
    	*{
    		margin: 0;
    		padding: 0;
    	}
    	.father{
    		width: 100%;
    		background-color: #000;
    		/******************/
    		position: absolute;
    		/******************/
    	}
    	.son{
    		width: 200px;
    		height: 200px;
    		background-color: red;
    	    float: left;
    	}
    </style>

display

	<div class="father">
		<div class="son"></div>
		<div class="son"></div>
		<div class="son"></div>
	</div>
    
    <style type="text/css">
    	*{
    		margin: 0;
    		padding: 0;
    	}
    	.father{
    		width: 100%;
    		background-color: #000;
    		/******************/
    		display: inline-block;
    		/******************/
    	}
    	.son{
    		width: 200px;
    		height: 200px;
    		background-color: red;
    	    float: left;
    	}
    </style>

overflow

	<div class="father">
		<div class="son"></div>
		<div class="son"></div>
		<div class="son"></div>
	</div>
    
    <style type="text/css">
    	*{
    		margin: 0;
    		padding: 0;
    	}
    	.father{
    		width: 100%;
    		background-color: #000;
    		/******************/
    		overflow: hidden;
    		/******************/
    	}
    	.son{
    		width: 200px;
    		height: 200px;
    		background-color: red;
    	    float: left;
    	}
    </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值