使用弹性基础时弹性项目溢出

时间:2019-05-09 15:36:53

标签: css flexbox

我想使用flex-basis属性在元素之间创建间隔符。这样做可以使我在元素之间保持空间,而不必考虑“弯曲方向”。

这在使用flex-direction: column时效果很好,但是在使用flex-direction: row(默认值)时,该行的最后一项溢出并且被裁剪。

enter image description here

此处的完整示例:https://codepen.io/anon/pen/NVxaoy

input {
  margin: 0;
}

.field {
  display: flex;
}

.field.stacked {
  flex-flow: column;
}

.label {
  display: flex;
}

.spacer.x1 {
  background: green;
  flex: 0 0 8px;
}

.spacer.x2 {
  background: red;
  flex: 0 0 16px;
}
<h1>Horizontal</h1>
<div class="field">
  <label class="label">
            <input type="checkbox">
            <div class="spacer x1"></div>
            <div class="text">Apple</div>
        </label>
  <div class="spacer x2"></div>
  <label class="label">
            <input type="checkbox" />
            <div class="spacer x1"></div>
            <div class="text">Banana</div>
        </label>
  <div class="spacer x2"></div>
  <label class="label">
            <input type="checkbox" />
            <div class="spacer x1"></div>
            <div class="text">Orange</div>
        </label>
</div>
<h1>Stacked</h1>
<div class="field stacked">
  <label class="label">
            <input type="checkbox">
            <div class="spacer x1"></div>
            <div class="text">Apple</div>
        </label>
  <div class="spacer x2"></div>
  <label class="label">
            <input type="checkbox" />
            <div class="spacer x1"></div>
            <div class="text">Banana</div>
        </label>
  <div class="spacer x2"></div>
  <label class="label">
            <input type="checkbox" />
            <div class="spacer x1"></div>
            <div class="text">Orange</div>
        </label>
</div>

是否有解决方案或解决方法?

1 个答案:

答案 0 :(得分:1)

老实说,我不确定为什么会出现这种裁剪,但是您可以通过更改间隔符来使用width而不是flex来解决它

input { margin: 0 }

.field {
  display: flex;
}

.field.stacked {
  flex-flow: column;
}

.label {
  display: flex;
}

.spacer.x1 {
  background: green;
  width: 8px;
}
.spacer.x2 {
  background: red;
  flex: 0 0 16px;
}
<h1>Horizontal</h1>
<div class="field">
  <label class="label">
    <input type="checkbox">
    <div class="spacer x1"></div>
    <div class="text">Apple</div>
  </label>
  <div class="spacer x2"></div>
  <label class="label">
    <input type="checkbox" />
    <div class="spacer x1"></div>
    <div class="text">Banana</div>
  </label>
  <div class="spacer x2"></div>
  <label class="label">
    <input type="checkbox" />
    <div class="spacer x1"></div>
    <div class="text">Orange</div>
  </label>
</div>

<h1>Stacked</h1>
<div class="field stacked">
  <label class="label">
    <input type="checkbox">
    <div class="spacer x1"></div>
    <div class="text">Apple</div>
  </label>
  <div class="spacer x2"></div>
  <label class="label">
    <input type="checkbox" />
    <div class="spacer x1"></div>
    <div class="text">Banana</div>
  </label>
  <div class="spacer x2"></div>
  <label class="label">
    <input type="checkbox" />
    <div class="spacer x1"></div>
    <div class="text">Orange</div>
  </label>
</div>