简体   繁体   English

在Flex中折叠文本而不指定width或flex-basis

[英]Collapse Text in Flex Without specifying width or flex-basis

I need to have a horizontal menu that will collapse text with ellipsis, but doesn't expand to full width. 我需要一个水平菜单,将使用省略号折叠文本,但不会扩展到全宽。 I've gotten it to work fine, except for IE. 我已经让它工作正常,除了IE。 If I set width or flex-basis, it will work, but then it's not set based on content size anymore. 如果我设置宽度或弹性基础,它将工作,但然后它不再基于内容大小设置。 Does anyone have any tips on a workaround? 有没有人对解决方法有任何提示? I've googled around for a fix, but nothing I've tried so far has been successful. 我已经google了一下来修复,但到目前为止我没有尝试过任何事情。

 html,body { font-family: verdana, sans-serif; } div { position: relative; width: 450px; background: #0088ff; margin-bottom: 20px; } div h2 { color: #fff; margin: 0; padding: 10px; line-height: 1em; } ul { display: flex; padding: 10px 0; margin: 0; list-style: none; } ul li { margin: 0; padding: 10px 30px; background: #bbccff; flex-grow: 0; flex-shrink: 1; flex-basis: auto; text-align: center; min-width: 0; } ul li:nth-child(2n+1) { background: #77aaff; } ul li a { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 
 <div> <h2>Should collapse</h2> <ul> <li style='flex-shrink:1'><a>Test</a></li> <li style='flex-shrink:11'><a>Test Medium</a></li> <li style='flex-shrink:13'><a>Test A Longer Phrase</a></li> </ul> </div> <div> <h2>Should not expand</h2> <ul> <li style='flex-shrink:1'><a>Test</a></li> <li style='flex-shrink:11'><a>Test Medium</a></li> </ul> </div> 

One solution to make IE behave is to add display: flex to the li so it becomes a flex container. 使IE行为的一个解决方案是将display: flex添加到li ,使其成为flex容器。

 html,body { font-family: verdana, sans-serif; } div { position: relative; width: 450px; background: #0088ff; margin-bottom: 20px; } div h2 { color: #fff; margin: 0; padding: 10px; line-height: 1em; } ul { display: flex; padding: 10px 0; margin: 0; list-style: none; } ul li { margin: 0; padding: 10px 30px; background: #bbccff; /* not needed as it is their default flex-grow: 0; flex-shrink: 1; flex-basis: auto; */ text-align: center; min-width: 0; display: flex; /* fix for IE */ } ul li:nth-child(2n+1) { background: #77aaff; } ul li a { /* display: block; not needed */ overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 
 <div> <h2>Should collapse</h2> <ul> <li style='flex-shrink:1'><a>Test</a></li> <li style='flex-shrink:11'><a>Test Medium</a></li> <li style='flex-shrink:13'><a>Test A Longer Phrase</a></li> </ul> </div> <div> <h2>Should not expand</h2> <ul> <li style='flex-shrink:1'><a>Test</a></li> <li style='flex-shrink:11'><a>Test Medium</a></li> </ul> </div> 


Another is to add overflow: hidden to the li . 另一个是添加overflow: hiddenli

 html,body { font-family: verdana, sans-serif; } div { position: relative; width: 450px; background: #0088ff; margin-bottom: 20px; } div h2 { color: #fff; margin: 0; padding: 10px; line-height: 1em; } ul { display: flex; padding: 10px 0; margin: 0; list-style: none; } ul li { margin: 0; padding: 10px 30px; background: #bbccff; /* not needed as it is their default flex-grow: 0; flex-shrink: 1; flex-basis: auto; */ text-align: center; /* min-width: 0; not needed when overflow:hidden is used */ overflow: hidden; /* fix for IE */ } ul li:nth-child(2n+1) { background: #77aaff; } ul li a { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 
 <div> <h2>Should collapse</h2> <ul> <li style='flex-shrink:1'><a>Test</a></li> <li style='flex-shrink:11'><a>Test Medium</a></li> <li style='flex-shrink:13'><a>Test A Longer Phrase</a></li> </ul> </div> <div> <h2>Should not expand</h2> <ul> <li style='flex-shrink:1'><a>Test</a></li> <li style='flex-shrink:11'><a>Test Medium</a></li> </ul> </div> 

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

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