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

相关推荐
Avan_菜菜1 小时前
AI 能写代码了,为什么我反而开始要求它先写文档?
前端·github·ai编程
爱勇宝5 小时前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱
android·前端·程序员
IT_陈寒8 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen8 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
牧艺9 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
红尘散仙10 小时前
想写一个像样的终端 App?试试把 React 的开发体验搬进 Rust TUI
前端·rust
袋鼠云数栈UED团队10 小时前
一套 Spec-First 的 AI 编程工作流
前端·人工智能
袋鼠云数栈前端10 小时前
一套 Spec-First 的 AI 编程工作流
前端·ai+
angerdream10 小时前
Android手把手编写儿童手机远程监控App之vue3 路由守卫
前端
不服老的小黑哥10 小时前
AI规范驱动编程-harness工程项目实战
前端