padding-bottom和 margin-bottom

本文介绍了一种使用CSS实现不同高度元素等高的方法。通过为每个子元素设置相同的`padding-bottom`和负的`margin-bottom`来达到视觉上统一高度的效果。此技巧适用于需要保持布局一致性的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过设置margin-bottom跟padding-bottom绝对值相同

   padding-bottom:200px;
      margin-bottom:-200px;

能达到同高

html代码

<div id="wrap">
        <div id="left">
            <p style="height:50px">style="height:50px"</p>
        </div>
        <div id="center">
            <p style="height:100px">style="height:100px"</p>
        </div>
        <div id="right">
            <p style="height:200px">style="height:200px"</p>
        </div>
    </div>

css代码

 #wrap{
      overflow:hidden;
        width:580px;
       
        border: 2px solid black;
    }
    #left,#center,#right{
     padding-bottom:200px;
      margin-bottom:-200px;
    }
    #left {
        float:left;
        width:140px;
        background:#777;
    }
    #center {
        float:left;
        width:300px;
        background:#888;
    }
    #right {
        float:right;
        width:140px;
        background:#999;
    }
    p {color:#FFF;text-align:center; margin:0;}
其中wrap要设置overflow:hidden,清除float,使wrap包裹里面的div,也把超出的隐藏起来。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值