flex: 1 是哪些属性的缩写?

flex:1是哪些属性的缩写?

flex:1 是 flex-grow: 1, flex-shrink: 1,flex-basis: 0% 的缩写;

解释下flex-grow

flex-grow是将剩余的空间,根据flex-grow的值平分,然后加到flex-basis上

javascript 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>flex-grow</title>
  </head>
  <body>
    <p>内容的宽度是500px,flex item的flex-basic是60px。</p>
    <p>A、B 为 flex-grow:1。C和D为 flex-grow:4</p>
    <div id="content">
      <div class="box" style="background-color: red">A</div>
      <div class="box" style="background-color: lightblue">B</div>
      <div class="box1" style="background-color: yellow">C</div>
      <div class="box1" style="background-color: brown">D</div>
    </div>
  </body>
</html>

上述代码 的flex-basis加起来是240px,父容器分完以后剩下260px,ABCD的flex-grow加起来10,那么每一份是26px,最终AB是60px + 26px = 86px,CD是60px + 26px * 4 = 164px。
是不是特别像老爷子突然噶了留下500块钱的遗产,兄弟4个来分,老大和老二说"我要60块钱,如果有多的可以再给我来一份",老三和老四说"我们也要60块钱,如果有多的我们要四份"。最终老大和老二分到86,老三老四分到164,大家还都挺满意。

解释下flex-shrink

javascript 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>flex-shrink</title>
    <style>
      #content {
        display: flex;
        width: 500px;
      }
      .box {
        flex-shrink: 1;
      }
      .box1 {
        flex-shrink: 4;
      }
    </style>
  </head>
  <body>
    <p>内容的宽度是500px,flex item的flex-basic总和是1000px。</p>
    <div id="content">
      <div class="box" style="background-color: red; flex-basis: 100px">A</div>
      <div class="box" style="background-color: lightblue; flex-basis: 200px">B</div>
      <div class="box1" style="background-color: yellow; flex-basis: 300px">C</div>
      <div class="box1" style="background-color: brown; flex-basis: 400px">D</div>
    </div>
  </body>
</html>

上面例子

A的flex-basis 是100px, flex-shrink 是 1

B的flex-basis 是200px, flex-shrink 是 1

C的flex-basis 是300px, flex-shrink 是 4

D的flex-basis 是500px, flex-shrink 是 4

要平分的容量 NT = 100 + 200 + 300 + 400 - 500 = 500

最终A的宽度 = 100 - 100 * 1 / (100 * 1 + 200 * 1 + 300 * 4 + 400 * 4) * NT =

83.87096774193549

B的宽度 = 200 - 200 * 1 / (100 * 1 + 200 * 1 + 300 * 4 + 400 * 4) * NT = 167.74193548387098

C的宽度 = 300 - 300 * 4 / (100 * 1 + 200 * 1 + 300 * 4 + 400 * 4) * NT = 106.45161290322582

D的宽度 = 400 - 400 * 4 / (100 * 1 + 200 * 1 + 300 * 4 + 400 * 4) * NT = 141.93548387096774

相关推荐
涛涛ing21 分钟前
2026年最值得关注的前端变化,藏在“默认值”里
前端
亿元程序员2 小时前
太牛了!这AI做出来的游戏,居然还能变现!
前端
hellice3 小时前
闪传 FlashDrop 实战:我用 300 行 Node 代码做了个「4 位取件码」临时文件中转站
前端·产品
用户0829773260053 小时前
RTX 5500 MIG 踩坑记录:切分之后模型跑不起来,排错整整一下午
前端
Lank_M3 小时前
AI 放大后的图更清楚,但它不能当原图入库
前端
Swot3 小时前
SvelteKit 数据流研究:`+page.js` 和 `+page.server.js` 到底谁在跑,数据又去了哪儿
前端·后端
用户4147679304153 小时前
Windows 触摸屏上 Swiper 轮播滑不动?可能是内部滚动容器抢走了你的手势
前端
张明君644513 小时前
从一个 Referer 引发的 403,到 SSR 服务失稳:Astro 流式渲染与缓存失效分析
前端
律宏阔3 小时前
微信小程序 + Taro + TDesign 去掉 [&&&],让普通 Tailwind 类接管局部样式
前端·微信小程序·taro
费曼学习法3 小时前
把 3 个内部服务封装成 MCP Server 的工程复盘:进程模型、超时与错误契约
前端