解决块元素嵌套垂直外边距塌陷的方法

        

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
		/* 方式一:给父级元素定义一个边框 */
		/* .father{
			width: 300px;
			height: 300px;
			background: red;
			margin-top:30px;
			border: 1px solid;
		}
		.son{
			width:200px;
			height:200px;
			background: skyblue;
			margin-top: 30px;
		}
		 */
		/* 方式二:给父级元素定义一个内边距 */
		.father{
			width:300px;
			height:300px;
			background: red;
			margin-top: 30px;
			padding: 1px;
		}
		.son{
			width:200px;
			height:200px;
			background: skyblue;
			margin-top: 30px;
		}
		
		/* 方式三:给父级元素定义一个overflow:auto或hidden; */
		/* .father{
			width:300px;
			height:300px;
			background: red;
			margin-top: 30px;
			overflow: auto;
			overflow: hidden;
		}
		.son{
				width: 200px;
				height:200px;
				background: deepskyblue;
				margin-top: 30px;
		} */
		
		/* 方式四:直接在父类设置height */
		/* .father{
			border: 1px solid blue;
			height: 300px;
		}
		.son{
				width: 200px;
				height: 200px;
				background: deepskyblue;
				margin-top: 30px;
		} */
		
		/* 方法五:伪类after */
		/* #father:after{
			display: block;
			content:"" ;
			clear:both;
		}
		.son{
			width: 200px;
			height: 200px;
			background: skyblue;
			border-top: 30px ;
		} */
		
		
		/* 方式六:父元素中最后一个块元素的clear:both; */	
		/* .father{
			margin: 0 auto;
			width:600px;
		border: 1px solid red;

		}
		.son{
			float: left;
				width: 200px;
				height: 200px;
				background: deepskyblue;
				
				
		} 
		.sonn{
			float: left;
				width: 200px;
				height: 200px;
				background: deepskyblue;
		}
		.class{
			clear: both;
		}
		.text{
			height: 300px;
			background: black;
		}
		 */
		</style>
	</head>
	<body>
		<div class="father">
			<div class="son">
				
			</div>
			<div class="sonn">
				
			</div>
			<div class="class">
				
			</div>
			<div class="text">
				
			</div>
		</div>
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值