css中flex和flex-grow的区别

设置了1个class为parent且宽度为700px的div父级元素;
它有3个子元素,分别宽高为100px;
其中item2的元素flex值为1,item3的元素flex值为2

css 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    .parent {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      width: 700px;
    }
    .item {
      height: 100px;
      width: 100px;
    }
    .item1 {
      background: red;
    }
    .item2 {
      background: yellow;
      flex: 1;
    }
    .item3 {
      background: blue;
      flex: 2;
    }
  </style>
</head>
<body>
  <div class="parent">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
  </div>
</body>
</html>

此时预览效果如下:


可以看到item2的元素宽度为200px,item3的元素宽度为400px。

如果将flex改为flex-grow即:

css 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    .parent {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      width: 700px;
    }
    .item {
      height: 100px;
      width: 100px;
    }
    .item1 {
      background: red;
    }
    .item2 {
      background: yellow;
      flex-grow : 1;
    }
    .item3 {
      background: blue;
      flex-grow: 2;
    }
  </style>
</head>
<body>
  <div class="parent">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
  </div>
</body>
</html>

则效果如下:


此时item2的元素宽度为233.34px,item3的元素宽度为366.66px。

可以发现flex和flex-grow产生的结果并不一致,这是因为二者计算方式并不同;

flex计算方式:

总宽度700px,减去没有flex的元素宽度,此时只有一个item1且宽度为100px,则可分配宽度为600px,item2的flex值为1,item3的flex值为2,所以将可分配宽度600px分成3份,item2是一份200px,item3是两份所以是400px;

flex-grow计算方式:

总宽度700px,减去初始的所有子元素宽度,此时item1,item2,item3分别为100px,则可分配宽度为400px,item2的flex-grow值为1,item3的flex-grow值为2,所以将可分配宽度400px分成3份,item2是一份133.34px,item3是两份所以是266.66px;此时item2和item3需要再加上原本宽度100px,所以最终item2宽度为233.34px,item3宽度为366.66px;

分出的子元素宽度小数位很多的情况下,flex布局会将其转化为2位小数,且为了保证元素之间加和等于总宽度,将进行自适应舍去多余小数或者小数点后第二位加1

一句话概括:
flex是总宽度减去 非flex元素的宽度 然后等分;
flex-grow是总宽度减去 初始的所有子元素宽度 等分后 再加上初始宽度


额外示例

css 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    .parent {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      width: 700px;
    }
    .item {
      height: 100px;
    }
    .item1 {
      width: 100px;
      background: red;
    }
    .item2 {
      width: 200px;
      background: yellow;
      flex: 1;
    }
    .item3 {
      width: 200px;
      background: blue;
      flex: 2;
    }
  </style>
</head>
<body>
  <div class="parent">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
  </div>
</body>
</html>

此时为flex,所以item2的宽度为 (700 - 100) / 3 * 1 = 200px

item3的宽度为 (700 - 100) / 3 * 2 = 400px


css 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    .parent {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      width: 700px;
    }
    .item {
      height: 100px;
    }
    .item1 {
      width: 100px;
      background: red;
    }
    .item2 {
      width: 200px;
      background: yellow;
      flex-grow: 1;
    }
    .item3 {
      width: 200px;
      background: blue;
      flex-grow: 2;
    }
  </style>
</head>
<body>
  <div class="parent">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
  </div>
</body>
</html>

此时为flex-grow,所以item2的宽度为 (700 - 500) / 3 * 1 + 200 = 266.66px

item3的宽度为 (700 - 500) / 3 * 2 + 200 = 333.34px

相关推荐
子兮曰3 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰3 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝3 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋3 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁3 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
汉堡大王95274 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大4 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师4 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端
沙蒿同学4 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端