css ellipsis on second line
Asked Answered
M

21

402

CSS text-overflow: ellipsis on second line, is this possible? I can't find it on the net.

example:

what I want is like this:

I hope someone could help me. I need 
an ellipsis on the second line of...

but what's happening is this:

I hope someone could help me. I ... 
Muster answered 11/3, 2011 at 6:30 Comment(4)
AFAIK the ellipsis will appear and cut off the text at the end of the element's width. It won't flow over to the next line. The best solution here would be to implement some server- or client-side script which automatically trims the text to a certain amount of characters and then appends the ellipsis. My guess is a client-side script would be better, which would enable you to still have all the original text available if you need it.Papyrology
here is a similar question: #3923239Planchet
tl;dr: it is possible only in webkitPlanchet
The closest I came to achieving this was add an 'after' pseudo-element for the ellipsis, and position it inline, directly after the element containing the text. But the ellipsis vanishes if the element text is too long, so you'd have to trim the text somehow, in order to make this reliable.Screech
N
163

A requirement for text-overflow: ellipsis; to work is a one-line version of white-space (pre, nowrap etc). Which means the text will never reach the second line.

Ergo. Not possible in pure CSS.

My source when I was looking for the exact same thing just now: http://www.quirksmode.org/css/textoverflow.html (Quirksmode ftw!)

EDIT If the good CSS gods will implement http://www.w3.org/TR/css-overflow-3/#max-lines we can haz this in pure CSS using fragments (new) and max-lines (new). Also some more info on http://css-tricks.com/line-clampin/

EDIT 2 WebKit/Blink has line-clamp: -webkit-line-clamp: 2 will put ellipsis on 2nd line.

Noncombatant answered 6/10, 2011 at 21:32 Comment(3)
Keeping a watch, but so far is seems the gods are not with us yet: caniuse.com/#search=max-linesReannareap
How to achieve above feature that with saas? @NoncombatantRainbow
-webkit-line-clamp: 3Autohypnosis
B
388

This should work in webkit browsers:

overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;

browser support

