页面顶部元素与body共享margin导致出现滚动条

body里面有个div,html和body高度都是100%,margin和padding都是0,内部div设置了一个margin-top,body就出现了滚动条。

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
		html,body{
			height: 100%;
			margin:0;
			padding:0;
		}
		.test{
			height:400px;
			margin:40px;
			background:#0f0;
		}
	</style>
</head>
<body>
	<div class="test"></div>
</body>
</html>

出现原因:

In this specification, the expression collapsing margins means that adjoining margins (no non-empty content, padding or border areas or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin. 所有毗邻的两个或更多盒元素的margin将会合并为一个margin共享之。毗邻的定义为:同级或者嵌套的盒元素,并且它们之间没有非空内容、Padding或Border分隔。所以div的margin-top与body共享了,这样就让body把html撑大了

解决方案:

1.  body中添加

body{
  overflow-y:hidden;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值