138

I want to wrap a text within only two lines inside div of specific width. If text goes beyond the length of two lines then I want to show ellipses. Is there a way to do that using CSS?

e.g.

Sample text showing wrapping
of text in only two line...
Braiam
  • 1
  • 11
  • 47
  • 78
Mady
  • 5,016
  • 7
  • 35
  • 46

14 Answers14

194

Limiting output to two lines of text is possible with CSS, if you set the line-height and height of the element, and set overflow:hidden;:

#someDiv {
    line-height: 1.5em;
    height: 3em;       /* height is 2x line-height, so two lines will display */
    overflow: hidden;  /* prevents extra lines from being visible */
}

--- jsFiddle DEMO ---

Alternatively, you can use the CSS text-overflow and white-space properties to add ellipses, but this only appears to work for a single line.

#someDiv {
    line-height: 1.5em;
    height: 3em;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 100%;
}

And a demo:

--- jsFiddle DEMO ---

Achieving both multiple lines of text and ellipses appears to be the realm of javascript.

jackwanders
  • 15,612
  • 3
  • 40
  • 40
  • 21
    I only see one line going across for some reason :/ – SearchForKnowledge Oct 13 '14 at 16:04
  • 9
    The second example has only one line, when the requested solution requires two. – goyote Feb 11 '16 at 20:29
  • The third example does not work for me, tested in Chrome and Firefox. – Oliver Lorton Mar 23 '20 at 10:04
  • 3
    In that broken example `white-space: nowrap;` breaks it, if you comment it out it works. – Wilt Apr 02 '20 at 10:14
  • 1
    Did someone change something in 2022? I used to do this and worked like a charm, but it seems it doesn't work anymore in my browser. I tested it in Chrome and Firefox, but ellipses are not shown. Even in the jsFiddle. See other answer by @vinesh https://stackoverflow.com/a/32585024/5830500 this works. – Wael Alshabani Dec 23 '21 at 18:13
103

Another simple and quick solution

.giveMeEllipsis {
   overflow: hidden;
   text-overflow: ellipsis;
   display: -webkit-box;
   -webkit-box-orient: vertical;
   -webkit-line-clamp: N; /* number of lines to show */
   line-height: X;        /* fallback */
   max-height: X*N;       /* fallback */
}

The reference to the original question and answer is here

Community
  • 1
  • 1
vinesh
  • 4,745
  • 6
  • 41
  • 45
28

CSS only

    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  • 2
    This, requires box-orient property set to vertical but `box-orient` is non-standart. https://developer.mozilla.org/en-US/docs/Web/CSS/box-orient – Wax Aug 06 '21 at 12:59
  • Please explain your code as it will make it more helpful for others. – ethry Nov 02 '22 at 08:38
22

The best one I've seen which is CSS only and responsive comes from Mobify Developer Blog - CSS Ellipsis: How to Manage Multi-Line Ellipsis in Pure CSS:

JS Fiddle Example

CSS:

html, body, p { margin: 0; padding: 0; font-family: sans-serif;}

.ellipsis {
    overflow: hidden;
    height: 200px;
    line-height: 25px;
    margin: 20px;
    border: 5px solid #AAA; }

.ellipsis:before {
    content:"";
    float: left;
    width: 5px; height: 200px; }

.ellipsis > *:first-child {
    float: right;
    width: 100%;
    margin-left: -5px; }        

.ellipsis:after {
    content: "\02026";  

    box-sizing: content-box;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    float: right; position: relative;
    top: -25px; left: 100%; 
    width: 3em; margin-left: -3em;
    padding-right: 5px;

    text-align: right;

    background: -webkit-gradient(linear, left top, right top,
        from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
    background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);           
    background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
    background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
    background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); }

Html:

<div class="ellipsis">
    <div class="blah">
        <p>Call me Ishmael. Some years ago &ndash; never mind how long precisely &ndash; having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off &ndash; then, I account it high time to get to sea as soon as I can.</p>
    </div>
</div>
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • The best solution I've seen so far. You might want to reduce the height (200px) variable in the fiddle, for my screen size the text did not overflow initially. – Mike Fuchs Feb 24 '15 at 13:01
16

I believe the CSS-only solution text-overflow: ellipsis applies to one line only, so you won't be able to go this route:

.yourdiv {

    line-height: 1.5em; /* Sets line height to 1.5 times text size */
    height: 3em; /* Sets the div height to 2x line-height (3 times text size) */
    width: 100%; /* Use whatever width you want */
    white-space: normal; /* Wrap lines of text */
    overflow: hidden; /* Hide text that goes beyond the boundaries of the div */
    text-overflow: ellipsis; /* Ellipses (cross-browser) */
    -o-text-overflow: ellipsis; /* Ellipses (cross-browser) */
}

Have you tried http://tpgblog.com/threedots/ for jQuery?

Eric Tjossem
  • 2,536
  • 1
  • 15
  • 18
  • as I mentioned, combining ellipses with multiple lines of text doesn't appear to work, at least not for me in Chrome. – jackwanders Aug 16 '12 at 14:53
  • This worked in my current problem after I added: `display: block` and `min-height: 13px` and `max-height: 26px` for setting height for a `` – Underverse Oct 30 '15 at 06:43
16

CSS only solution for Webkit

// Only for DEMO
$(function() {

  $('#toggleWidth').on('click', function(e) {

    $('.multiLineLabel').toggleClass('maxWidth');

  });

})
.multiLineLabel {
  display: inline-block;
  box-sizing: border-box;
  white-space: pre-line;
  word-wrap: break-word;
}

