詳解flex布局中flex-grow與flex-shrink的計算方式
CSS 中的 Flex(彈性布局) 可以很靈活的控制網(wǎng)頁的布局,其中決定 Flex 布局內(nèi)項目寬度/高度的是三個屬性:
flex-basis, flex-grow, flex-shrink.
flex-basis
flex-basis 決定了項目占據(jù)主軸的空間,除非使用 box-sizing 進(jìn)行設(shè)置,否則它將設(shè)置內(nèi)容框的大小,因此當(dāng)你指定一個flex項的大小時需要額外小心,因為它很肯能包含內(nèi)邊距與邊框。
你可以為其指定一個具體的CSS尺寸值,也可以指定其占據(jù)父元素的百分比,它的默認(rèn)值為 auto(根據(jù)內(nèi)容自動調(diào)整大小)
<!-- demo-1 -->
<div class="parent">
<div class="child1">100px</div>
<div class="child2">200px</div>
</div>
<div class="parent">
<div class="child1">10%</div>
<div class="child2">20%</div>
</div>
<div class="parent">
<div class="child1">30%</div>
<div class="child2">auto</div>
</div>
<style>
.parent {
width: 500px;
display: flex;
margin-bottom: 15px;
text-align: center;
background-color: #eeeeee;
}
/** 像素值*/
.parent:nth-child(1) .child1 {
flex-basis: 100px;
background-color: #356969
}
.parent:nth-child(1) .child2 {
flex-basis: 200px;
background-color: #369925;
}
/** 百分比 */
.parent:nth-child(2) .child1 {
flex-basis: 10%;
background-color: #356969
}
.parent:nth-child(2) .child2 {
flex-basis: 20%;
background-color: #369925;
}
/** 自動 */
.parent:nth-child(3) .child1 {
flex-basis: 30%;
background-color: #356969
}
.parent:nth-child(3) .child2 {
flex-basis: auto;
background-color: #369925;
}
</style>

flex-grow
flex-grow 設(shè)置當(dāng) flex 容器存在剩余空間(flex容器的大小減去所有flex項的大小之和)時項目的放大比例,它的默認(rèn)值為 0 (即使存在剩余空間也不放大)。如果所有項目的 flex-grow 屬性值都是相同的,則它們將等分剩余空間,否則,將根據(jù)不同的屬性值所定義的比率進(jìn)行分配。
例如,主軸長度為600px, 項目1占據(jù)50px, 項目2占據(jù)100px, 項目3占據(jù)150px, 則剩余空間為:600px - (50px + 100px + 150px) = 300px
假如每個項目的 flex-grow 屬性值都相同(例如都為1),則所有項目分配到相同的剩余空間:
- 項目1: 300px * (1 / (1 + 1 + 1)) = 100px;
- 項目2: 300px * (1 / (1 + 1 + 1)) = 100px;
- 項目3: 300px * (1 / (1 + 1 + 1)) = 100px;
<!-- demo-2 -->
<div class="parent">
<div class="child1">50px + 100px</div>
<div class="child2">100px + 100px</div>
<div class="child3">150px + 100px</div>
</div>
<style>
.parent {
width: 600px;
display: flex;
text-align: center;
color: #eee;
}
.child1 {
flex-basis: 50px;
flex-grow: 1;
background-color: #0066CC;
}
.child2 {
flex-basis: 100px;
flex-grow: 1;
background-color: #009900;
}
.child3 {
flex-basis: 150px;
flex-grow: 1;
background-color: #CC3300;
}
</style>

假設(shè)每個項目的 flex-grow 屬性的值并不都是相同的,例如項目1為 1, 項目2為 2, 項目3為 3, 則它們分配到的剩余空間分別為:
- 項目1: 300px * (1 / (1 + 2 + 3)) = 50px;
- 項目2: 300px * (2 / (1 + 2 + 3)) = 100px;
- 項目3: 300px * (3 / (1 + 2 + 3)) = 150px;
<!-- demo-3 -->
<div class="parent">
<div class="child1">50px + 50px</div>
<div class="child2">100px + 100px</div>
<div class="child3">150px + 150px</div>
</div>
<style>
.parent {
width: 600px;
display: flex;
text-align: center;
color: #eee;
}
.child1 {
flex-basis: 50px;
flex-grow: 1;
background-color: #0066CC;
}
.child2 {
flex-basis: 100px;
flex-grow: 2;
background-color: #009900;
}
.child3 {
flex-basis: 150px;
flex-grow: 3;
background-color: #CC3300;
}
</style>

