Ellipsis in the middle of a text (Mac style)

后端 未结 15 2263
长情又很酷
长情又很酷 2020-11-29 03:04

I need to implement ellipsis (\"...\") in the middle of a text within a resizable element. Here is what it might look like. So,

\"Lorem ipsum do         


        
15条回答
  •  孤独总比滥情好
    2020-11-29 03:25

    I'd like to propose mine example of solving this problem.

    The main idea is to split text in two even parts (or first is bigger, if the length is odd) one of which has ellipsis in the end and another aligned right with text-overflow: clip.

    So all you need to do with js, if you want to make it automatic/universal - is to split string and set attributes.

    It has some disadvantages, though.

    1. No nice wrapping by words, or even letters (text-overflow: '' works only in FF at the moment)
    2. If the split happens between words - space should be in the first part. Otherwise, it will be collapsed.
    3. End of the string should not have any exclamation marks, due to direction: rtl - they will be moved to the left of the string. I think, it is possible to fix this with putting right part of the word in the tag and exclamation mark in the ::after pseudo-element. But I haven't yet made it properly working.

    But, with all of these, it looks really cool to me, especially when you dragging the border of the browser, which you can do on the jsfiddle page easily: https://jsfiddle.net/extempl/93ymy3oL/. Or just run the snippet with fixed max-width below.

    Gif under the spoiler:

    body {
      max-width: 400px;
    }
    
    span::before, span::after {
      display: inline-block;
      max-width: 50%;
      overflow: hidden;
      white-space: pre;
    }
    
    span::before {
      content: attr(data-content-start);
      text-overflow: ellipsis;
    }
    
    span::after {
      content: attr(data-content-end);
      text-overflow: '';
      direction: rtl;
    }
    
    
    

提交回复
热议问题