html - margin-top css 属性的意外行为

标签 html css

我正面临一个非常简单的 margin-top 问题,它把我吓坏了。我简化了我的文档以使其可读。 这是我的html文档结构

<body>
    <section>

    </section>

    <section>
        <div></div>
    </section>
</body>

这是我的 CSS

section{
    width: 100%;
    height:768px;
    background-color: red;
    border-bottom: 1px solid yellow;
    position: relative;
}

div{
    background-color:green;
    height:50px;
    width:50px;

    margin-top:0px;
}

divmargin-top为0时,看起来像这样: enter image description here

但是当我将它更改为 margin-top10px 时,它看起来像这样:enter image description here

我无法指出该空白是如何添加的。检查表明它是 body 的一部分。节实际上被推了下来。我期待那个小 div 相对于部分被推低。谁能解释这种奇怪的行为?

最佳答案

这里是 fiddle http://jsfiddle.net/suriyag/UhqX9/1/有满足您要求的解决方案

    section{
    position:relative;
    width: 100%;
    height:768px;
    background-color: red;
    border-bottom: 1px solid yellow;
    position: relative;
}

div{
    position:absolute; 
    top:10px;
    background-color:green;
    height:50px;
    width:50px;
  }

对你的代码做了一些小改动

关于html - margin-top css 属性的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165880/

相关文章:

java - 如何从 Java 中的浏览器(自动内存数据)等网站下载 HTML

css - "@extend must be used with a %placeholder"是什么意思?

css - Salesforce 闪电操作模式大小

php - 从sql数据库中删除图像

javascript - 在 Javascript 中单击文本时 Swal.fire (Sweetalert)

c# - 如何去除 C# 代码中除 <br> 之外的所有 html 标签?

javascript - Turbogears 与 Require JS 集成

html - Bootstrap 3 Affix 侧边栏无法正常工作

html - 使用中央 Logo 导航栏 Bootstrap 时如何将社交图标拉到右边

javascript - 媒体查询的 jQuery 图像错误处理