kar*_*rim 2 html css grid flexbox
我有一个溢出flexbox的小问题:
HTML
<div class="first">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
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%;
}
Run Code Online (Sandbox Code Playgroud)
问题是,最后一个孩子满溢,为什么?我使用盒子大小到边框?
如何在子项之间添加边距而不会导致溢出
我想这就是你要做的事情:
* {
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;
}Run Code Online (Sandbox Code Playgroud)
<div class="first">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>Run Code Online (Sandbox Code Playgroud)
或者,您可以使用calc设置子宽度和justify-content:space-betweenflex-container
* {
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);
}Run Code Online (Sandbox Code Playgroud)
<div class="first">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>Run Code Online (Sandbox Code Playgroud)