h5中的宫格元素居中几种方式

元素的居中实现很多,css和js都可以完成效果。
这里,记录下在h5实现宫格的居中对齐。我这里主要是3*3, 3*2, 3*1,最后一行分别的是3个元素,2个元素,1个元素的情况。

3*3效果

  • 也是最简单的效果
    在这里插入图片描述
<body>
   <div class="box">1</div>
   <div class="box">2</div>
   <div class="box">3</div>
   <div class="box">4</div>
   <div class="box">5</div>
   <div class="box">6</div>
   <div class="box">7</div>
   <div class="box">8</div>
   <div class="box">9</div>
  </body>
body{
  background-color: slateblue;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
.box{
  width: 100px;height: 100px;
  background-color: cornflowerblue;
  margin: 10px;
  color: #fff;
  text-align: center;
  line-height: 100px;
}

3*2效果

<body>
   <div class="box">1</div>
   <div class="box">2</div>
   <div class="box">3</div>
   <div class="box">4</div>
   <div class="box">5</div>
   <div class="box">6</div>
   <div class="box">7</div>
   <div class="box">8</div>
   <div class="box">9</div>
   <div class="box">10</div>
   <div class="box">11</div>
  </body>
  • 这样的居中,不是我想要的效果,要最后一行能左对齐·,线下继续使用上面的css无法做到
    在这里插入图片描述
  • 改造css,增加下面的代码
body::after{
  content: '';
  width: 100px;
  height: 100px;
  margin: 10px;
}

这样,我们就实现了最后一行的左对齐
在这里插入图片描述

3*1的效果

同理,最后一行只有一个元素的时候
在这里插入图片描述

body::after{
  content: '';
  width: calc(110px * 2);
  height: 100px;
  margin: 10px;
}

大家可以根据自己的实际情况,去扩展下,4*4等其他的场景。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用GridLayout来实现手机9宫格布局,以下是一个示例代码: ```xml <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="3" android:columnCount="3" android:alignmentMode="alignMargins" android:columnOrderPreserved="false"> <Button android:text="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="0" android:layout_column="0"/> <Button android:text="2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="0" android:layout_column="1"/> <Button android:text="3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="0" android:layout_column="2"/> <Button android:text="4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="1" android:layout_column="0"/> <Button android:text="5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="1" android:layout_column="1"/> <Button android:text="6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="1" android:layout_column="2"/> <Button android:text="7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="2" android:layout_column="0"/> <Button android:text="8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="2" android:layout_column="1"/> <Button android:text="9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="2" android:layout_column="2"/> </GridLayout> ``` 在GridLayout,我们设置了rowCount和columnCount属性来定义行数和列数,alignmentMode属性来设置对齐方式,columnOrderPreserved属性来设置列的顺序是否保留。然后,我们在每个Button上设置了layout_row和layout_column属性来指定它在GridLayout的位置。这样就可以实现手机9宫格布局了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值