Web前端之路

CSS外边距塌陷问题

2021-01-14  本文已影响0人  前小小

外边距塌陷:

块级元素的上下外边距有时候会合并,合并后的外边距等于合并前两个外边距中的较大值,这种现象称为外边距塌陷。

外边距塌陷有以下三种情况:

解决方法:

方法一:为父元素设置边框,并将父元素高度减少

.father {
    height: 199px;
    width: 200px;
    background: gray;
    border-top: 1px solid gray;
}

方法二:父元素增加内边距,并减少父元素高度

.father {
    height: 199px;
    padding-top:1px;
    width: 200px;
    background: gray;
}

方法三:父元素增加属性 overflow: hidden

.father {
    overflow: hidden;
    height: 200px;
    width: 200px;
    background: gray;
}
上一篇 下一篇

猜你喜欢

热点阅读