With CSS, use “…” for overflowed block of multi-lines

后端 未结 16 2367
情话喂你
情话喂你 2020-11-22 07:15

with

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

\"...\" will be shown in the end of the line if overflowed. However, t

相关标签:
16条回答
  • 2020-11-22 07:41

    After looking over the W3 spec for text-overflow, I don't think this is possible using only CSS. Ellipsis is a new-ish property, so it probably hasn't received much usage or feedback as of yet.

    However, this guy appears to have asked a similar (or identical) question, and someone was able to come up with a nice jQuery solution. You can demo the solution here: http://jsfiddle.net/MPkSF/

    If javascript is not an option, I think you may be out of luck...

    0 讨论(0)
  • 2020-11-22 07:44

    Here's a recent css-tricks article which discusses this.

    Some of the solutions in the above article (which are not mentioned here) are

    1) -webkit-line-clamp and 2) Place an absolutely positioned element to the bottom right with fade out

    Both methods assume the following markup:

    <div class="module"> /* Add line-clamp/fade class here*/
      <p>Text here</p>
    </div>
    

    with css

    .module {
      width: 250px;
      overflow: hidden;
    }
    

    1) -webkit-line-clamp

    line-clamp FIDDLE (..for a maximum of 3 lines)

    .line-clamp {
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;  
      max-height: 3.6em; /* I needed this to get it to work */
    }
    

    2) fade out

    Let's say you set the line-height to 1.2em. If we want to expose three lines of text, we can just make the height of the container 3.6em (1.2em × 3). The hidden overflow will hide the rest.

    Fade out FIDDLE

    p
    {
        margin:0;padding:0;
    }
    .module {
      width: 250px;
      overflow: hidden;
      border: 1px solid green;
      margin: 10px;
    }
    
    .fade {
      position: relative;
      height: 3.6em; /* exactly three lines */
    }
    .fade:after {
      content: "";
      text-align: right;
      position: absolute;
      bottom: 0;
      right: 0;
      width: 70%;
      height: 1.2em;
      background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
    }
    

    Solution #3 - A combination using @supports

    We can use @supports to apply webkit's line-clamp on webkit browsers and apply fade out in other browsers.

    @supports line-clamp with fade fallback fiddle

    <div class="module line-clamp">
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
    </div>
    

    CSS

    .module {
      width: 250px;
      overflow: hidden;
      border: 1px solid green;
      margin: 10px;
    }
    
    .line-clamp {
          position: relative;
          height: 3.6em; /* exactly three lines */
        }
    .line-clamp:after {
          content: "";
          text-align: right;
          position: absolute;
          bottom: 0;
          right: 0;
          width: 70%;
          height: 1.2em;
          background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
     }
    
    @supports (-webkit-line-clamp: 3) {
        .line-clamp {
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;  
            max-height:3.6em; /* I needed this to get it to work */
            height: auto;
        }
        .line-clamp:after {
            display: none;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:47

    Bit late to this party but I came up with, what I think, is a unique solution. Rather than trying to insert your own ellipsis through css trickery or js I thought i'd try and roll with the single line only restriction. So I duplicate the text for every "line" and just use a negative text-indent to make sure one line starts where the last one stops. FIDDLE

    CSS:

    #wrapper{
        font-size: 20pt;
        line-height: 22pt;
        width: 100%;
        overflow: hidden;
        padding: 0;
        margin: 0;
    }
    
    .text-block-line{
        height: 22pt;
        display: inline-block;
        max-width: 100%;
        overflow: hidden;
        white-space: nowrap;
        width: auto;
    }
    .text-block-line:last-child{
        text-overflow: ellipsis;
    }
    
    /*the follwing is suboptimal but neccesary I think. I'd probably just make a sass mixin that I can feed a max number of lines to and have them avialable. Number of lines will need to be controlled by server or client template which is no worse than doing a character count clip server side now. */
    .line2{
        text-indent: -100%;
    }
    .line3{
        text-indent: -200%;
    }
    .line4{
        text-indent: -300%;
    }
    

    HTML:

    <p id="wrapper" class="redraw">
        <span class="text-block-line line1">This text is repeated for every line that you want to be displayed in your element. This example has a max of 4 lines before the ellipsis occurs. Try scaling the preview window width to see the effect.</span>
        <span class="text-block-line line2">This text is repeated for every line that you want to be displayed in your element. This example has a max of 4 lines before the ellipsis occurs. Try scaling the preview window width to see the effect.</span>
        <span class="text-block-line line3">This text is repeated for every line that you want to be displayed in your element. This example has a max of 4 lines before the ellipsis occurs. Try scaling the preview window width to see the effect.</span>
        <span class="text-block-line line4">This text is repeated for every line that you want to be displayed in your element. This example has a max of 4 lines before the ellipsis occurs. Try scaling the preview window width to see the effect.</span>
    </p>
    

    More details in the fiddle. There is an issue with the browser reflowing that I use a JS redraw for and such so do check it out but this is the basic concept. Any thoughts/suggestions are much appreciated.

    0 讨论(0)
  • 2020-11-22 07:50

    Just want to add to this question for completeness sake.

    • Opera has non-standard support for this called -o-ellipsis-lastline.
    • dotdotdot is a great jQuery plugin I can recommend.
    0 讨论(0)
提交回复
热议问题