解决margin塌陷问题

什么是margin塌陷

在标准文档流中,竖直方向的margin会出现叠加现象(水平方向不会塌陷),两个margin紧挨着,中间没有border或者padding
margin直接接触,就产生了合并
表现为较大的margin会覆盖掉较小的margin,竖直方向的两个盒子中间只有一个较大的margin,这就是margin塌陷现象

解决margin塌陷的方法

  1. 1. 设置边框
  2. 2.overflow:hidden 溢出隐藏
  3. 3.name:before{content:''; display:table;}设置伪类 
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			/* 解决margin塌陷的两种方法
				1. 设置边框
				2.overflow:hidden 溢出隐藏
				3. .name:before{content:''; display:table;}设置伪类
			*/
			.box{
				height: 12.5rem;
				width: 12.5rem;
				background-color: aquamarine;
				/* border: 1px solid #000000; */
			}
			.nbox{
				margin-top: 3.125rem;
				height: 3.125rem;
				width: 3.125rem;
				background-color: black;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="nbox"></div>
		</div>
	</body>
</html>

父盒子没有设置边框,子盒子的margin合并到了父类上,造成了塌陷。

加上边框

塌陷问题就解决了。

overflow:hidden 隐藏溢出

.box{
				height: 12.5rem;
				width: 12.5rem;
				background-color: aquamarine;
				/* border: 1px solid #000000;*/
				overflow: hidden;
			}

 

为父盒子添加隐藏溢出也能解决margin塌陷。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值