gpt4 book ai didi

html - Flex box 没有相互对齐

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:36 24 4
gpt4 key购买 nike

我有两行 flexbox。第一行有 3 个元素,每个元素占 4 列。第二行有2个元素,一个占4列,另一个占8列。但是当两行堆叠在一起时,每行的大小彼此不匹配。即每行的第一项具有不同的大小。

这是我的意思的图片: https://imgur.com/nOPJaNN

.flex-display {
display: flex;
}
.pretty-container {
background-color: rgb(38, 47, 53);
margin: 5px;
padding: 10px;
position: relative;
}
.container {
position: relative;
width: 100%;
max-width: 1200px;
margin: 0 auto;
padding: 20px 20px;
box-sizing: border-box;
background-color:rgb(54, 69, 79);
}
.four.columns { width: calc(100% / 3); }
.eight.columns { width: calc(100% / 3 * 2); }

body {
font-size: 1.5em; /* currently ems cause chrome bug misinterpreting rems on body element */
line-height: 1.6;
font-weight: 400;
font-family: "Open Sans", "Helvetica Neue", "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #EBEBEB; }
<body>
<div class="container">
<div class="row flex-display" style="justify-content: space-between;">
<div id="test4-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 1</div>
<div id="test4"></div>
</div>
</div>
<div id="test5-container" class="pretty-container eight columns">
<div>
<div class="section-banner">Item 2</div>
<div id="test5"></div>
</div>
</div>
</div>
<div class="row flex-display" style="justify-content: space-between;">
<div id="test1-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 3</div>
<div id="test1"></div>
</div>
</div>
<div id="test2-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 4</div>
<div id="test2"></div>
</div>
</div>
<div id="test3-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 5</div>
<div id="test3"></div>
</div>
</div>
</div>
</div>
</body>

最佳答案

你在两者之间添加边距,所以当只有两个时,边距使用的空间减少 10px。你需要把这个额外的缺失添加到更大的盒子里。

flex-shrink, flex-grow 在这里没有任何帮助,flex-basis 也需要考虑计算那些 10px,flex 没有装订线系统;)。

.flex-display {
display: flex;
}
.pretty-container {
background-color: rgb(38, 47, 53);
margin: 5px;
padding: 10px;
position: relative;
box-sizing:border-box/* will include padding and border into calculation*/
}
.container {
position: relative;
width: 100%;

max-width: 1200px;
margin: 0 auto;
padding: 20px 20px;
box-sizing: border-box;
background-color:rgb(54, 69, 79);
}
.four.columns { width: calc(100% / 3); }
.eight.columns { width: calc(100% / 3 * 2 + 10px) ;/* add those 10px margins missing and unseen by flex when this is meant not to take room from 2 boxes */}

body {
font-size: 1.5em; /* currently ems cause chrome bug misinterpreting rems on body element */
line-height: 1.6;
font-weight: 400;
font-family: "Open Sans", "Helvetica Neue", "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #EBEBEB; }
<body>
<div class="container">
<div class="row flex-display" style="justify-content: space-between;">
<div id="test4-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 1</div>
<div id="test4"></div>
</div>
</div>
<div id="test5-container" class="pretty-container eight columns">
<div>
<div class="section-banner">Item 2</div>
<div id="test5"></div>
</div>
</div>
</div>
<div class="row flex-display" style="justify-content: space-between;">
<div id="test1-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 3</div>
<div id="test1"></div>
</div>
</div>
<div id="test2-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 4</div>
<div id="test2"></div>
</div>
</div>
<div id="test3-container" class="pretty-container four columns">
<div>
<div class="section-banner">Item 5</div>
<div id="test3"></div>
</div>
</div>
</div>
</div>
</body>

关于html - Flex box 没有相互对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243847/

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