如何创建水平生长并具有水平滚动的项目列表?
这是我的代码:
.wrap-here {
display: flex;
gap: 5px;
flex-wrap: wrap;
width: 250px;
overflow: auto;
}
.item {
flex: 0 0 20%;
border: 1px solid black;
padding: 10px;
margin-bottom: 5px;
}
<div class="wrap-here">
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定
flexbox
是否可以实现。我强烈建议您在网格
中执行此操作,以实现更好的布局控件。说明
flexbox
,grid
。是的,
flex-wrap
将构成多行,但是您无法按照需要控制布局。在这种情况下,flex-wrap
的问题是,当应用时,它不会引起任何溢出,因为flex-wrap
用于将项目包装到防止溢出。该解决方案
由于您用
宽度指定了大小:250px
我将假设您始终需要2行。通过应用
网格-Auto-Flow:列
,布局将不断为每个项目制作一个新列。使用
网格 - 板板行:1FR 1FR
,布局中总会有2行。使用
网格-Auto-Collumns:20%
,每个项目将是250px
的宽度20%
的列,代码> 50px (据我了解,您的首选尺寸是您的特定flex-basis:20%
)。通过应用
Overflow-X:auto
,将从250px
容器中溢出的内容中有一个水平滚动条。I'm not sure this is achievable with
flexbox
. I'd highly recommend doing this ingrid
for much better layout control.Explanation
flexbox
is for one dimensional layouts, whilegrid
is for two dimensional layouts.Yes,
flex-wrap
will make multiple rows, but you can't control the layout as you want to. The problem withflex-wrap
in this scenario is that when applied, it won't cause any overflow, asflex-wrap
is used to wrap the items to prevent overflow.The solution
Since you specified the size with
width: 250px
I'm going to assume you always want 2 rows.By applying
grid-auto-flow: column
, the layout will continuously make a new column for each item.With
grid-template-rows: 1fr 1fr
, there will always be 2 rows in the layout.With
grid-auto-columns: 20%
, each item will be a column taking up20%
of the width of250px
, which is50px
(which is, from what I understand, your preferred size as you specificedflex-basis: 20%
).By applying
overflow-x: auto
, there will be a horizontal scrollbar from the content overflowing from the250px
container.