简体   繁体   English

截断列表项中的单个文本块

[英]Truncating an individual block of text within a list item

I have a list of items, where each item is split up into 3 parts. 我有一个项目列表,其中每个项目都分为3部分。

<li>[long part(1)] [separator(2)] [important part(3)]</li>

I want the first part ("long part") to truncate instead of wrap. 我希望第一部分(“长部分”)被截断而不是包装。

I can get the first part to not wrap, while the other parts wrap - but I can't figure out how to get the browser to truncate the first part. 我可以让第一部分不包装,而其他部分可以包装-但我不知道如何让浏览器截断第一部分。

Here's a codepen example: https://codepen.io/fiver/pen/rGRevq?editors=1010 这是一个Codepen示例: https ://codepen.io/fiver/pen/rGRevq ? editors = 1010

Use the Change view button to move the output pane to the side. 使用Change view按钮将输出窗格移到侧面。 Then you can use the slider to see the wrapping behaviour. 然后,您可以使用滑块查看包装行为。 I tried using flexbox ("flex try") and styling overflow attributes ("overflow try") but both just extend the text out off the view. 我尝试使用flexbox(“ flex try”)和样式溢出属性(“ overflow try”),但两者都只是将文本扩展到视图之外。

The third item is my workaround (just let it wrap) - it's not what I want to do, but it works. 第三项是我的解决方法(只是让它包装一下)-这不是我想要做的,但是可以。

So is there any way I can get that first part to truncate (with or without ellipsis)? 那么,有什么办法可以截断第一部分(带或不带省略号)?

I only need to get this working in modern browsers: Edge (not actually a bid deal), Chrome, Firefox and Safari (mobile and desktop). 我只需要在现代浏览器中使用它即可:Edge(实际上不是竞标交易),Chrome,Firefox和Safari(移动和台式机)。 I don't need to worry about IE or Android browser. 我不需要担心IE或Android浏览器。


Example of answer: https://codepen.io/fiver/pen/yEYWmp 答案示例: https : //codepen.io/fiver/pen/yEYWmp

Your "overflow try" works but you need to put these 2 in <li> 您的“尝试溢出”有效,但是您需要将这2个放入<li>

overflow: hidden;
text-overflow: ellipsis;

as you are using spans. 当您使用跨度时。 you need to set display property in CSS and width too. 您还需要在CSS和width设置display属性。

   span {
        display: block;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 40%; /* it be upto requirment */
    }

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

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