div {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

* { font-family: verdana; }
<div>
   The <b><a target='_blank' href='https://developer.mozilla.org/docs/Web/CSS/-webkit-line-clamp'>-webkit-line-clamp</a></b> CSS property allows limiting of the contents of a block container to the specified number of lines. It only works in combination with the display  property set to <b>-webkit-box</b> or <b>-webkit-inline-box</b> and the <b>-webkit-box-orient</b> property set to vertical. In most cases you will also want to set overflow to hidden, otherwise the contents won't be clipped but an ellipsis will still be shown after the specified number of lines. When applied to anchor elements, the truncating can happen in the middle of the text, not necessarily at the end.
</div>
Bibb answered 27/9, 2013 at 11:20 Comment(16)
Note that, as of this comment, -webkit-line-clamp does not respect visibility: hidden. bugs.webkit.org/show_bug.cgi?id=45399Canoewood
Hey - seems nice but doesn't work for me. It simply puts '...' at given line-clamp but doesnt cut rest of text (even with white-space attr)Berkelium
You may need to add overflow: hidden for this to work.Vastitude
This will not work in Firefox, webkit not supported in gecko engineStricklin
This should be the accepted answer. You don't want any padding at the bottom of the element you apply this to either, as you can get the remainder of the text peeping through.Recognizor
This does in fact work in Firefox now. It's not even necessary to specify text-overflow: ellipsis; when using this.Albany
Works good and it has pretty good support nowadays. For those who wants it to show normally in older browsers you can just add max-height: XXXpx; - it will look the same just without ellipsis.Owades
mine was still showing the next lines partially and turned out it was because of padding. dont use padding with this.Ruffin
What is the workaround to get it working in firefox?Shamblin
@AmirZiarati You're right, padding does interfere with this, but margin doesn't so that may work in lieu.Metaplasia
box-orient is deprecated and should not be used see MDN. Does anyone know if there is an alternative?Woodie
The question asked for 2 lines of text, not 3.Rodroda
This does not work properly if a specific line-height is set.Aphorize
Working version with comments: JsFiddlePolyhedron
might want to add ... max-height: 2lh; ... to provide extra safetyNicolette
Don't forget to add a display to the parent component. it was not working for me until I added the display: block to the parentKos
N
163

A requirement for text-overflow: ellipsis; to work is a one-line version of white-space (pre, nowrap etc). Which means the text will never reach the second line.

Ergo. Not possible in pure CSS.

My source when I was looking for the exact same thing just now: http://www.quirksmode.org/css/textoverflow.html (Quirksmode ftw!)

EDIT If the good CSS gods will implement http://www.w3.org/TR/css-overflow-3/#max-lines we can haz this in pure CSS using fragments (new) and max-lines (new). Also some more info on http://css-tricks.com/line-clampin/

EDIT 2 WebKit/Blink has line-clamp: -webkit-line-clamp: 2 will put ellipsis on 2nd line.

Noncombatant answered 6/10, 2011 at 21:32 Comment(3)
Keeping a watch, but so far is seems the gods are not with us yet: caniuse.com/#search=max-linesReannareap
How to achieve above feature that with saas? @NoncombatantRainbow
-webkit-line-clamp: 3Autohypnosis
Z
116

Just use line-clamp for the browsers that support it(most modern browsers) and fall back to 1 line for older.

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


    @supports (-webkit-line-clamp: 2) {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: initial;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }

browser support

Zimmer answered 28/5, 2018 at 12:7 Comment(4)
white-space: initial; saved meSucking
best quality of those answers. Thank youObregon
From my point of view, I see this solution as optimal. Nowadays the websites are using a lot of JS, compared with 10 years ago, and we have to cater for performance. I prefer to use CSS to style, even if there is a small difference between browsers.Lubricant
Addition: if your content is not text, make sure it is set to display: inline-blockAllogamy
C
50

I found that Skeep's answer was not enough and needed an overflow style too:

overflow: hidden;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
display: -webkit-box;
-webkit-box-orient: vertical;
Coloquintida answered 29/10, 2015 at 20:15 Comment(1)
Just to mention that other solutions included -webkit-line-clamp: 3; while the question was for 2 lines, so this one is more correct.Rodroda
K
36

As others have already answered, a pure CSS solution does not exists. There is a jQuery plugin that is very easy to use, it is called dotdotdot. It uses the container's width and height to calculate if it needs to truncate and add ellipsis.

$("#multilinedElement").dotdotdot();

Here is a jsFiddle.

Kithara answered 15/1, 2013 at 13:39 Comment(5)
This plugin also provides lots of other utilities like adding "read more" links, it can also parse URIs and HTML markup. Good find!Blastomere
I tried out jQuery dotdotdot 1.7.3 for an ecommerce store, to limit the product names on a grid category page to two lines. After thorough testing, we decided not to go with this plugin because sometimes, after a shift-refresh, the category page layout would be broken. YMMV. In the end we went with a very simple vanilla JS solution: if the product name element's clientHeight is less than its scrollHeight, truncate the text at a preset boundary and append "...". Sometimes a few product names can be a bit too short because of the preset boundary, but it's super stable.Nodical
Easty to implement and works beautifully. Thank you!Mert
Although it works good, unfortunately it suffers from performance issues. If your project requires multiple ellipsis usages, consider another option. In addition here's a quote from the repository: "Because its performance can not be improved, this plugin is no longer actively maintained.".Outing
also, it wouldn't work if the text changes dynamically after loading :(Beverleebeverley
P
25

I used these css properties for line ellipsis-

overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3; 
-webkit-box-orient: vertical;

here -webkit-line-clamp: 3; is the number of line(3 lines) after which ... shows.

Platus answered 14/1, 2021 at 16:49 Comment(0)
G
23

If anyone is trying to get line-clamp to work in flexbox, it's slightly trickier - especially once you are torture testing with really long words. The only real differences in this code snippet are min-width: 0; in the flex item containing truncated text, and word-wrap: break-word;. A hat-tip to https://css-tricks.com/flexbox-truncated-text/ for the insight. Disclaimer: at the time this answer was written, browser support was poor outside of Chrome. However, things may have improved since then.

.flex-parent {
  display: flex;
}

.short-and-fixed {
  width: 30px;
  height: 30px;
  background: lightgreen;
}

.long-and-truncated {
  margin: 0 20px;
  flex: 1;
  min-width: 0;/* Important for long words! */
}

.long-and-truncated span {
  display: inline;
  -webkit-line-clamp: 3;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  word-wrap: break-word;/* Important for long words! */
}
<div class="flex-parent">
  <div class="flex-child short-and-fixed">
  </div>
  <div class="flex-child long-and-truncated">
    <span>asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd</span>
  </div>
  <div class="flex-child short-and-fixed">
  </div>
</div>

http://codepen.io/AlgeoMA/pen/JNvJdx (codepen version)

Gadid answered 12/5, 2017 at 21:21 Comment(1)
thanks ! word-wrap: break-word is key to meFlagstone
A
21

if someone is using SASS/SCSS and stumbles upon this question - maybe this mixin could be of help:

@mixin line-clamp($numLines : 1, $lineHeight: 1.412) {
  overflow: hidden;
  text-overflow: -o-ellipsis-lastline;
  text-overflow: ellipsis;
  display: block;
  /* autoprefixer: off */
  display: -webkit-box;
  -webkit-line-clamp: $numLines;
  -webkit-box-orient: vertical;
  max-height: $numLines * $lineHeight + unquote('em');
}

this only adds the ellipsis in webkit browsers. rest just cuts it off.

Autoplasty answered 29/10, 2014 at 15:45 Comment(2)
Using LESS, I had to change /* autoprefixer: off */ to /*! autoprefixer: off */ otherwise -webkit-box-orient: vertical; was getting removed after compilation which means the ellipsis never showedMiguelmiguela
Using less and autoprefixer as a gulp task I had to set remove:false in the options list.. thank you @nathanPalecek
O
11

What a shame that you can't get it to work over two lines! Would be awesome if the element was display block and had a height set to 2em or something, and when the text overflowed it would show an ellipsis!

For a single liner you can use:

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

For multiple lines maybe you could use a Polyfill such as jQuery autoellipsis which is on github http://pvdspek.github.com/jquery.autoellipsis/

Oblast answered 13/2, 2012 at 12:46 Comment(1)
That plugin is way too heavy for what it does. It's 5 years old. Rather use dotdotdot mentioned in another post.Furnish
G
11

I'm not a JS pro, but I figured out a couple ways you could do this.

The HTML:

<p id="truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi elementum consequat tortor et euismod. Nam commodo consequat libero vel lobortis. Morbi ac nisi at leo vehicula consectetur.</p>

Then with jQuery you truncate it down to a specific character count but leave the last word like this:

// Truncate but leave last word
var myTag = $('#truncate').text();
if (myTag.length > 100) {
  var truncated = myTag.trim().substring(0, 100).split(" ").slice(0, -1).join(" ") + "…";
  $('#truncate').text(truncated);
}

The result looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi
elementum consequat tortor et…

Or, you can simply truncate it down to a specific character count like this:

// Truncate to specific character
var myTag = $('#truncate').text();
if (myTag.length > 15) {
  var truncated = myTag.trim().substring(0, 100) + "…";
  $('#truncate').text(truncated);
}

The result looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi
elementum consequat tortor et euismod…

Hope that helps a bit.

Here is the jsFiddle.

Guide answered 9/5, 2012 at 19:44 Comment(0)
S
9

here is good example in pure css.

.container{
  width: 200px;
}
.block-with-text {
  overflow: hidden;
  position: relative; 
  line-height: 1.2em;
  max-height: 3.6em;
  text-align: justify;  
  margin-right: -1em;
  padding-right: 1em;
}
.block-with-text+.block-with-text{
  margin-top:10px;
}
.block-with-text:before {
  content: '...';
  position: absolute;
  right: 0;
  bottom: 0;
}
.block-with-text:after {
  content: '';
  position: absolute;
  right: 0;
  width: 1em;
  height: 1em;
  margin-top: 0.2em;
  background: white;
}
<div class="container">


<div class="block-with-text">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a
</div>

<div class="block-with-text">
Lorem Ipsum is simply dummy text of the printing and typesetting industry
</div>


<div class="block-with-text">
Lorem Ipsum is simply
</div>

</div>
Sonorant answered 5/5, 2017 at 15:0 Comment(1)
Pretty good solution , just it truncates texts even if the second line is too short to be truncated, but still , thumbs upFinitude
J
9

Sass helper mixin:

For those who are using a preprocessor like Sass, you could have a mixin with an optional parameter called lines which defaults to 1, this makes it very convenient to apply text truncation on an element, and change the number of lines when you need:

@mixin text-ellipsis($lines: 1) {
    text-overflow: ellipsis;
    overflow: hidden;
    @if ($lines > 1) {
        display: -webkit-box;
        -webkit-line-clamp: $lines;
        -webkit-box-orient: vertical;
    } @else {
        white-space: nowrap;
    }
}

Usage:

.some-title {
    @include text-ellipsis($lines: 2);
}
Job answered 14/11, 2021 at 13:58 Comment(0)
R
6

I have used the jQuery-condense-plugin before, which looks like it can do what you want. If not, there are different plugins that might suit your needs.

Edit: Made you a demo - note that I linked the jquery.condense.js on the demo which does the magic, so full credit to the author of that plugin :)

