一、子项默认值 flex: 0 1 auto
,表现如下。
- 子项宽度和 < 父元素宽时, 不占剩余空间
<body>
<div class="wrap">
<div class="lf son">所发生的方式得分</div>
<div class="rt son">sdfs我是</div>
</div>
</body>
<style>
.wrap {
display: flex;
border: 5px black solid;
width: 300px;
}
.lf {
background-color: lightgreen;
}
.rt {
background-color: orange;
}
</style>
- 子项宽度和 > 父元素宽时,在
各自原宽度
上按1:1系数缩放
<body>
<div class="wrap">
<div class="lf son">所发生的方式得分 超长超长超长超长超长超长超长超长</div>
<div class="rt son">sdfs我是</div>
</div>
</body>
<style>
.wrap {
display: flex;
border: 5px black solid;
width: 300px;
}
.lf {
background-color: lightgreen;
}
.rt {
background-color: orange;
}
</style>
二、子项主动设置 flex: 1 1 auto
在css中加上如下代码
.son {
flex: 1 1 auto;
}
-
不超宽时,在
各自原宽度
基础上按1:1占剩余空间
-
超宽时,在
各自原宽度
上按1:1系数缩放
三、子项主动设置 flex: 1
在css中加上如下代码
.son {
flex: 1;
}
-
不超宽时,等分
-
超宽时,等分
四、子项主动设置 flex: 1 1 20px(随便数值)
或flex: 1 1 30%(随便百分比)
-
不超宽时,等分
-
超宽时,等分
结论:
- 子项的
flex: 1
不是flex: 1 1 auto
的缩写 flex:1
的作用可以达到等分空间,不论子项原始宽度
是多少,通过上述可发现关键在于flex-basis
值引起的等分。