本文介绍了如何使用 flexbox 将四列包装成两行两列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们正在使用 flexbox 并尝试以下内容.目标是每个项目在移动断点上有一列,在平板电脑断点上有两列,在桌面断点上有四列.
该示例仅使用了四个项目,但假设我有 5 或 6 个项目,那么我只希望这些项目具有响应性.如果该行只有足够的空间来显示每行 2 个项目,那么我希望每个项目继续移动到其上方的行下方.
如何实现?
.flex-row {显示:弹性;@media 屏幕和(最小宽度:768px){弹性:1;弹性方向:行;对齐内容:间隔;}}.flex-col {边距:6px;填充:16px;背景颜色:黑色;显示:弹性;对齐内容:居中;对齐项目:居中;弹性:1;弹性方向:列;白颜色;}
<div class="flex-col">在没有跨职能流程改进的情况下,果断地协商可互操作的门户.</div> 极大地激励战术最佳实践.<div class="flex-col">无缝提升竞争力.</div><div class="flex-col">相对于即插即用的信息媒体,以用户为中心的思想共享进行了独特的优化.无缝优化有影响力的解决方案和启用的基础设施.</div><div class="flex-col">通过大流行供应链动态扩展灵活的催化剂以实现变化.有效地.
当前结果
平板电脑的预期结果
解决方案
只需添加 flex-wrap:wrap
就可以让元素在没有足够空间的情况下跳到下一行并考虑媒体查询如果你想控制什么时候休息:
.flex-row {显示:弹性;弹性:1;弹性方向:行;flex-wrap: 包裹;}.flex-col {边距:6px;填充:16px;背景颜色:红色;显示:弹性;对齐内容:居中;对齐项目:居中;弹性:1;弹性方向:列;白颜色;box-sizing:border-box;}@media(最大宽度:767px){.flex-col {flex-basis: calc(50% - 12px);}}@media(最大宽度:460px){.flex-col {弹性基础:100%;}}
<div class="flex-col">在没有跨职能流程改进的情况下,果断地协商可互操作的门户.</div> 极大地激励战术最佳实践.<div class="flex-col">无缝提升竞争力.</div><div class="flex-col">相对于即插即用的信息媒体,以用户为中心的思想共享进行了独特的优化.无缝优化有影响力的解决方案和启用的基础设施.</div><div class="flex-col">通过大流行供应链动态扩展灵活的催化剂以实现变化.有效地.
We are using flexbox and attempting to the following below. The goal is to have one column per item on mobile breakpoints, two columns on tablet breakpoints, and four columns on desktop breakpoints.
The example uses just four items, but let's say i had 5 or 6 items, then i just wanted the items to be responsive. If the row only has enough space to display 2 items per row, then i would expect each item to continue to move below the row above it.
How can this be achieved?
.flex-row {
display: flex;
@media screen and (min-width: 768px) {
flex: 1;
flex-direction: row;
justify-content: space-between;
}
}
.flex-col {
margin: 6px;
padding: 16px;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
flex: 1;
flex-direction: column;
color: white;
}
<div class="flex-row">
<div class="flex-col">Assertively negotiate interoperable portals without cross functional process improvements. Dramatically incentivize tactical best practices with.</div>
<div class="flex-col">Seamlessly grow competitive.</div>
<div class="flex-col">Distinctively optimize user-centric mindshare vis-a-vis plug-and-play infomediaries. Seamlessly optimize impactful solutions and enabled infrastructures.</div>
<div class="flex-col">Dynamically extend flexible catalysts for change via pandemic supply chains. Efficiently.</div>
</div>
Current results
Expected results on tablet
解决方案
Simply add flex-wrap:wrap
to allow element to go to the next line if there is no enough space and consider media query if you want to control when the break will happen:
.flex-row {
display: flex;
flex: 1;
flex-direction: row;
flex-wrap: wrap;
}
.flex-col {
margin: 6px;
padding: 16px;
background-color: red;
display: flex;
justify-content: center;
align-items: center;
flex: 1;
flex-direction: column;
color: white;
box-sizing:border-box;
}
@media (max-width:767px) {
.flex-col {
flex-basis: calc(50% - 12px);
}
}
@media (max-width:460px) {
.flex-col {
flex-basis: 100%;
}
}
<div class="flex-row">
<div class="flex-col">Assertively negotiate interoperable portals without cross functional process improvements. Dramatically incentivize tactical best practices with.</div>
<div class="flex-col">Seamlessly grow competitive.</div>
<div class="flex-col">Distinctively optimize user-centric mindshare vis-a-vis plug-and-play infomediaries. Seamlessly optimize impactful solutions and enabled infrastructures.</div>
<div class="flex-col">Dynamically extend flexible catalysts for change via pandemic supply chains. Efficiently.</div>
</div>
这篇关于如何使用 flexbox 将四列包装成两行两列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!