flex布局 常用的场景

  • 实现一个div绝对居中
    效果图:
    在这里插入图片描述
    .wrapper {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .item {
        width: 100px;
        height: 100px;
        background: red;
    }
    
 <div class="wrapper">
      <view class="item"></view>
 </div>
  1. 俩端对其,中间均分
    在这里插入图片描述
    .content {
        display: flex;
        height: 100px;
        background: yellow;
        justify-content: space-between;
        width: 400px;
    }
    .box {
        width: 100px;
        height: 100%;
        background: red;
    }

    <view class="content">
        <view class="box"></view>
        <view class="box"></view>
        <view class="box"></view>
    </view>
  1. 左右间距一样,全部均分
    在这里插入图片描述
    .content {
        display: flex;
        height: 100px;
        background: yellow;
        justify-content: space-around;
        width: 400px;
    }
    .box {
        width: 100px;
        height: 100%;
        background: red;
    }

    <view class="content">
        <view class="box"></view>
        <view class="box"></view>
        <view class="box"></view>
    </view>
  1. 一个列表 一行三个 ,多行(下图的有问题)
    在这里插入图片描述
<view class="content2">
        <view class="box2"></view>
        <view class="box2"></view>
        <view class="box2"></view>
        <view class="box2"></view>
        <view class="box2"></view>
        <!-- <view class="box2"></view> -->
</view>


    .content2 {
        width: 330px;
        display: flex;
        flex-wrap: wrap;
        background: yellow;
        justify-content: space-between;
    }
    .box2 {
        width: 100px;
        height: 100px;
        background: red;
        margin-bottom: 15px;
    }

解决方法:(多加几行代码)

.content2:after {
        content: '';
        width: 100px;
        visibility: hidden;
        margin-bottom: 15px;
}

完美!! 效果图如下
在这里插入图片描述

  1. 一个(多个)div 右对齐
    在这里插入图片描述
    <view class="content">
        <view class="box"></view>
        <!-- <view class="box"></view> -->
    </view>

    .content {
        display: flex;
        height: 100px;
        background: yellow;
        justify-content: flex-end;   
        width: 400px;
    }
    
    .box {
        width: 100px;
        height: 100%;
        background: red;
        margin-left: 10px;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值