CSS清除浮动

浮动的清除


未清楚浮动之前样式

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    .wrap1 {
	    	width: 600px;
	    	background-color: green;
	    }

	    .wrap1 .son1 {
	    	width: 150px;
	    	height: 100px;
	    	background-color: pink;
	    	float: left;
	    }

	    .wrap1 .son2 {
	    	width: 280px;
	    	height: 100px;
	    	background-color: skyblue;
	    	float: left;
	    }

	    .wrap2 {
	    	width: 680px;
	    	height: 300px;
	    	background-color: purple;
	    }
	</style>
</head>
<body>
	<div class="wrap1">
		<div class="son1"></div>
		<div class="son2"></div>
	</div>
	<div class="wrap2"></div>
</body>
</html>
返回顶层目录

清除浮动 — 给父亲增加高度

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    .wrap1 {
	    	width: 600px;
	    	height: 100px;	/*给父亲添加一个高度*/
	    	background-color: green;
	    }

	    .wrap1 .son1 {
	    	width: 150px;
	    	height: 100px;
	    	background-color: pink;
	    	float: left;
	    }

	    .wrap1 .son2 {
	    	width: 280px;
	    	height: 100px;
	    	background-color: skyblue;
	    	float: left;
	    }

	    .wrap2 {
	    	width: 680px;
	    	height: 300px;
	    	background-color: purple;
	    }
	</style>
</head>
<body>
	<div class="wrap1">
		<div class="son1"></div>
		<div class="son2"></div>
	</div>
	<div class="wrap2"></div>
</body>
</html>
返回顶层目录

清除浮动 —clear属性

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    .wrap1 {
	    	width: 600px;
	    	background-color: green;
	    }

	    .wrap1 .son1 {
	    	width: 150px;
	    	height: 100px;
	    	background-color: pink;
	    	float: left;
	    }

	    .wrap1 .son2 {
	    	width: 280px;
	    	height: 100px;
	    	background-color: skyblue;
	    	float: left;
	    }

	    .wrap2 {
	    	width: 680px;
	    	height: 300px;
	    	background-color: purple;
	    }

	    .clear {
	    	clear: both; /*清除浮动 */
	    }
	</style>
</head>
<body>
	<div class="wrap1">
		<div class="son1"></div>
		<div class="son2"></div>
		<div class="clear"></div>
	</div>
	<div class="wrap2"></div>
</body>
</html>
返回顶层目录

清除浮动 — overflow

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    .wrap1 {
	    	width: 600px;
	    	background-color: green;
	    	overflow: hidden;	//清楚浮动
	    }

	    .wrap1 .son1 {
	    	width: 150px;
	    	height: 100px;
	    	background-color: pink;
	    	float: left;
	    }

	    .wrap1 .son2 {
	    	width: 280px;
	    	height: 100px;
	    	background-color: skyblue;
	    	float: left;
	    }

	    .wrap2 {
	    	width: 680px;
	    	height: 300px;
	    	background-color: purple;
	    }
	</style>
</head>
<body>
	<div class="wrap1">
		<div class="son1"></div>
		<div class="son2"></div>
	</div>
	<div class="wrap2"></div>
</body>
</html>
返回顶层目录

清除浮动 — 利用伪元素

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    .wrap1 {
	    	width: 600px;
	    	background-color: green;
	    }

	    .wrap1 .son1 {
	    	width: 150px;
	    	height: 100px;
	    	background-color: pink;
	    	float: left;
	    }

	    .wrap1 .son2 {
	    	width: 280px;
	    	height: 100px;
	    	background-color: skyblue;
	    	float: left;
	    }

	    .wrap2 {
	    	width: 680px;
	    	height: 300px;
	    	background-color: purple;
	    }
         
         /*伪元素产出的是行内元素*/
	    .clearfix::after {
	    	content: "."; /*尽量加不要空 */
	    	display: block; /*转块元素*/
	    	clear: both; /*清除浮动*/
	    	height: 0;   /*高度为0*/
	    	visibility: hidden;/*隐藏盒子*/
	    }
	    .clearfix {
	    	*zoom: 1; /* zoom是ie6 7 清除浮动用的属性 
	    	             *表示该属性只有ie6 7 识别
	    	         */
	    }
	</style>
</head>
<body>
	<div class="wrap1 clearfix">
		<div class="son1"></div>
		<div class="son2"></div>
	</div>
	<div class="wrap2"></div>
</body>
</html>
返回顶层目录

清除浮动 — 双伪元素

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
	    .wrap1 {
	    	width: 600px;
	    	background-color: green;
	    }

	    .wrap1 .son1 {
	    	width: 150px;
	    	height: 100px;
	    	background-color: pink;
	    	float: left;
	    }

	    .wrap1 .son2 {
	    	width: 280px;
	    	height: 100px;
	    	background-color: skyblue;
	    	float: left;
	    }

	    .wrap2 {
	    	width: 680px;
	    	height: 300px;
	    	background-color: purple;
	    }
        
        /* 推荐使用  代码简洁*/
	    .clearfix:before, 
	    .clearfix:after {
           content: "";
           display: table; /* 触发BFC BFC可以清除浮动*/   
	    }

	    .clearfix:after {
	    	clear: both;
	    }

	    .clearfix {
	    	*zoom: 1;
	    }

	    .wrap2:after {
	    	content: "abc";
	    }
	</style>
</head>
<body>
	<div class="wrap1 clearfix">
		<div class="son1"></div>
		<div class="son2"></div>
	</div>
	<div class="wrap2"></div>
</body>
</html>
返回顶层目录

返回浮动目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值