html - margin-top 与#section div 一起移动?

标签 html css margin

我试图将 .column 类移动到 50px 以下,但是 margin-top: 50px 一起移动#节。为什么会这样?如何解决?

*{ padding:0; margin:0; }

body { width:100%; font-family:"Source Sans Pro" }
#section { height:400px; background-color:#383838; color:White; }
span { font-size:40px; }
.column { width: 300px; border: 2px solid yellow; margin-left:40px; margin-top:50px; }
<html>
<head>
    <title>Breadth</title>
    <link rel="Stylesheet" type="text/css" href="breadth.css" />
    <link href="fonts.css" rel="stylesheet" type="text/css" />
    <link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,900" rel="stylesheet" />
</head>
<body>
    <div id="main">
        <div id="section">
            <div class="column">
                <span class="icon icon-cogs"></span>
                <h2>Maecenas lectus sapien</h2>
                <p>In posuere eleifend odio. Quisque semper augue mattis wisi. Pellentesque viverra vulputate enim. Aliquam erat volutpat.</p>
            </div>
        </div>
    </div>
</body>
</html>

最佳答案

您可以将 padding-top: 50px 应用到 #section 而不是将 margin-top 应用到 .column

引用自 Mozilla 文档:

If there is no border, padding, inline content, or clearance to separate the margin-top of a block from the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block from the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

了解 Margin Collapsing了解更多详情。

*{ padding:0; margin:0; }

body{ width:100%; font-family:"Source Sans Pro" }
#section{height:400px; background-color:#383838; color:White; padding-top: 50px; }
span{  font-size:40px; }
.column{ width: 300px; border: 2px solid yellow; margin-left:40px;}
<html>
<head>
    <title>Breadth</title>
    <link rel="Stylesheet" type="text/css" href="breadth.css" />
    <link href="fonts.css" rel="stylesheet" type="text/css" />
    <link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,900" rel="stylesheet" />
</head>
<body>
    <div id="main">
        <div id="section">
            <div class="column">
                <span class="icon icon-cogs"></span>
                <h2>Maecenas lectus sapien</h2>
                <p>In posuere eleifend odio. Quisque semper augue mattis wisi. Pellentesque viverra vulputate enim. Aliquam erat volutpat.</p>
            </div>
        </div>
    </div>
</body>
</html>

关于html - margin-top 与#section div 一起移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37270087/

相关文章:

html - 样式化水平导航

html - 为什么我的 margin 不起作用?

css - 流体高度 div 包括边距

html - 我如何使这些 <a> 适用于 Firefox 而不仅仅是 Internet Explorer 6

html - 为什么嵌套的 div 位置为 : absolute render with a harsh interpretation of shrink-to-fit?

javascript - 使用 CSSStyleDeclaration.setProperty() 时如何告诉 Chrome 浏览器不要将电话号码转换为指数数?

javascript - 动态改变背景颜色

javascript - 进入和离开时使用 jQuery 的 CSS 动画

javascript - 如何使用 JavaScript 将表单输入和音乐从一个页面传递到另一个页面?

javascript - 全尺寸背景图像到 SVG 路径