Insert Ellipsis (...) into HTML Tag If Content Too Wide

Insert ellipsis (...) into HTML tag if content too wide

I've got a solution working in FF3, Safari and IE6+ with single and multiline text

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

.ellipsis.multiline {
white-space: normal;
}

<div class="ellipsis" style="width: 100px; border: 1px solid black;">Lorem ipsum dolor sit amet, consectetur adipisicing elit</div>
<div class="ellipsis multiline" style="width: 100px; height: 40px; border: 1px solid black; margin-bottom: 100px">Lorem ipsum dolor sit amet, consectetur adipisicing elit</div>

<script type="text/javascript" src="/js/jquery.ellipsis.js"></script>
<script type="text/javascript">
$(".ellipsis").ellipsis();
</script>

jquery.ellipsis.js

(function($) {
$.fn.ellipsis = function()
{
return this.each(function()
{
var el = $(this);

if(el.css("overflow") == "hidden")
{
var text = el.html();
var multiline = el.hasClass('multiline');
var t = $(this.cloneNode(true))
.hide()
.css('position', 'absolute')
.css('overflow', 'visible')
.width(multiline ? el.width() : 'auto')
.height(multiline ? 'auto' : el.height())
;

el.after(t);

function height() { return t.height() > el.height(); };
function width() { return t.width() > el.width(); };

var func = multiline ? height : width;

while (text.length > 0 && func())
{
text = text.substr(0, text.length - 1);
t.html(text + "...");
}

el.html(t.html());
t.remove();
}
});
};
})(jQuery);

Add an ellipsis to HTML text if it is too large

Ideal way would be to use css text-overflow property:

p {
white-space: nowrap;
width: 100%;
overflow: hidden; /* "overflow" value must be different from "visible" */
text-overflow: ellipsis;
}

How to insert ellipsis into a large dataset in HTML with the least latency

Nobody will have found a robust & performant solution for this, that covers all browsers and works equally well in all of them since it doesn't exist. The only robust & performant solution would be pure css, which is only supported by a subset of the browsers most people wish to support.

Until all browsers you wish to support, support text-overflow: ellipsis, you can have either robust (slow js that looks at all parameters) or performant (substring whether server or clientside).

The only browser that doesn't support the css trick is firefox.
IE7+,and webkit browsers like chrome and safari supports it just fine.
Opera supports it by -o-text-overflow

Should you decide to not support firefox, you can support all the other browsers by doing this:

white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-o-text-overflow: ellipsis;

You need white-space: nowrap; and overflow: hidden; to make the text-overflow work well.

If you can live with text not being selectable then you can use the XUL hack for firefox, but i rarely find it usefull myself.

text-overflow ellipsis in fluid width table cells

Unfortunately, there is now way to have the CSS ellipsis on a dynamic width element.

Adding

display: block;
width: 300px;

for example, will create your ellipsis. But this will not do, in your case.

You will have to fallback on a JavaScript solution, like jquery.ellipsis plugin.

See this question for more information: Insert ellipsis (...) into HTML tag if content too wide


Edit:

Actually, if you just want the column to extend to all the width of the table, without crossing out of the screen (and keeping it only on one line), you should add

table { 
width: 100%;
table-layout: fixed;
}

See the updated fiddle: http://jsfiddle.net/7v72L/

(thanks to @CBroe for the suggestion)

Insert ellipsis to the partial text inside the anchor tag

It's ellipsis by the way, not ellipse.

http://jsfiddle.net/Vc4N6/12/

.e {
overflow: hidden;
max-width:70px;
text-overflow: ellipsis;
white-space:nowrap;
display: inline-block;
margin: 0;
vertical-align: top;
}

How can I do text-overflow: ellipsis on two lines?

Add a span to the container, which will hold the text:

<div class="container">
<span>text...</span>
</span>

Add this CSS:

.container {
overflow: hidden;
position: relative;
background: white; //or other color
}

.container:after {
content: '...'; //the ellipsis
position: absolute; //positioned in bottom-right
bottom: 0; //...
right: 0; //...
padding: 0 0.3em; //give some separation from text
background: inherit; //same color as container
}

.container span:after {
content: '\0000a0'; //a space
position: absolute; //to cover up ellipsis if needed
width: 1000px; //...
z-index: 1; //...
background: white; //must match container's color. can't use inherit here.
}

Fiddle

Resize the container, and you'll see that the ellipsis displays only as necessary.

Should work in all modern browsers.

Text overflow ellipsis on two lines

I'm not sure if you have seen this article, but Chris Coyier's excellent CSS-Tricks.com posted a link to this a while back and it's a pure CSS solution that accomplishes exactly what you seek.

(Click to View on CodePen)

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-size: 100% 100%;
/* 512x1 image,gradient for IE9. Transparent at 0% -> white at 50% -> white at 100%.*/
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAABCAMAAACfZeZEAAAABGdBTUEAALGPC/xhBQAAAwBQTFRF////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////AAAA////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////wDWRdwAAAP90Uk5TgsRjMZXhS30YrvDUP3Emow1YibnM9+ggOZxrBtpRRo94gxItwLOoX/vsHdA2yGgL8+TdKUK8VFufmHSGgAQWJNc9tk+rb5KMCA8aM0iwpWV6dwP9+fXuFerm3yMs0jDOysY8wr5FTldeoWKabgEJ8RATG+IeIdsn2NUqLjQ3OgBDumC3SbRMsVKsValZplydZpZpbJOQco2KdYeEe36BDAL8/vgHBfr2CvTyDu8R7esU6RcZ5ecc4+Af3iLcJSjZ1ivT0S/PMs3LNck4x8U7wz7Bv0G9RLtHuEq1TbJQr1OtVqqnWqRdoqBhnmSbZ5mXapRtcJGOc4t2eYiFfH9AS7qYlgAAARlJREFUKM9jqK9fEGS7VNrDI2+F/nyB1Z4Fa5UKN4TbbeLY7FW0Tatkp3jp7mj7vXzl+4yrDsYoVx+JYz7mXXNSp/a0RN25JMcLPP8umzRcTZW77tNyk63tdprzXdmO+2ZdD9MFe56Y9z3LUG96mcX02n/CW71JH6Qmf8px/cw77ZvVzB+BCj8D5vxhn/vXZh6D4uzf1rN+Cc347j79q/zUL25TPrJMfG/5LvuNZP8rixeZz/mf+vU+Vut+5NL5gPOeb/sd1dZbTs03hBuvmV5JuaRyMfk849nEM7qnEk6IHI8/qn049hB35QGHiv0yZXuMdkXtYC3ebrglcqvYxoj1muvC1nDlrzJYGbpcdHHIMo2FwYv+j3QAAOBSfkZYITwUAAAAAElFTkSuQmCC);
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);
}
<div class="ellipsis">
<div>
<p>
Call me Ishmael. Some years ago – never mind how long precisely – 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 –
then, I account it high time to get to sea as soon as I can.
</p>
</div>
</div>

Slice text within width of p tag

use :

p {
text-overflow: ellipsis;
display block;
width: 100px;
overflow: hidden;
white-space: nowrap;
padding: 5px;
border: solid 1px #000;
}

Example:
http://jsfiddle.net/ezywm/2/



Related Topics



Leave a reply



Submit