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

后端 未结 16 2374
情话喂你
情话喂你 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: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:

    /* Add line-clamp/fade class here*/

    Text here

    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

    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.

    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;
        }
    }
    

提交回复
热议问题