gpt4 book ai didi

html - flexbox 边距导致溢出

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:54 25 4
gpt4 key购买 nike

我有一个 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%;
}

问题是最后一个 child 溢出了,为什么??我使用了 box-sizing 到 border-box?

最佳答案

How to add margin between children without causing the overflow

我想这就是你想要做的:

* {
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;
}
<div class="first">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>

或者,您可以使用 calc 设置子宽度,并在 flex-container 上使用 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);
}
<div class="first">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>

关于html - flexbox 边距导致溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37257336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com