解決嵌套元素外邊距塌陷問題

外邊距塌陷(合并)問題:在嵌套元素中,給內部元素設置外邊距會作用到外部元素上,而無法達到想要效果

 .father {
    width: 300px;
    height: 300px;
    background-color: pink;
}
.son {
    width: 200px;
    height: 200px;
        background-color: purple;
        margin-top: 50px;
}
實現效果
目標效果

解決辦法:
1、給父級元素添加邊框:

.father {
    width: 300px;
    height: 300px;
    background-color: pink;
    border:1px solid white;
}
.son {
    width: 200px;
    height: 200px;
    background-color: purple;
    margin-top: 50px;
}

2、給父級元素添加內邊距:

.father {
    width: 300px;
    height: 300px;
    background-color: pink;
    padding:1px;
}
.son {
    width: 200px;
    height: 200px;
    background-color: purple;
    margin-top: 50px;
}

3、給父級元素添加overflow:

.father {
    width: 300px;
    height: 300px;
    background-color: pink;
    overflow:hidden;
}
.son {
    width: 200px;
    height: 200px;
    background-color: purple;
    margin-top: 50px;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容