Text-overflow Ellipsis Without "white-space: Nowrap"?
I was wondering if there is any clever way, to achieve the css ellipsis effect without the need to apply white-space: nowrap also. In other words, lets say we have a block element
Solution 1:
There is no way to do this using pure CSS. It's monstrous and sad. I've often desired this myself when you want the browser to "ellipsify" a multi-line text with possibly overflowing text whenever it spills out of the container.
Solution 2:
Update
nowdays CSS line-clamp
can be used with the latest browsers.
p:first-of-type {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
color:#777
}
<h2>Run that snippet to find out if your browser understand this</h2>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus
enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis
luctus, metus</p>
<h3>same text below, not clamped.</h3>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus
enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis
luctus, metus</p>
You can fake ellipsis with content:'...' and position:absolute;
set height within a numbers of line (add eventually vertical-padding)
http://jsfiddle.net/V3ZQH/
span {
background-color: red;
width: 100px;
/*height: 50px;*/
height:2.4em;
display: block;
overflow: hidden;
text-overflow: ellipsis;
/*white-space: nowrap; */
position:relative;
padding:0 0.5em;
}
span:after {
content:'...';
background:inherit;
position:absolute;
bottom:0;
right:0;
box-shadow:0 0 5px red
}
Solution 3:
I know only of ugly work arounds involving content
, absolute positionings, helper elements holding the dots and padding. I think it is probably best to have your height to be an exact multitude of your line height, for example by doing so:
height: 2em;
font-size: 1em;
line-height: 1em;
Post a Comment for "Text-overflow Ellipsis Without "white-space: Nowrap"?"