flex:1 | 0 | auto | none

flex: 1 | 0 | auto | none

flex属性是flex-grow、flex-shrink、flex-basis三个属性的简写,默认值是flex: 0 1 auto。

flex-grow(扩展量): 对剩余空间的占用量,只有在有剩余空间时有效,默认值为0。
flex-shrink(收缩量): 当成员的空间超出整行的空间时,成员按比例缩小,默认值为1,该属性只有在成员空间超出整体空间时有效。
flex-basis: 表示在分配额外空间之前,成员占据的空间,默认值为auto,意思就是本来占多少就是多少。但也可以自己设置长度(px)。这个值的效果就是确定在释放和分配空间的时候,成员的初值是多少。

常写的几个值的意义

  1. flex: 1 和 flex: auto
    flex:1(1 1 0%)
    flex:auto(1 1 auto)
    flex:auto和flex:1的区别只在于flex-basis这个属性,auto表示基准值(取成员设置的width),0%表示0,无尺寸,以实际内容宽度为主,会覆盖设置的width
<!DOCTYPE html>
<html>
<head>
<style>
    .container{
      display: flex;
      width: 600px;
      height: 600px;
      border: 1px solid #000;
      flex-direction: row;
      flex-wrap: wrap;
      align-items: flex-start;
      align-content: flex-start;
    }
    .mid,.left,.right{
        width: 100px;
        height: 100px;
        color: #fff;
        font-size: 30px;
        text-align: center;
        line-height: 100px;
    }

    .blue{
        flex: 1;  /*flex:1 1 0%, flex-basis为0%,会覆盖width,以实际内容为准*/
        background-color: blue;
    }
    .red{
        flex: auto;  /*flex:1 1 auto, flex-basis为auto,以设置的宽width为准,width:100px*/
        background-color: red;
    }
    .yellow{
        flex: 1 1 200px; /*flex-basis为200px,宽度为200*/
        background-color: yellow;
    }

 </style>
</head>
<body>

<div class="container">
  <div class="left blue">1</div>
  <div class="mid red">2</div>
  <div class="right yellow">3</div>
</div>
</body>
</html>

flex:1and auto
剩余空间:600-(0+100+200)=300px;
每部分扩展比例:1/3;
part1:0+300*1/3=100;
part2:100+300*1/3=200;
part3: 200+300*1/3=300;
2. flex: 0 和 flex:none
flex:0(0 1 0)
flex: none(0 0 auto)

.blue{
   flex: 0;  /*flex:0 1 0%, flex-basis为0%,会覆盖width,以实际内容为准*/
   background-color: blue;
}
.red{
   flex: none;  /*flex:0 0 auto, flex-basis为auto,以设置的宽width为准,width:100px*/
   background-color: red;
}

flex:0和none

参考

https://blog.51cto.com/u_9161018/4952822

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值