flex-grow
用於處理flex
容器剩餘空間。css
剩餘空間 = flex容器寬度 - (項目1flex-basis
+ 項目2flex-basis
+ ... + 項目nflex-basis
)html
項目1分配空間 = [項目1flex-grow
/ (項目1flex-grow
+ 項目2flex-grow
+ ... + 項目nflex-grow
)] * 剩餘空間css3
300px
)<div class="row"> <div class="col"></div> <div class="col"></div> </div>
.row { display: flex; width: 600px; height: 100px; } .row > .col:nth-child(1) { background: red; flex-basis: 100px; flex-grow: 0; } .row > .col:nth-child(2) { background-color: green; flex-basis: 200px; flex-grow: 0; }
<div class="row"> <div class="col"></div> <div class="col"></div> </div>
.row { display: flex; width: 600px; height: 100px; } .row > .col:nth-child(1) { background: red; flex-basis: 100px; flex-grow: 2; } .row > .col:nth-child(2) { background-color: green; flex-basis: 200px; flex-grow: 1; }
項目1:學習
項目2:flex
flex-shrink
用於處理flex
容器溢出空間。spa
溢出空間 = (項目1flex-basis
+ 項目2flex-basis
+ ... + 項目nflex-basis
) - flex容器寬度code
加權值 = (項目1flex-basis
項目1flex-shrink
) + (項目2flex-basis
項目2flex-shrink
) + (...) + (項目nflex-basis
* 項目nflex-shrink
)htm
項目1壓縮寬度 = (項目1flex-basis
項目1flex-shrink
/ 加權值) 溢出空間blog
300px
)<div class="row"> <div class="col"></div> <div class="col"></div> </div>
.row { display: flex; width: 300px; height: 100px; } .row > .col:nth-child(1) { background: red; flex-basis: 200px; flex-shrink: 0; } .row > .col:nth-child(2) { background-color: green; flex-basis: 400px; flex-shrink: 0; }
<div class="row"> <div class="col"></div> <div class="col"></div> </div>
.row { display: flex; width: 300px; height: 100px; } .row > .col:nth-child(1) { background: red; flex-basis: 200px; flex-shrink: 1; } .row > .col:nth-child(2) { background-color: green; flex-basis: 400px; flex-shrink: 2; }
項目1:ip
項目2: