css清除浮动

问题

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		.one {
			width: 500px;
			border: 1px  solid red;
		}
		.one1 {
			float: left;
			width: 200px;
			height: 200px;
			background-color: purple;
		}
		.one2 {
			float: left;
			width: 250px;
			height: 250px;
			background-color: skyblue;
		}
		.two {
			width: 700px;
			height: 150px;
			background-color: #000;
		}
	</style>
</head>
<body>
	<div class="one">
		<div class="one1"></div>
		<div class="one2"></div>
	</div>
	<div class="two"></div>
</body>
</html>

上面代码显示结果
在这里插入图片描述
one1和one2为one的自己元素,但是因为one1和one2加了浮动,所以不能撑开one,并且two也顶上来了。

解决

方式一:对于子元素

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		.one {
			width: 500px;
			border: 1px  solid red;
		}
		.one1 {
			float: left;
			width: 200px;
			height: 200px;
			background-color: purple;
		}
		.one2 {
			float: left;
			width: 250px;
			height: 250px;
			background-color: skyblue;
		}
		.two {
			width: 700px;
			height: 150px;
			background-color: #000;
		}
		/* 添加样式 */
		.clear {
			clear: both;
		}
	</style>
</head>
<body>
	<div class="one">
		<div class="one1"></div>
		<div class="one2"></div>
		<div class="clear"></div>  <!-- 添加一个标签 -->
	</div>
	<div class="two"></div>
</body>
</html>

在这里插入图片描述

方式二:对于父元素

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		.one {
			width: 500px;
			border: 1px  solid red;
			/*给父级添加 overflow 就可以清除浮动*/
			overflow: hidden;
		}
		.one1 {
			float: left;
			width: 200px;
			height: 200px;
			background-color: purple;
		}
		.one2 {
			float: left;
			width: 250px;
			height: 250px;
			background-color: skyblue;
		}
		.two {
			width: 700px;
			height: 150px;
			background-color: #000;
		}

	</style>
</head>
<body>
	<div class="one">
		<div class="one1"></div>
		<div class="one2"></div>
	</div>
	<div class="two"></div>
</body>
</html>

方式三:伪元素

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		/*声明清除浮动的样式*/
		.clearfix:after {
			content: "";
			display: block;
			height: 0;
			visibility: hidden;
			clear: both;
		}
		.clearfix {
			*zoom: 1;  /*ie6,7 清除浮动的样式*/
		}
		.one {
			width: 500px;
			border: 1px  solid red;
			/*给父级添加 overflow 就可以清除浮动*/
			overflow: hidden;
		}
		.one1 {
			float: left;
			width: 200px;
			height: 200px;
			background-color: purple;
		}
		.one2 {
			float: left;
			width: 250px;
			height: 250px;
			background-color: skyblue;
		}
		.two {
			width: 700px;
			height: 150px;
			background-color: #000;
		}

	</style>
</head>
<body>
<div class="one clearfix">
	<div class="one1"></div>
		<div class="one2"></div>
	</div>
	<div class="two"></div>
</body>
</html>

方式四:双伪元素

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		/*声明清除浮动的样式*/
		.clearfix:before,
		.clearfix:after {
			content: "";
			display: table;
		}
		.clearfix:after {
			clear: both;
		}
		.clearfix {
			*zoom: 1;
		}
		.one {
			width: 500px;
			border: 1px  solid red;
			/*给父级添加 overflow 就可以清除浮动*/
			overflow: hidden;
		}
		.one1 {
			float: left;
			width: 200px;
			height: 200px;
			background-color: purple;
		}
		.one2 {
			float: left;
			width: 250px;
			height: 250px;
			background-color: skyblue;
		}
		.two {
			width: 700px;
			height: 150px;
			background-color: #000;
		}

	</style>
</head>
<body>
<div class="one clearfix">
	<div class="one1"></div>
	<div class="one2"></div>
</div>
<div class="two"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值