我有一个溢出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%;
}
问题是最后一个孩子满溢了,为什么?我使用盒子大小调整边框?
答案 0 :(得分:7)
如何在不导致溢出的情况下在子项之间添加边距
我认为这就是你要做的事情:
* {
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;
或者,您可以使用calc
设置子容器宽度,并在flex容器上设置justify-content:space-between
* {
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;