本文主要是介绍外边距重叠问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
父子
父子结构中水平外边距与垂直外边距表现的往往也不同
水平外边距
水平外边距往往规则渲染
html
<div class="wrapper"><div class="inner"></div>
</div>
css
.wrapper{width: 400px;height: 400px;background-color: red;
}
.inner{width: 200px;height: 200px;background-color: black;margin-left: 50px;
}
效果
垂直外边距
垂直外边距会发生重叠问题
html
<div class="wrapper"><div class="inner"></div>
</div>
css
.wrapper{width: 400px;height: 400px;background-color: red;
}
.inner{width: 200px;height: 200px;background-color: black;margin-top: 50px;
}
效果
问题解决
父元素
- 外层元素
padding
代替 - 外层元素
overflow:hidden;
子元素
- 内层元素透明边框
border:1px solid transparent;
- 内层元素绝对定位
postion:absolute;
- 内层元素 加
float:left;
或display:inline-block;
- 内层元素
padding:1px;
兄弟
兄弟元素与父子元素往往表现的不同
垂直外边距
垂直外边距取最大值
结果取了最大的外边距即
20px
水平外边距
水平外边距最终是要相加(这一点与垂直外边距不同)
html
<div class="wrapper"><div class="box1"></div><div class="box2"></div>
</div>
css
.wrapper{display: flex;
}
.box1 {width: 200px;height: 200px;margin-right: 100px;background-color: red;
}/* 取最大外边距 */
.box2 {width: 200px;height: 200px;background-color: green;margin-left: 50px;
}
效果
这篇关于外边距重叠问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!