css实现手机端底部4个正方形

本文介绍了一种利用CSS的padding-bottom属性设置元素百分比高度的方法,并通过实例演示了如何实现固定比例的布局,适用于手机底部四个正方形的场景。

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

1、根据原理:padding、margin可以设置百分百;百分比的数值是根据父元素的宽度设置的。
例:父div   width:100px   那么子元素设置height:0 ; padding-bottom:25%  ;这样子元素的高度为  100*25% = 25px

 

2、实现:

布局:
<div class="wrap">
<div class="item1">
<div class="item2"></div>
</div>
<div class="item1">
<div class="item2"></div>
</div>
<div class="item1">
<div class="item2"></div>
</div>
<div class="item1">
<div class="item2"></div>
</div>
</div>
样式:
.wrap{
width: 100%;
background: red;
position: absolute;
bottom:0;
}
.wrap .item1{
width: 25%;
background: green;
float: left;
height: 100%;
}
.wrap .item1 .item2{
height: 0;
padding-bottom: 100%;
width: 100%;
background: #fff;
border:1px solid;
       box-sizing: border-box;
}

3、实现手机底部四个正方形
布局:

<div class="t1">

  <div class="t2">

    <div class="aaaa">

      dadsa

    </div>

  </div>

  <div class="t2">

      <div class="aaaa">

          dadsa

        </div>

  </div>

  <div class="t2"></div>

  <div class="t2"></div>

</div>


样式:
    

.t1{

      width: 400px;

      height: 200px;;

      background: red;

    }

    .t2{

      width: 25%;

      background: #ccc;

      position: relative;

      float: left;

      border: 2px solid #000;

      }

      .t2:after{

        content: "";

        display: block;

        padding-bottom: 100%;

      }

      .aaaa{

        position: absolute;

        width: 100%;

        height: 100%;

      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值