要是屬性值為小數(shù)怎么辦呢?這里分兩種情況:
1. 所有flex項的 flex-gorw 屬性值之和大于1,仍然按照上述方式進(jìn)行計算;
2. 所有flex項的 flex-gorw 屬性值之和小于1,基值按照1來進(jìn)行計算,例如項目1為 0.2, 項目2為 0.3, 項目3為 0.4, 則它們分配到的剩余空間分別為:
- 項目1: 300px * (0.2 / 1) = 60px;
- 項目2: 300px * (0.3 / 1) = 90px;
- 項目3: 300px * (0.4 / 1) = 120px;
<!-- demo-4 -->
<div class="parent">
<div class="child1">50px + 60px</div>
<div class="child2">100px + 90px</div>
<div class="child3">150px + 120px</div>
</div>
<style>
.parent {
width: 600px;
display: flex;
text-align: center;
color: #eee;
}
.child1 {
flex-basis: 50px;
flex-grow: 0.2;
background-color: #0066CC;
}
.child2 {
flex-basis: 100px;
flex-grow: 0.3;
background-color: #009900;
}
.child3 {
flex-basis: 150px;
flex-grow: 0.4;
background-color: #CC3300;
}

flex-shrink
flex-shrink 設(shè)置當(dāng) flex 容器空間不足時項目的放大比例,它的默認(rèn)值為 1 (空間不足時該項目將縮小)。
flex-shrink 的計算方式與 flex-grow 略有不同,有兩個因素影響 flex 項該縮小多少,一個是 flex-shrink 的屬性值,另一個是 flex 項本身的大小,它們按各自的權(quán)重進(jìn)行縮小,舉例來說:
主軸長度為600px, 項目1占據(jù)100px, 項目2占據(jù)300px, 項目3占據(jù)500px, 每個項目的 flex-shrink 屬性值分別為1,3,2, 則總權(quán)重為 100px 1 + 300px 3 + 500px *2 = 2000px, 每個項目的權(quán)重分別為為:
- 項目1: (100px * 1) / 2000px = 0.05;
- 項目2: (300px * 3) / 2000px = 0.45;
- 項目3: (500px * 2) / 2000px = 0.50;
溢出的空間長度為:100px + 300px + 500px - 600px = 300px;
那么每個項目分別縮小:
- 項目1: 300px * 0.05 = 15px;
- 項目2: 300px * 0.45 = 135px;
- 項目3: 300px * 0.50 = 150px;
<!-- demo-5 -->
<div class="parent">
<div class="child1">100px - 15px</div>
<div class="child2">300px - 135px</div>
<div class="child3">500px - 150px</div>
</div>
<style>
.parent {
width: 600px;
display: flex;
text-align: center;
color: #eee;
}
.child1 {
flex-basis: 100px;
flex-shrink: 1;
background-color: #0066CC;
}
.child2 {
flex-basis: 300px;
flex-shrink: 3;
background-color: #009900;
}
.child3 {
flex-basis: 500px;
flex-shrink: 2;
background-color: #CC3300;
}
</style>

同樣的,當(dāng) flex-shrink 的值為小數(shù)時,也分兩種情況:
1. 所有flex項的 flex-shrink 屬性值之和大于1,仍然按照上述方式進(jìn)行計算;
2. 所有flex項的 flex-shrink 屬性值之和小于1,只收縮溢出空間的一部分,例如項目1為 0.1, 項目2為 0.3, 項目3為 0.2, 則總的收縮空間為:
300px * (0.1 + 0.3 + 0.2) = 180px
每個項的權(quán)重計算方式是不變的,每個項目分別縮小:
- 項目1: 180px * 0.05 = 9px;
- 項目2: 180px * 0.45 = 81px;
- 項目3: 180px * 0.50 = 90px;
<!-- demo-6 -->
<div class="parent">
<div class="child1">100px - 9px</div>
<div class="child2">300px - 135px</div>
<div class="child3">500px - 90px</div>
</div>
<style>
.parent {
width: 600px;
display: flex;
text-align: center;
color: #eee;
}
.child1 {
flex-basis: 100px;
flex-shrink: 0.1;
background-color: #0066CC;
}
.child2 {
flex-basis: 300px;
flex-shrink: 0.3;
background-color: #009900;
}
.child3 {
flex-basis: 500px;
flex-shrink: 0.2;
background-color: #CC3300;
}
</style>

由于只收縮了溢出空間的一部分,div 內(nèi)的元素總寬度實際上是超出 div 的寬度的。
以上就是關(guān)于使用flex布局中 flex-grow 與 flex-shrink 計算方式的簡單介紹。
該篇博客內(nèi)的代碼已同步到Github
參考資料:
[1]. MDN文檔 https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-basis
[2]. MDN文檔 https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-grow
[3]. MDN文檔 https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-shrink
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

flex-grow、flex-shrink、flex-basis和九宮格布局理解
這篇文章主要介紹了flex-grow、flex-shrink、flex-basis和九宮格布局理解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要2019-07-25
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于flex-shrink如何計算?flex-shrink的計算方法介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助2018-10-25CSS3 Flexbox中flex-shrink屬性的用法示例介紹
當(dāng)flex items的大小超過了flex container時, 各個flex item的壓縮比例,下面有個不錯的教程,大家可以參考下2013-12-30



