CSS flex-basis如何影响其容器的宽度计算?

时间:2016-06-12 02:04:13

标签: html css css3 flexbox

在以下示例中,弹性项目的flex-basis: 30pxwidth: 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版本则不会。这是怎么发生的。

0 个答案:

没有答案