CSS:margin“塌陷”的处理方式

1、 垂直外边距合并、也称外边距塌陷
  • 适用场景:两个垂直排列的同级元素(上面的盒子设置margin-bottom、下面的盒子设置margin-top)
<!-- html部分 -->
<div class="fa">
    <div class="son1">A盒子</div>
    <div class="son2">B盒子</div>
</div>
  • 解决方式:只需要设置其中的一种即可(要么保留margin-top、要么保留margin-bottom)
<!-- css部分 -->
<style type="text/css">
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
        margin: 20px auto;
    }
    .son1, .son2 {
        width: 100px;
        height: 100px;
    }
    .son1 {
        background-color: pink;
        /* margin-bottom: 20px; */
    }
    .son2 {
        background-color: red;
        margin-top: 40px;
    }
</style>
  • 页面展示:取margin-top与margin-bottom两者中值最大的那个

2、父子黏连 
  • 适用场景:父子元素(子元素设置margin-top、父盒子也会受到影响)
<!-- html部分 -->
<div class="fa">
    <div class="son">子元素</div>
</div>
  • 解决方式:

方式一:给父级元素添加overflow属性,比如:overflow: hidden; 或者 overflow: auto; 

<!-- css部分 -->
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
        overflow: auto;
    }
    .son {
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 40px;
    }
</style>

方式二:给父级元素添加padding属性,比如:padding: 1px;

<!-- css部分 -->
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
        padding: 1px;
    }
    .son {
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 40px;
    }
</style>

 方式三:给父级元素添加border属性,比如:border: 1px solid transparent;

<!-- css部分 -->
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
        border: 1px solid transparent;
    }
    .son {
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 40px;
    }
</style>

方式四:给父级元素添加position属性,比如:position: absolute; 

<!-- css部分 -->
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
        position: absolute;
    }
    .son {
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 40px;
    }
</style>

方式五:给父级元素添加float属性,比如:float: left;

<!-- css部分 -->
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
        float: left;
    }
    .son {
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 40px;
    }
</style>

 方式六:给父级元素添加伪元素

<!-- css部分 -->
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
    }
    .fa::before {
        content: '';
        display: table;
    }
    .son {
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 40px;
    }
</style>

  方式七:给父级元素添加display属性,比如:display: table;

<!-- css部分 -->
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    .fa {
        width: 200px;
        height: 240px;
        background-color: #ccc;
        display: table;
    }
    .son {
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 40px;
    }
</style>

 

  • 页面展示:


  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值