解决CSS子级float而父级不能自适应的问题

目录

尴尬场景

解决方法1

解决方法2

解决方法3


尴尬场景

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
	<style type="text/css">
	*{
		margin: 0;
		padding: 0;
	}
	.main{
		padding: 10px;
		border: #000000 1px solid;
	}
	.sub1{
		width: 30%;
		height: 100px;
		float: left;
		border: #000000 1px solid;
	}
	.sub2{
		width: 65%;
		height: 100px;
		float: right;
		border: #000000 1px solid;
	}
</style>
</head>
<body>
	<div class="mui-content">
		<div class="main">
			<div class="sub1">

			</div>
			<div class="sub2">

			</div>
		</div>
	</div>
</body>
</html>

解决方法1

给父标签添加overflow:hidden属性

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
	<style type="text/css">
	*{
		margin: 0;
		padding: 0;
	}
	.main{
		padding: 10px;
		border: #000000 1px solid;
        overflow: hidden;
	}
	.sub1{
		width: 30%;
		height: 100px;
		float: left;
		border: #000000 1px solid;
	}
	.sub2{
		width: 65%;
		height: 100px;
		float: right;
		border: #000000 1px solid;
	}
</style>
</head>
<body>
	<div class="mui-content">
		<div class="main">
			<div class="sub1">

			</div>
			<div class="sub2">

			</div>
		</div>
	</div>
</body>
</html>

解决方法2

父级标签内添加清除浮动的子级标签

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
	<style type="text/css">
	*{
		margin: 0;
		padding: 0;
	}
	.main{
		padding: 10px;
		border: #000000 1px solid;
	}
	.sub1{
		width: 30%;
		height: 100px;
		float: left;
		border: #000000 1px solid;
	}
	.sub2{
		width: 65%;
		height: 100px;
		float: right;
		border: #000000 1px solid;
	}
	.sub3{
		clear: both;
	}
</style>
</head>
<body style="padding: 20px">
	<div class="mui-content">
		<div class="main">
			<div class="sub1">

			</div>
			<div class="sub2">

			</div>
			<div class="sub3"></div>
		</div>
	</div>
</body>
</html>

解决方法3

如果知道子级标签的高度的话,那就给父级添加固定高度吧

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
	<style type="text/css">
	*{
		margin: 0;
		padding: 0;
	}
	.main{
		padding: 10px;
		border: #000000 1px solid;
		height: 120px;
	}
	.sub1{
		width: 30%;
		height: 100px;
		float: left;
		border: #000000 1px solid;
	}
	.sub2{
		width: 65%;
		height: 100px;
		float: right;
		border: #000000 1px solid;
	}
</style>
</head>
<body style="padding: 20px">
	<div class="mui-content">
		<div class="main">
			<div class="sub1">

			</div>
			<div class="sub2">

			</div>
		</div>
	</div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值