flexbox边缘导致溢出

时间:2016-05-16 15:15:39

标签: html css grid flexbox

我有一个溢出flexbox的小问题:

html

<div class="first">
            <div class="child"></div>
            <div class="child"></div>
            <div class="child"></div>
</div>

CSS

* {
    box-sizing: border-box;
}

body {
    margin: 50px;
}
.first {
    display: flex;
    flex-flow: row nowrap;
}

.child {
    background: red;
    border: 1px blue solid;
    height: 10px;
    flex: 0 0 33.3%;

    margin-right: 10px;
}

.first :last-child {
    flex: 0 0 33.4%;
}

问题是最后一个孩子满溢了,为什么?我使用盒子大小调整边框?

1 个答案:

答案 0 :(得分:7)

  

如何在不导致溢出的情况下在子项之间添加边距

我认为这就是你要做的事情:

&#13;
&#13;
* {
  box-sizing: border-box;
}

body {
  margin: 50px;
}

.first {
  display: flex;
  flex-flow: row nowrap;
  border:1px solid green;
}

.child {
  background: red;
  border: 1px blue solid;
  height: 10px;
  flex: 1; /* equal widths */
  margin-right: 10px;
}
.first :last-child {
  margin-right: 0;
}
&#13;
<div class="first">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>
&#13;
&#13;
&#13;

或者,您可以使用calc设置子容器宽度,并在flex容器上设置justify-content:space-between

&#13;
&#13;
* {
  box-sizing: border-box;
}
body {
  margin: 50px;
}
.first {
  display: flex;
  flex-flow: row nowrap;
  border: 1px solid green;
  justify-content: space-between;
}
.child {
  background: red;
  border: 1px blue solid;
  height: 10px;
  width: calc((100% - 20px)/3);
  /* or */
  flex: 0 0 calc((100% - 20px)/3);
}
&#13;
<div class="first">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>
&#13;
&#13;
&#13;