.multiLineLabel .textMaxLine {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}


/* Only for DEMO */

.multiLineLabel.maxWidth {
  width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="multiLineLabel">
  <span class="textMaxLine">This text is going to wrap automatically in 2 lines in case the width of the element is not sufficiently wide.</span>
</div>
<br/>
<button id="toggleWidth">Toggle Width</button>
Ashish Singh
  • 1,004
  • 11
  • 23
6

Try something like this: http://jsfiddle.net/6jdj3pcL/1/

<p>Here is a paragraph with a lot of text ...</p>

p {
    line-height: 1.5em;
    height: 3em;
    overflow: hidden;
    width: 300px;
}

p::before {
   content: '...';
   float: right;
   margin-top: 1.5em;
}
  • I don't find this solution ideal because it adds ellipsis even if the text doesn't overflow or even if the text fits in one line. – Jerry Jul 21 '21 at 08:12
5

Typically a one-line truncate is quite simple

.truncate-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

Two line truncate is a little bit more tricky, but it can be done with css this example is in sass.

@mixin multiLineEllipsis($lineHeight: 1.2rem, $lineCount: 2, $bgColor: white, $padding-right: 0.3125rem, $width: 1rem, $ellipsis-right: 0) {
  overflow: hidden; /* hide text if it is more than $lineCount lines  */
  position: relative; /* for set '...' in absolute position */
  line-height: $lineHeight; /* use this value to count block height */
  max-height: $lineHeight * $lineCount; /* max-height = line-height * lines max number */
  padding-right: $padding-right; /* place for '...' */
  white-space: normal; /* overwrite any white-space styles */
  word-break: break-all; /* will break each letter in word */
  text-overflow: ellipsis; /* show ellipsis if text is broken */

  &::before {
    content: '...'; /* create the '...'' points in the end */
    position: absolute;
    right: $ellipsis-right;
    bottom: 0;
  }

  &::after {
    content: ''; /* hide '...'' if we have text, which is less than or equal to max lines and add $bgColor */
    position: absolute;
    right: 0;
    width: $width;
    height: 1rem * $lineCount;
    margin-top: 0.2rem;
    background: $bgColor; /* because we are cutting off the diff we need to add the color back. */
  }
}
ArturoRomero
  • 320
  • 1
  • 3
  • 10
3

See http://jsfiddle.net/SWcCt/.

Just set a line-height the half of height:

line-height:20px;
height:40px;

Of course, in order to make text-overflow: ellipsis work you also need:

overflow:hidden;
white-space: pre;
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • This solution isnt flexible and requires a manual line break in the source text. Note that http://jsfiddle.net/SWcCt/282/ each box only contains one line of text. The desired solution would look like the 2nd box of http://jsfiddle.net/SWcCt/283/ except with an ellipsis at the end of the 2nd line. – Josh Coady Apr 01 '16 at 01:47
  • @JoshuaCoady Good point, but `text-overflow: ellipsis` only works for inline boxes which overflow the line box. Without `white-space: pre` they would just go to the next line box. And then a manual line break is needed. I don't think there is a perfect solution. – Oriol Apr 01 '16 at 02:05
1

@Asiddeen bn Muhammad's solution worked for me with a little modification to the css

    .text {
 line-height: 1.5;
  height: 6em; 
white-space: normal;
overflow: hidden;
text-overflow: ellipsis;
display: block;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
 }
Obyno Pac
  • 103
  • 2
  • 13
1
<tag> {
  max-height: calc(2 * 1.5em); /* 2 lines of 1.5em each */
  line-height: 1.5em;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* number of lines to show */
  -webkit-box-orient: vertical;
}

This code uses the -webkit-box and -webkit-line-clamp properties to achieve the desired effect of showing only 2 lines of text and then adding ellipses for any remaining text. The max-height property sets the maximum height of the p element to 3em (2 lines of 1.5em each), and line-height sets the height of each line to 1.5em. The overflow property is set to hidden to hide any overflow beyond the maximum height, and text-overflow is set to ellipsis to add ellipses for any hidden text.

Subham Raj
  • 103
  • 8
0

Below worked for me:

CSS:

.longWordBreakParent {
  display: flex;
}
.longWordBreak {
  word-break: break-all;
}

HTML:

<div className="longWordBreakParent">
    <span className='longWordBreak'>someverylongtextthatcannotbefitinoneline</span>
</div>

For more details, see:
. https://developer.mozilla.org/en-US/docs/Web/CSS/display
. https://developer.mozilla.org/en-US/docs/Web/CSS/word-break

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
0

I have the answer for this "Achieving both multiple lines of text and ellipses appears to be the realm of Javascript".

WebkitLineClamp: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp WebKItBoxOrient:https://developer.mozilla.org/en-US/docs/Web/CSS/box-orient

`.textWithTwoLine {
 overflow: hidden;
 text-overflow: ellipsis;
 display: -webkit-box;
 -webkit-line-clamp: 2;
 -webkit-box-orient: vertical;
  }`
-3

In all these examples, let’s assume we have:

#someDiv {
  width: 250px;
  overflow: hidden;
}
Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
  • How this make sure that only 2 lines are shown? It only deals with the width of the div. – Braiam Oct 28 '21 at 20:17
  • Please explain your code as it will make it more helpful for others. Please also use a comment for this type of message. – ethry Nov 02 '22 at 08:39