清除浮动的方式

高度塌陷:当所有的子元素浮动的时候,且父元素没有设置高度,这时候父元素就会产生高度塌陷。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style>
		.box { 
		    width:100%;
			background-color:pink;
		}         
		.item { 
		    width:200px;
			height: 200px;
			background-color:yellow;
			float: left;
		}
		.itemTwo{
			background-color:skyblue;
			margin-left: 100px;
		}
	</style>
	<body>
		<div class="box">
		    <div class="item"></div>
			<div class="item itemTwo"></div>
		</div>
	</body>
</html>

在这里插入图片描述

  1. 给父元素单独定义高度
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style>
		*{
			margin: 0;
			padding:0;
		}
		.box { 
		    width:100%;
			background-color:pink;
			height: 200px;
		}         
		.item { 
		    width:200px;
			height: 200px;
			background-color:yellow;
			float: left;
		}
		.itemTwo{
			background-color:skyblue;
			margin-left: 100px;
		}
	</style>
	<body>
		<div class="box">
		    <div class="item"></div>
			<div class="item itemTwo"></div>
		</div>
	</body>
</html>

在这里插入图片描述
优点:快速简单,代码少 缺点:无法进行响应式布局

  1. 父级定义overflow:hidden;zoom:1(针对ie6的兼容)
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style>
		*{
			margin: 0;
			padding:0;
		}
		.box { 
		    width:100%;
			background-color:pink;
			overflow: hidden;
			zoom: 1;
		}         
		.item { 
		    width:200px;
			height: 200px;
			background-color:yellow;
			float: left;
		}
		.itemTwo{
			background-color:skyblue;
			margin-left: 100px;
		}
	</style>
	<body>
		<div class="box">
		    <div class="item"></div>
			<div class="item itemTwo"></div>
		</div>
	</body>
</html>

优点:简单快速、代码少,兼容性较高 缺点:超出部分被隐藏,布局时要注意

  1. 在浮动元素后面加一个空标签,clear:both;height:0;overflow:hidden
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style>
		*{
			margin: 0;
			padding:0;
		}
		.box { 
		    width:100%;
			background-color:pink;
		}         
		.item { 
		    width:200px;
			height: 200px;
			background-color:yellow;
			float: left;
		}
		.itemTwo{
			background-color:skyblue;
			margin-left: 100px;
		}
		.emptyLabel{
			clear: both;
			height: 0;
			overflow: hidden;
		}
	</style>
	<body>
		<div class="box">
		    <div class="item"></div>
			<div class="item itemTwo"></div>
			<div class="emptyLabel"></div>
		</div>
	</body>
</html>

优点:简单快速、代码少,兼容性较高。缺点:增加空标签,不利于页面优化

  1. 父级定义overflow:auto
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style>
		*{
			margin: 0;
			padding:0;
		}
		.box { 
		    width:100%;
			background-color:pink;
			overflow: auto;
		}         
		.item { 
		    width:200px;
			height: 200px;
			background-color:yellow;
			float: left;
		}
		.itemTwo{
			background-color:skyblue;
			margin-left: 100px;
		}
	</style>
	<body>
		<div class="box">
		    <div class="item"></div>
			<div class="item itemTwo"></div>
		</div>
	</body>
</html>

优点:简单,代码少,兼容性好。 缺点:内部宽高超过父级div时,会出现滚动条

  1. 万能清除法
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style>
		* {
			margin: 0;
			padding: 0;
		}

		.box {
			width: 100%;
			background-color: pink;
		}

		.item {
			width: 200px;
			height: 200px;
			background-color: yellow;
			float: left;
		}

		.itemTwo {
			background-color: skyblue;
			margin-left: 100px;
		}

		.clearfix:after {
			content: "";
			clear: both;
			display: block;
			height: 0;
			overflow: hidden;
			visibility: hidden;
		}
	</style>
	<body>
		<div class="box clearfix">
			<div class="item"></div>
			<div class="item itemTwo"></div>
		</div>
	</body>
</html>

优点:写法固定,兼容性高 缺点:代码多

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值