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)。这个值的效果就是确定在释放和分配空间的时候,成员的初值是多少。
常写的几个值的意义
- 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>
剩余空间: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;
}