简体   繁体   English

CSS Flex-我有一个项目列表,可能需要使它们水平滚动并隐藏溢出

[英]CSS Flex - I have a list of items can need to make them horizontally scroll with overflow hidden

I have 3 items inside a div. 我的div内有3个项目。

This its container needs to be 100% widget with a horizontal scroll bar. 此容器必须是带有水平滚动条的100%小部件。

So like a carousel. 就像轮播一样。

 .wrapper { display: flex; flex: 1 1 0; overflow-x: scroll; overflow-y: hidden; } .content { display: flex; width: 500px; } .item { height: 200px; width: 100px; margin-right: 10px; } 
 <div class='wrapper'> <div class='content'> <div class='item'></div> <div class='item'></div> <div class='item'></div> </div> </div> 

Outcome: I want the items to go horizontal with a scroll bar. 结果:我希望这些项目通过滚动条水平放置。

Currently the above code breaks my flex layout. 目前,以上代码破坏了我的flex布局。 I can do this easily without flex but I'm looking for a flex solution. 我可以在没有flex的情况下轻松做到这一点,但我正在寻找一个flex解决方案。

A flex item's flex-shrink defaults to 1 , which allow it to shrink, and since your item 's as well doesn't have any content keeping them at a certain width, they will. 一个flex项目的flex-shrink默认为1 ,它允许它收缩,并且由于您的item也不包含任何将它们保持在一定宽度的内容,因此它们会。

Add flex-shrink: 0 to the item 's rule, and they won't. flex-shrink: 0添加到item的规则,但不会。

Btw, since the flex: 1 1 0; 顺便说一句,因为flex: 1 1 0; set on wrapper , and being a "flex item" property, will only apply to the wrapper if its parent as well has display: flex . wrapper设置并作为“ flex item”属性,仅在其父级也具有display: flex才适用于该wrapper


Updated 更新

If you make the content display inline-flex , you avoid the overflow of it, as an inline element grows with its content. 如果使content显示为inline-flex ,则可以避免内容溢出,因为内联元素随内容的增长而增加。

Stack snippet 堆栈片段

 .wrapper { display: flex; flex: 1 1 0; overflow-x: scroll; overflow-y: hidden; } .content { display: inline-flex; border: 2px dotted black; } .item { height: 160px; width: 250px; flex-shrink: 0; margin-right: 10px; background: red; } 
 <div class='wrapper'> <div class='content'> <div class='item'></div> <div class='item'></div> <div class='item'></div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM