简体   繁体   English

如何使省略号在具有动态宽度(flex:1)的flex项目上工作?

[英]How to make ellipsis work on flex item with dynamic width (flex:1)?

I'm trying to implement white-space: nowrap; 我正在尝试实现white-space: nowrap; and text-overflow: ellipsis; text-overflow: ellipsis; on a flex:1; flex:1; div without setting the max-width . div而不设置max-width I want it to adjust to the width flex:1 gives. 我希望它调整为flex:1给出的宽度。

Here is a fiddle that explains what I want and what actually happens: https://jsfiddle.net/dani3l/6y04bvu1/13/ 这是一个小提琴,解释了我想要的以及实际发生的事情: https : //jsfiddle.net/dani3l/6y04bvu1/13/

I'd like to keep the solution css only if possible. 我只想在可能的情况下保留解决方案。

Thanks in advance! 提前致谢!

You can try this in your flex:1 class: 您可以在flex:1类中尝试此操作:

.flex-1, .flex-1 > * {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Here is a snippet to show this. 这是显示此内容的摘录。

 .flex-container { display: flex; align-items: center; height: 50px; width: 120px; padding: 0 10px; background-color: black; } .flex-1 { flex: 1; color: white; } .flex-1, .flex-1 > * { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } 
 <div class="flex-container"> <div class="flex-1"> This is very long text. </div> </div> <br> <br> <div class="flex-container"> <div class="flex-1"> <div> Child divs work too </div> <br /> <span> Child spans work too </span> </div> </div> 

I found Larry Gordon's Codepen that helped me with this. 我发现拉里·戈登(Larry Gordon)的Codepen帮了我大忙

要应用文本溢出:省略号,您必须提供width或max-width,否则它不知道需要在哪里停止。

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

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