Limit text length to n lines using CSS

后端 未结 13 1180
滥情空心
滥情空心 2020-11-22 02:53

Is it possible to limit a text length to \"n\" lines using CSS (or cut it when overflows vertically).

text-overflow: ellipsis; only works for 1 line tex

13条回答
  •  萌比男神i
    2020-11-22 03:15

    Working Cross-browser Solution

    This problem has been plaguing us all for years.

    To help in all cases, I have laid out the CSS only approach, and a jQuery approach in case the css caveats are a problem.

    Here's a CSS only solution I came up with that works in all circumstances, with a few minor caveats.

    The basics are simple, it hides the overflow of the span, and sets the max height based on the line height as suggested by Eugene Xa.

    Then there is a pseudo class after the containing div that places the ellipsis nicely.

    Caveats

    This solution will always place the ellipsis, regardless if there is need for it.

    If the last line ends with an ending sentence, you will end up with four dots....

    You will need to be happy with justified text alignment.

    The ellipsis will be to the right of the text, which can look sloppy.

    Code + Snippet

    jsfiddle

    .text {
      position: relative;
      font-size: 14px;
      color: black;
      width: 250px; /* Could be anything you like. */
    }
    
    .text-concat {
      position: relative;
      display: inline-block;
      word-wrap: break-word;
      overflow: hidden;
      max-height: 3.6em; /* (Number of lines you want visible) * (line-height) */
      line-height: 1.2em;
      text-align:justify;
    }
    
    .text.ellipsis::after {
      content: "...";
      position: absolute;
      right: -12px; 
      bottom: 4px;
    }
    
    /* Right and bottom for the psudo class are px based on various factors, font-size etc... Tweak for your own needs. */
    Lorem ipsum dolor sit amet, nibh eleifend cu his, porro fugit mandamus no mea. Sit tale facete voluptatum ea, ad sumo altera scripta per, eius ullum feugait id duo. At nominavi pericula persecuti ius, sea at sonet tincidunt, cu posse facilisis eos. Aliquid philosophia contentiones id eos, per cu atqui option disputationi, no vis nobis vidisse. Eu has mentitum conclusionemque, primis deterruisset est in. Virtute feugait ei vim. Commune honestatis accommodare pri ex. Ut est civibus accusam, pro principes conceptam ei, et duo case veniam. Partiendo concludaturque at duo. Ei eirmod verear consequuntur pri. Esse malis facilisis ex vix, cu hinc suavitate scriptorem pri.

    jQuery Approach

    In my opinion this is the best solution, but not everyone can use JS. Basically, the jQuery will check any .text element, and if there are more chars than the preset max var, it will cut the rest off and add an ellipsis.

    There are no caveats to this approach, however this code example is meant only to demonstrate the basic idea - I wouldn't use this in production without improving on it for a two reasons:

    1) It will rewrite the inner html of .text elems. whether needed or not. 2) It does no test to check that the inner html has no nested elems - so you are relying a lot on the author to use the .text correctly.

    Edited

    Thanks for the catch @markzzz

    Code & Snippet

    jsfiddle

    setTimeout(function()
    {
    	var max = 200;
      var tot, str;
      $('.text').each(function() {
      	str = String($(this).html());
      	tot = str.length;
        str = (tot <= max)
        	? str
          : str.substring(0,(max + 1))+"...";
        $(this).html(str);
      });
    },500); // Delayed for example only.
    .text {
      position: relative;
      font-size: 14px;
      color: black;
      font-family: sans-serif;
      width: 250px; /* Could be anything you like. */
    }
    
    

    Old men tend to forget what thought was like in their youth; they forget the quickness of the mental jump, the daring of the youthful intuition, the agility of the fresh insight. They become accustomed to the more plodding varieties of reason, and because this is more than made up by the accumulation of experience, old men think themselves wiser than the young.

    Old men tend to forget what thought was like in their youth;

提交回复
热议问题