flex常用固定搭配

flex常用固定搭配

flex简写:flex-grow(项目放大),flex-shrink (项目缩小),flex-basis(项目本身)

  • flex: 1;

全写:flex: 1 1 0%;内容自动放大或缩小占满剩余空间,优先最小的。

1、子元素都设置flex: 1; 子元素盒子会平分并占满父盒子,使用场景:每个子元素有相同的宽度,平分整个可用空间;

.parent {
display: flex;
}
.son{
flex:1;
}

2、一个元素宽度(或高度)固定,另一个元素宽度(或高度)自适应。

.parent {
display: flex;
}
.son1 {
width: 200px; //或者 height: 200px;
}
.son2 {
flex: 1; // flex: 1 1 0%;
}

  • flex:auto

全写:flex: 1 1 auto;

使用场景:

子元素根据内容自适应宽度。

  • flex实现每行放置具体数目,并且只有中间有间距,两边没有间距(下面例子实现每行排4个)
<template>
<div class="box">
 <div class="f" v-for="item of list">
  {{item}}
  </div>
</div>
</template>

<script lang="ts" setup>
const list = [1,2,3,4,5,6,7,8,9]
</script>
<style scss>

.box {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  background-color: yellow;
  gap: 10px 0; /* gap是子元素之间的间距,10px是上下距离,0是左右距离 */
}

.f {
  background-color: pink;
  width: calc((100% - 30px)/4); /* 每个子元素之间距离等于100%-3个空隙10px,除以4 */
  box-sizing: border-box;
  margin-right: 10px;
}
/* 移除每行第4个子元素的右边间隙 */
.f:nth-child(4n) {
  margin-right: 0px;
}
</style>

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

  • flex结合margin-left:auto,实现有的元素在最左边,有的元素在最右边
    margin-left:auto,把元素推到最右边。
<template>
<div class="box">
<div class="div1">1</div>
<div class="div2">2</div>
<div class="div3">3</div>
</div>
</template>

<script lang="ts" setup>

</script>
<style scss>
.box {
  width: 100%;
  background-color: bisque;
  display: flex;
  text-align: center;
  >div{
    width: 40px;
    line-height: 32px;
    background-color: lawngreen;
  }
  .div2{
    margin-left: 10px;
  }
  .div3{
    margin-left: auto;
  }
}
</style>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值