flex-shrink 的使用

flex-shrink 根据容器大小和自己原先的尺寸 (flex-basis) ,当空间不足时适当适当缩小,看示例:

<p>The width of content is 460px; the flex-basis of the flex items is 120px.</p>
<p>A, B, C have flex-shrink:1 set. D and E have flex-shrink:2 set</p>
<p>The width of D and E is less than the others.</p>
<div id="content">
  <div class="box" style="background-color:red;">A</div>
  <div class="box" style="background-color:lightblue;">B</div>
  <div class="box" style="background-color:yellow;">C</div>
  <div class="box1" style="background-color:brown;">D</div>
  <div class="box1" style="background-color:lightgreen;">E</div>
</div>
#content {
  display: flex;
  width: 460px;
}

#content div {
  flex-basis: 120px;
}

.box {
  flex-shrink: 1;
}

.box1 {
  flex-shrink: 2;
}

A, B, C, D, E 五个 div 都是 content 容器的子元素,它们的 flex-basis 都是 120px,所以原本它们的宽度和应该是 600px,现在 content 容器宽 460px,超出 140px 由5个子元素分担缩减,ABC 减少1份,DE减少 2 份,总共是 7 份(1+1+1+2+2),所以每份是 20,ABC 减少 20px 最终宽度是 100px,DE 减少 40px 最终宽度80。

演示:

发表回复