Represent answered 11/3, 2011 at 8:43 Comment(0)
E
6

Pure css way of trim multiline text with ellipsis

Adjust text container's hight, control line to break by -webkit-line-clamp: 2;

.block-ellipsis {
  display: block;
  display: -webkit-box;
  max-width: 100%;
  height: 30px;
  margin: 0 auto;
  font-size: 14px;
  line-height: 1;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
Endosmosis answered 2/8, 2019 at 14:0 Comment(0)
S
5

All the answers here are wrong, they missing important piece, the height

.container{
    width:200px;
    height:600px;
    background:red
}
.title {
        overflow: hidden;
        line-height: 20px;
        height: 40px;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }
<div class="container">
    <div class="title">this is a long text you cant cut it in half</div>
</div>
Sanfo answered 9/8, 2019 at 12:48 Comment(0)
A
4

It is a non-standard CSS, which is not covered in current version of CSS (Firefox does not support it). Try to use JavaScript instead.

Acolyte answered 11/3, 2011 at 6:36 Comment(0)
M
2

I've met this issue before, and there is no pure css solution

That's why i have developped a small library to deal with this issue (among others). The library provides objects to modelize and perform letter-level text rendering. You can for example emulate a text-overflow: ellipsis with an arbitrary limit (not necessary one line)

Read more at http://www.samuelrossille.com/home/jstext.html for screenshot, tutorial, and dowload link.

Manganin answered 16/5, 2012 at 20:30 Comment(0)
C
0

a pure css method base on -webkit-line-clamp, which works on webkit:

@-webkit-keyframes ellipsis {/*for test*/
    0% { width: 622px }
    50% { width: 311px }
    100% { width: 622px }
}
.ellipsis {
    max-height: 40px;/* h*n */
    overflow: hidden;
    background: #eee;

    -webkit-animation: ellipsis ease 5s infinite;/*for test*/
    /**
    overflow: visible;
    /**/
}
.ellipsis .content {
    position: relative;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-box-pack: center;
    font-size: 50px;/* w */
    line-height: 20px;/* line-height h */
    color: transparent;
    -webkit-line-clamp: 2;/* max row number n */
    vertical-align: top;
}
.ellipsis .text {
    display: inline;
    vertical-align: top;
    font-size: 14px;
    color: #000;
}
.ellipsis .overlay {
    position: absolute;
    top: 0;
    left: 50%;
    width: 100%;
    height: 100%;
    overflow: hidden;

    /**
    overflow: visible;
    left: 0;
    background: rgba(0,0,0,.5);
    /**/
}
.ellipsis .overlay:before {
    content: "";
    display: block;
    float: left;
    width: 50%;
    height: 100%;

    /**
    background: lightgreen;
    /**/
}
.ellipsis .placeholder {
    float: left;
    width: 50%;
    height: 40px;/* h*n */

    /**
    background: lightblue;
    /**/
}
.ellipsis .more {
    position: relative;
    top: -20px;/* -h */
    left: -50px;/* -w */
    float: left;
    color: #000;
    width: 50px;/* width of the .more w */
    height: 20px;/* h */
    font-size: 14px;

    /**
    top: 0;
    left: 0;
    background: orange;
    /**/
}
<div class='ellipsis'>
    <div class='content'>
        <div class='text'>text text text text text text text text text text text text text text text text text text text text text </div>
        <div class='overlay'>
            <div class='placeholder'></div>
            <div class='more'>...more</div>
        </div>
    </div>
</div>
Colloquial answered 12/2, 2017 at 4:15 Comment(0)
O
0

No real easy way to do this. Use the Clamp.js library.

$clamp(myHeader, {clamp: 3});
Orcutt answered 19/7, 2018 at 15:4 Comment(0)
J
-2

Late Reply but a html line-break works as well, so you can do a html + css only solution. So it's bad practice to usually use the br element, but if you break your comment with br the text-overflow ellipsis will start on the next line of the html text.

Joshua answered 10/11, 2020 at 9:21 Comment(1)
Please try including an example code with your answerRondo
A
-7

I found a solution however you need to know a rough character length that will fit in to your text area, then join a ... to the preceeding space.

The way I did this is to:

  1. assume a rough character length (in this case 200) and pass to a function along with your text
  2. split the spaces so you have one long string
  3. use jQuery to get slice the first " " space after your character length
  4. join the remaining ...

code :

truncate = function(description){
        return description.split('').slice(0, description.lastIndexOf(" ",200)).join('') + "...";           
}

here is a fiddle - http://jsfiddle.net/GYsW6/1/

Afton answered 3/4, 2014 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.