在以下示例中,弹性项目的flex-basis: 30px
和width: 30px
将导致不同的容器宽度(弹性基础更短)。
flex-basis
如何影响其父宽度,如下例所示:
.flex-container {
padding: 0;
margin: 0;
display: flex;
border: 1px solid blue;
}
.grand {
display: flex;
border: 1px solid green;
}
.flex-item {
background: tomato;
padding: 10px;
border: 1px solid red;
color: white;
font-size: 2em;
}
.flex1 {
flex-shrink: 0; /* added according to Michael_B's answer */
flex-basis: 100px;
}
<div class="grand">
<div class="flex-container">
<div class="flex-item flex1">1</div>
<div class="flex-item flex2">22222 222222222</div>
</div>
</div>
这是预期的行为:
.flex-container {
padding: 0;
margin: 0;
display: flex;
border: 1px solid blue;
}
.grand {
display: flex;
border: 1px solid green;
}
.flex-item {
background: tomato;
padding: 10px;
border: 1px solid red;
color: white;
font-size: 2em;
}
.flex1 {
/* flex-basis: 100px; */
width: 100px;
}
<div class="grand">
<div class="flex-container">
<div class="flex-item flex1">1</div>
<div class="flex-item flex2">22222 222222222</div>
</div>
</div>
在flex-basis
版本中,flex2
项目将包装文本,而width
版本则不会。这是怎么发生的。