html - CSS 边距不起作用。为什么?

标签 html css margin

<分区>

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#a
{
    background:#33F;
    width:1000px;
    height:1000px;
}

#b
{
    width:500px;
    height:500px;
    background:#F00;
}

#c
{
    margin-top:50px;
    height:200px;
    width:200px;
    background:#FF0;
}
</style>
</head>

<body>

<div id="a">
    <div id="b">
        <div id="c">
        </div>
    </div>
</div>
</body>
</html>

为什么 div#c 从浏览器顶部开始边缘化而不是远离 div#b。

最佳答案

CSS has a concept called margin collapse .

Two margins are adjoining if and only if:

...

  • both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
    • top margin of a box and top margin of its first in-flow child

关于html - CSS 边距不起作用。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14325788/

上一篇:css - 自定义字体无法在 Chrome 上正确显示

下一篇:css - Firefox:类型未在按钮中垂直居中

相关文章:

javascript - 为什么此 CSS 代码会在桌面 View 中 chop 内容?

html - 更改图像的边距值未生效

html - Font Awesome 图标未显示在某些元素中

html - CSS3 中的垂直对齐

html - 具有渐变颜色的背景图像在 chrome 中不起作用

javascript - 让鼠标滚轮选框无限大?

android - setmargin 在 android 中的相对布局中不起作用

html - 嵌套 div 时不要在宽度中包含边距/填充

javascript - JavaScript 中的倒数计时器

javascript - 如何在 node.js 生成的 html 中显示变量