【css】多行多列流式布局

要达到这种效果,自动换行,并且左右等距离对齐:
在这里插入图片描述

方法一,使用grid布局:

      .container-1 {
        padding: 20px; 
        width: 800px;
        border: 1px solid black;
        margin: 0 auto;
        margin-bottom: 50px;

        /** key words*/
        display: grid;
        /**指定重复元素的宽度*/
        grid-template-columns: repeat(auto-fill, 180px); 
        justify-content: space-between;
        grid-gap: 1rem;
      }
      .container-1 .item {
        display: inline-block;
        height: 50px;
        border: 1px solid green;
        text-align: center;
        line-height: 50px;
      }

不好的地方就是兼容性问题了,IE并不支持,但是足够简洁,并且元素的宽度可以设置为固定宽度,也可以调整为百分比。

方法二,使用flex

      
      /**缺点就是 只能将子元素一行分成4列,切这个宽度基本没法自定义, 而且需要计算子元素的margin*/
      .container-2 {
        width: 800px;
        border: 1px solid black;
        margin: 0 auto;
        margin-bottom: 50px;
        padding:20px;

        /*** key words*/
        display: flex;
        flex-flow: row wrap;
      }

      .container-2 .item {
        width: 23%;
        height: 50px;
        border: 1px solid green;
        text-align: center;
        line-height: 50px;
        display: inline-block;
        margin: 0 1% 20px;
         /**有边框的情况必须加上此属性,不然一行会放不下4个*/
        box-sizing: border-box;
      }

缺点:如果要将每行划分为几列,不仅要计算 每列的平均,还需要计算margin的值,保证总宽度为100%。tip:如果item元素有边框,必须加上box-sizing。

方法三,使用flex
这个方法和上面同样使用弹性布局。并且常见的使用场景下,会需要额外的元素来完成上面的效果。

	 .container-3 {
        width: 800px;
        border: 1px solid black;
        margin: 0 auto;
        margin-bottom: 50px;
        padding:20px;

        /*** key words*/
        display: flex;
        flex-flow: row wrap;
      }

      .container-3 .item {
        /**需要多少列,改变这个width的比率即可,一般情况下,会将border给隐藏 */
        width: 25%; 
        display: block;
        border: 1px solid green;
        text-align: center;
        margin-bottom: 10px;
        box-sizing: border-box;
        padding: 10px;
      }

      .container-3 .item .item-inner {
        border: 1px solid red;
        line-height: 50px;
      }


缺点: 会增加额外的dom元素,也可以将width:25%修改为flex-basis: 25%

html长这样:

  <div class="container-1">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
    <div class="item">D</div>
    <div class="item">E</div>
    <div class="item">F</div>
    <div class="item">G</div>
  </div>

  <hr/>

  <div class="container-2">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
    <div class="item">D</div>
    <div class="item">E</div>
    <div class="item">F</div>
    <div class="item">G</div>
  </div>

  <hr/>



  <div class="container-3">
    <div class="item">
      <div class="item-inner">A</div>
    </div>
    <div class="item">
      <div class="item-inner">B</div>
    </div>
    <div class="item">
      <div class="item-inner">C</div>
    </div>
    <div class="item">
      <div class="item-inner">D</div>
    </div>
    <div class="item">
      <div class="item-inner">E</div>
    </div>
    <div class="item">
      <div class="item-inner">F</div>
    </div>
    <div class="item">
      <div class="item-inner">G</div>
    </div>
  </div>

效果:
在这里插入图片描述

完整html: 点击这里


appended on 2020-08-13

如果每一行的列都是满的,那么用下面的方法【供参考】

父元素样式:

    display: flex;
    flex-flow: row wrap;
    justify-content: space-between;

子元素样式:

	width: 30%;
    height: 152px;
    border: 1px solid #E4E4E4;
    background: white;
    padding-left: 16px;
    padding-right: 16px;
    margin-bottom: 20px;
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值