flex:1是啥意思?

2020-06-29  本文已影响0人  redpeanuts

先说结论

flex:1 === flex:1 1 0px

flex的含义

flex是 flex-growflex-shrinkflex-basis属性的缩写
flex: <flex-grow> <flex-shrink> <flex-basis>

eg:
container_width=600px
总空间大于各basis之和,left = 600-200-300-20 = 80px,各个item均分剩余空间,80/3=26.67px

item1_width=200+22.67=226.67px
item2_width=300+22.67=326.67px
item3_width=20+22.67=46.67px

container_width=200px,总空间小于各basis之和,所以需要缩放

shrink1=1/4=0.25
shrink2=2/4=0.5
shrink3=1/4=0.25
item1_width=200*(1-0.25)=226.67px
item2_width=300+22.67=326.67px
item3_width=20+22.67=46.67px
 <div class="container">
      <div></div>
      <div></div>
      <div></div>
 </div>


.container {
  width: 100%;
  display: flex;
  height: 40px;
  div:nth-of-type(1) {
    flex: 1 1 200px;
    background: burlywood;
  }
  div:nth-of-type(2) {
    flex: 1 2 300px;
    background: cadetblue;
  }
  div:nth-of-type(3) {
    flex: 1 1 20px;
    background: chocolate;
  }
}
上一篇 下一篇

猜你喜欢

热点阅读