前端工具:MyCodepen】| 【W3School】| 【Bootstrap】| 【MarkDown】| 【正则表达式】| 【图标下载】| 【在线工具】| 【W3标准及兼容】| 【前端中文文档

布局时margin会影响父元素

布局时margin会影响父元素.md

在布局使用margin时

<div class=”login-bg”>
        <div class=”login”>
        12345
        </div>
</div>

<style>
.login-bg{
background-color:red;
width: 400px;
height: 400px;
margin: 0 auto;
background: url(imac.png) no-repeat;
background-size:400px 400px;
}
.login{
background-color: #999;
width: 300px;
height: 200px;
margin-top: 100px;
}
</style>

此时两个div都与上偏移了100px;

而不是我们所想要的.login偏移100px;

解决方法

给 login-bg Div 加上 padding/border,
或者给 login-bg Div / Inner Div 设置为 float/position:absolute(CSS2.1规定浮动元素和绝对定位元素不参与Margin折叠)

posted @ 2014-10-09 10:53  FuGardenia  阅读(215)  评论(0编辑  收藏  举报

关于我们

喜欢编程。
上大学四年,一直在探索。
最终走上前端工程师的不归路。


我的微博:

@WOOEOOBOO

GitHub:

@FuGardenia

关注我们

微信号:MoveClouds
移动互联网,云前端信息传播自媒体。

Simple is beauty,Less is more.

简而美,少即多。