Add dots/ellipsis on div/span element overflow without using jquery

前端 未结 5 1028
萌比男神i
萌比男神i 2021-02-04 06:58

Need to implement functionality similar to what dotdotdot jQuery plugin does but cannot use javascript frameworks (like jquery or ext).

Is there an

5条回答
  •  渐次进展
    2021-02-04 07:28

    Works for any number of lines and any width without any javascript - and is responsive. Simply set your max-height to a multiple of your line height: i.e. (22px line height) * (max 3 lines of text) = (max height 66px).

    https://codepen.io/freer4/pen/prKLPy

    html, body, p { margin: 0; padding: 0; font-family: sans-serif;line-height:22px;}
    
    .ellipsis{
      overflow:hidden;
      margin-bottom:1em;
      position:relative;
    }
    
    .ellipsis:before {
      content: "\02026";  
      position: absolute;
      bottom: 0; 
      right:0;
      width: 1.8em; 
      height:22px;
      margin-left: -1.8em;
      padding-right: 5px;
      text-align: right;
      background-size: 100% 100%;
      background: linear-gradient(to right, rgba(255, 255, 255, 0), white 40%, white);
      z-index:2;
    }
    .ellipsis::after{
      content:"";
      position:relative;
      display:block;
      float:right;
      background:#FFF;
      width:3em;
      height:22px;
      margin-top:-22px;
      z-index:3;
    }
    
    /*For testing*/
    .ellipsis{
      max-width:500px;
      text-align:justify;
    }
    .ellipsis-3{
      max-height:66px;
    }
    
    .ellipsis-5{
      max-height:110px;
    }

    Here we can have a great many lines of text and it works as we expect it to. Here we can have a great many lines of text and it works as we expect it to. Here we can have a great many lines of text and it works as we expect it to. Here we can have a great many lines of text and it works as we expect it to.

    The number of lines shown is easily controlled by setting the max-height of the .ellipsis element. The downsides are the requirement of a wrapping element, and that if the text is precisely as long as your number of lines, you'll get a white area covering the very trailing end of your text. You've been warned. This is just some pushing text to make the element longer. See the ellipsis? Yay.

提交回复
热议问题