Cross-browser multi-line text overflow with ellipsis appended within a fixed width and height

前端 未结 25 2759
栀梦
栀梦 2020-11-22 16:19

I made an image for this question to make it easier to understand.

Is it possible to create an ellipsis on a

with a fixed width and multiple
25条回答
  •  温柔的废话
    2020-11-22 16:38

    Maybe quite late but using SCSS you can declare a function like:

    @mixin clamp-text($lines, $line-height) {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: $lines;
      line-height: $line-height;
      max-height: unquote('#{$line-height*$lines}em');
    
      @-moz-document url-prefix() {
        position: relative;
        height: unquote('#{$line-height*$lines}em');
    
        &::after {
          content: '';
          text-align: right;
          position: absolute;
          bottom: 0;
          right: 0;
          width: 30%;
          height: unquote('#{$line-height}em');
          background: linear-gradient(
            to right,
            rgba(255, 255, 255, 0),
            rgba(255, 255, 255, 1) 50%
          );
        }
      }
    }
    

    And use it like:

    .foo {
        @include clamp-text(1, 1.4);
    }
    

    Which will truncate the text to one line and knowing that it is 1.4 its line-height. The output expected is chrome to render with ... at the end and FF with some cool fade at the end

    Firefox

    Chrome

提交回复
热议问题