Skip to content

容器

display: flex align-item: center;baseline; justify-content: space-between;flex-start;flex-end;center;space-around; flex-direction: column; row; row-reverse; column-reverse flex-wrap: nowrap; wrap; wrap-reverse align-content: flex-start;

项目属性

flex: initial (0,1,auto) 不放大、要缩小 默认 flex: auto(1 1 auto); 又放大、又缩小 flex: none(0,0,auto); 不放大、不缩小 flex: 1(1 1 0%); (取值为非负数字) ==>(赋值在第一位,后两位是固定的 1 ,0%) flex: 0(0 1 0%)

align-self: 覆盖父亲的 align-item, 定义单个 align-self order: 排序值越小越靠前

flex: 0 最小内容宽度 都是 0 flex: none 适用于不换行的内容固定或者较少的小控件元素 flex: auto 适合基于内容动态适配的布局;最大内容宽度, 按照自己的宽度计算宽度 flex: 1 适合等分布局 区别:https://juejin.cn/post/7061196914741477383#heading-7

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <style>
      .box {
        display: flex;
        width: 400px;
        outline: 1px red solid;
      }

      .item1 {
        flex: 0 2 300px;
        background-color: #32d6d6;
      }

      .item2 {
        flex: 0 1 200px;
        background-color: #e2a83e;
      }

      .item3 {
        flex: 0 2 100px;
        background-color: #b85ad0;
      }
    </style>
    <!-- ...  -->
    <div class="box">
      <div class="item1">1</div>
      <div class="item2">2</div>
      <div class="item3">3</div>
    </div>
  </body>
</html>

<!-- 最终
300 * 3/5 = 180
200 * 4/5 = 160
100 * 3/5 = 60 -->

<!-- 一 -->
<!-- 300*2+200+200 = 1000 -->
<!-- 300 - 300 * 2/1000 * 200 =  180
200 - 200 * 1/1000 * 200 =  160
100 - 100 * 1/1000 * 200 =  80 -->

<!-- 二 -->
<!-- 300 * (1-2/5) = 180
200 * (1-1/5) = 160
100 * (1-2/5) = 60 -->

<!-- 三 -->
<!-- 300 * 3/5 = 180
200 * 4/5 = 160
100 * 3/5 = 60 -->

https://mp.weixin.qq.com/s/is1ANAImt0WChsX8K2iyww

缩小情况

公式 1:- 自身的基础宽度 - 超出宽度 (自身宽度收缩比例/总权重) 其中总权重计算方式: 300 _ 2 + 200 _ 1 + 100 * 2 = 10000

= 300-2003002/1000 = 180 = 200-2002001/1000 = 160 = 100-2001002/1000 = 60

放大情况

html
<style>
  .item {
    display: flex;
    width: 500px;
    height: 100px;
  }
  .item0 {
    width: 50px;
    flex: none;
    background-color: red;
  }
  .item1 {
    flex: 1 1 50px;
    background-color: green;
  }
  .item2 {
    flex: 7 1 100px;
    background-color: blue;
  }
</style>
<body>
  <div class="item">
    <div class="item0">0</div>
    <div class="item1">1</div>
    <div class="item2">2</div>
  </div>
</body>

公式:自身的基础宽度 + 容器剩余宽度 _(自身伸张比例 / 所有子项伸张比例之和) 其中总权重计算方式: 300 _ 2 + 200 _ 1 + 100 _ 2 = 10000

剩余宽度: 500-200 = 300 = item0: 50 = item1: 50 + 3001/8= 87.5 = item2: 100 + 3007/8= 362.5

放大情况

https://juejin.cn/post/6932370503575617543 0% 对应 0 宽度 auto 对应 width 宽度

在 MIT 许可下发布