CSS margin奇怪的行为,为什么? [英] CSS margin strange behavior, why?

查看:100
本文介绍了CSS margin奇怪的行为,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <div style="width:400px;height:200px;background-color:Gray;margin-bottom:10px;"></div>
    <div style="width:400px;height:200px;background-color:Green;margin-top:10px;"></div>
</body>
</html>

这两个DIV之间的空间只有10像素。

the space between those two DIVs is only 10 pixels.

为什么?请解释。

推荐答案

这种行为称为折叠边距。不添加边距值,但使用较高的值:

That behavior is called collapsing margins. The margin values are not added but the higher value is used:


垂直边距可能在某些框之间折叠:

Vertical margins may collapse between certain boxes:


  • 正常流折叠中块框的两个或多个相邻垂直边距。所得到的边缘宽度是相邻边缘宽度的最大值。 [...]


这篇关于CSS margin奇怪的行为,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