将多个DIV放在一行显示的三种方法(超简洁,一目了然)

1.先设置一个DIV,里面套4个子div,并设置样式,width用像素或者%百分比表示时:

<template>
    <div id="contain">
        <div ref="main" class="main"></div>
        <div ref="main1" class="main"></div>
        <div ref="main2" class="main"></div>
        <div ref="main3" class="main"></div>
    </div>
</template>
#contain{
    display: flex;
    //挤不下换行
    flex-wrap: wrap;
    //展开铺满,justify-content:center;则代表居中
    justify-content:space-between;
}
.main{
    width: 500px;
    height:350px;  
}

2.width用vh表示时,父div中要加上position: fixed;:

<template>
    <div id="contain">
        <div ref="main" class="main"></div>
        <div ref="main1" class="main"></div>
        <div ref="main2" class="main"></div>
        <div ref="main3" class="main"></div>
    </div>
</template>
#contain{
    position: fixed;
    display: flex;
    //挤不下换行
    flex-wrap: wrap;
    justify-content:space-between;
}
.main{
    width: 40vh;
    height:350px;  
}

可以注意到子div无需加display: inline-block; 也可以实现。

效果如下

当width为40vh时,此时一行可装下:
在这里插入图片描述
当width为50vh时,此时一行装不下,自动换行:
在这里插入图片描述
**注意:**当一页装不下时,可在父DIV中设置 overflow: auto;使其未展示部分可上下滑动,不过此时父DIV不能用 position: fixed属性,也就是width不能用vh或vw表示,否则滑动条失效。
在这里插入图片描述
在这里插入图片描述
补充:也可以用antd组件中的space组件,将若干个DIV或者组件放在space中,设置size大小即space组件中每个组件的间隔。另外Row与Col组件的结合使用也可以达到以上效果,当无法确定具体间隔时推荐Row与Col组件。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

定位算法工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值