如何在3x2布局中与Flexbox对齐5个盒子?
我正在尝试将5个输入框与3x2布局与CSS Flexbox对齐。三个框应在第一行,两个盒子应在第二行上。我希望每个盒子看起来都以200px的宽度看起来相同,并与另一行上的相应盒子对齐。
这些盒子完全包裹了我在每个断点处的想要的方式,但是有一个小的未对准第二行的第二个盒子大约向上600-700px。我不想将CSS网格用作解决方法。我已经在代码片段中附加了工作。
什么会导致该错误,我该如何解决? 谢谢。
.my-grid {
display: flex;
flex-wrap: wrap;
row-gap: 1rem;
column-gap: 1rem;
background-color: #808080;
justify-content: space-between;
}
.item {
width: 200px;
}
.new-row {
flex-basis: 100%;
height: 0;
}
.spacer {
width: 200px;
}
<div class="my-grid">
<input class="item"></input>
<input class="item"></input>
<input class="item"></input>
<div class="new-row"></div>
<input class="item"></input>
<input class="item"></input>
<div class="spacer"></div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到的是输入字段上的默认填充。我只需添加一个通用选择器
*, *::之前, *:: at {margin:0;填充:0;盒子大小:边框框; }
到文档的开始,因为以后我总是在元素上设置这些属性,如果我不这样做,我宁愿浏览器不尝试为我做。另外,&lt; input&gt;
是自我关闭,您不需要&lt;/input&gt;
您也可以根据需要将其专门设置为输入,
输入{padding:0;}
,但输入不是唯一具有默认值的元素。What you're seeing is the default padding on input fields. I avoid this by just adding a universal selector
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
to the start of my document because I always set those attributes on my elements later on and if I don't I would rather the browser not try to do it for me. Also,<input>
is self closing, you don't need the</input>
You could also set it specifically to the input if you want,
input { padding: 0;}
but input isn't the only element with defaults.https://developer.mozilla.org/en-US/docs/Web/CSS/Universal_selectors
显示:flex
在flex方向:row
时,将彼此相邻设置一个flex-item。flex:1
是速记属性,可以使所有孩子的大小保持相同。flex-wrap:包装
,在包装溢出的flex孩子时确实需要。:nth-child
选择前三个孩子的选择器。.my-Grid .Item:nth-child(1).my-Grid .Item:nth-child(2).my-grid .Item:nth-child(3)(3)
。Imaginery -Row
中设置第两个flex -item
我使用了flex:1 0 calc(33.33%-2REM -20PX)
。display:flex
sets a flex-item next to each other, whenflex-direction:row
.flex:1
is shorthand property which will keep all child same size.flex-wrap:wrap
which is indeed need when wrapping overflowing flex child.:nth-child
selector for selecting first three child..my-grid .item:nth-child(1) .my-grid .item:nth-child(2) .my-grid .item:nth-child(3)
.flex-item
in firstimaginery-row
I have usedflex:1 0 calc(33.33% - 2rem - 20px)
.