简体   繁体   English

使用flex-basis来控制线上显示的项目数

[英]Using flex-basis to control the number of items displayed on the line

I want to push elements out in order to hide them while using flex. 我想推出元素以便在使用flex时隐藏它们。

For example, if flex-basis is 50%, show only two items flexed into the given space while the other elements are in overflow:hidden; 例如,如果flex-basis为50%,则只显示两个项目弯曲到给定空间,而其他元素处于overflow:hidden; territory. 领土。

Or if flex-basis is 25% then only show 4 flex elements and hide the others. 或者,如果flex-basis为25%,则仅显示4个flex元素并隐藏其他元素。

Is this possible? 这可能吗?

 .bargraph { display: flex; overflow: hidden; background: red; } .bargraph>dt { flex-basis: 50%; } 
 <dl class="bargraph"> <dt>test</dt> <dt>test</dt> <dt>test</dt> <dt>test</dt> <dt>test</dt> <dt>test</dt> </dl> 

https://jsfiddle.net/sffo02dp/ https://jsfiddle.net/sffo02dp/

In addition to flex-basis , use the flex-grow and flex-shrink properties to control sizing. 除了flex-basis ,还可以使用flex-growflex-shrink属性来控制尺寸。

More specifically: 进一步来说:

  • flex-grow: 0
  • flex-shrink: 0
  • flex-basis: 50% (or whatever you want it to be) flex-basis: 50% (或任何你想要的)

This tells flex items: don't grow, don't shrink, be a fixed percentage width 这告诉flex项目: 不增长,不缩小,是固定的百分比宽度

You can combine all these properties into one rule with the flex shorthand property: 您可以使用flex速记属性将所有这些属性合并为一个规则:

flex: 0 0 50%;       /* two items will show, OR... */
flex: 0 0 25%        /* four items will show, OR... */
flex: 0 0 20%        /* five items will show */

Here's the full code: 这是完整的代码:

 .bargraph { display: flex; overflow: hidden; background: red; } .bargraph>dt { flex: 0 0 50%; } 
 <dl class="bargraph"> <dt>test</dt> <dt>test</dt> <dt>test</dt> <dt>test</dt> <dt>test</dt> <dt>test</dt> </dl> 

Revised Fiddle 修改小提琴

you can try using this in your CSS file 您可以尝试在CSS文件中使用它

bargraph>dt{
 min-width: 10em;
}

This should be a good alternative and you can put the value based on the parent width. 这应该是一个很好的选择,您可以根据父宽度放置值。

Hope this helps 希望这可以帮助

Take care and Happy coding.. 保重和快乐编码..

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

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