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 ...
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.
No real easy way to do this. Use the Clamp.js library.
$clamp(myHeader, {clamp: 3});
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;
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;
}
}
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: this is still webkit only as far as I know.
.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! */
}
_x000D_
<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>
_x000D_
http://codepen.io/AlgeoMA/pen/JNvJdx (codepen version)
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/
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.
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 :)
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;
}
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.
here is good example in pure css.
.container{_x000D_
width: 200px;_x000D_
}_x000D_
.block-with-text {_x000D_
overflow: hidden;_x000D_
position: relative; _x000D_
line-height: 1.2em;_x000D_
max-height: 3.6em;_x000D_
text-align: justify; _x000D_
margin-right: -1em;_x000D_
padding-right: 1em;_x000D_
}_x000D_
.block-with-text+.block-with-text{_x000D_
margin-top:10px;_x000D_
}_x000D_
.block-with-text:before {_x000D_
content: '...';_x000D_
position: absolute;_x000D_
right: 0;_x000D_
bottom: 0;_x000D_
}_x000D_
.block-with-text:after {_x000D_
content: '';_x000D_
position: absolute;_x000D_
right: 0;_x000D_
width: 1em;_x000D_
height: 1em;_x000D_
margin-top: 0.2em;_x000D_
background: white;_x000D_
}
_x000D_
<div class="container">_x000D_
_x000D_
_x000D_
<div class="block-with-text">_x000D_
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_x000D_
</div>_x000D_
_x000D_
<div class="block-with-text">_x000D_
Lorem Ipsum is simply dummy text of the printing and typesetting industry_x000D_
</div>_x000D_
_x000D_
_x000D_
<div class="block-with-text">_x000D_
Lorem Ipsum is simply_x000D_
</div>_x000D_
_x000D_
</div>
_x000D_
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.
a pure css method base on -webkit-line-clamp, which works on webkit:
@-webkit-keyframes ellipsis {/*for test*/_x000D_
0% { width: 622px }_x000D_
50% { width: 311px }_x000D_
100% { width: 622px }_x000D_
}_x000D_
.ellipsis {_x000D_
max-height: 40px;/* h*n */_x000D_
overflow: hidden;_x000D_
background: #eee;_x000D_
_x000D_
-webkit-animation: ellipsis ease 5s infinite;/*for test*/_x000D_
/**_x000D_
overflow: visible;_x000D_
/**/_x000D_
}_x000D_
.ellipsis .content {_x000D_
position: relative;_x000D_
display: -webkit-box;_x000D_
-webkit-box-orient: vertical;_x000D_
-webkit-box-pack: center;_x000D_
font-size: 50px;/* w */_x000D_
line-height: 20px;/* line-height h */_x000D_
color: transparent;_x000D_
-webkit-line-clamp: 2;/* max row number n */_x000D_
vertical-align: top;_x000D_
}_x000D_
.ellipsis .text {_x000D_
display: inline;_x000D_
vertical-align: top;_x000D_
font-size: 14px;_x000D_
color: #000;_x000D_
}_x000D_
.ellipsis .overlay {_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 50%;_x000D_
width: 100%;_x000D_
height: 100%;_x000D_
overflow: hidden;_x000D_
_x000D_
/**_x000D_
overflow: visible;_x000D_
left: 0;_x000D_
background: rgba(0,0,0,.5);_x000D_
/**/_x000D_
}_x000D_
.ellipsis .overlay:before {_x000D_
content: "";_x000D_
display: block;_x000D_
float: left;_x000D_
width: 50%;_x000D_
height: 100%;_x000D_
_x000D_
/**_x000D_
background: lightgreen;_x000D_
/**/_x000D_
}_x000D_
.ellipsis .placeholder {_x000D_
float: left;_x000D_
width: 50%;_x000D_
height: 40px;/* h*n */_x000D_
_x000D_
/**_x000D_
background: lightblue;_x000D_
/**/_x000D_
}_x000D_
.ellipsis .more {_x000D_
position: relative;_x000D_
top: -20px;/* -h */_x000D_
left: -50px;/* -w */_x000D_
float: left;_x000D_
color: #000;_x000D_
width: 50px;/* width of the .more w */_x000D_
height: 20px;/* h */_x000D_
font-size: 14px;_x000D_
_x000D_
/**_x000D_
top: 0;_x000D_
left: 0;_x000D_
background: orange;_x000D_
/**/_x000D_
}
_x000D_
<div class='ellipsis'>_x000D_
<div class='content'>_x000D_
<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>_x000D_
<div class='overlay'>_x000D_
<div class='placeholder'></div>_x000D_
<div class='more'>...more</div>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
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.
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.
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>
This should work in webkit browsers:
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
div {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
* { font-family: verdana; }
_x000D_
<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>
_x000D_
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:
code :
truncate = function(description){
return description.split('').slice(0, description.lastIndexOf(" ",200)).join('') + "...";
}
here is a fiddle - http://jsfiddle.net/GYsW6/1/
Source: Stackoverflow.com