Using Text-Overflow

July 21, 2004

I am sure many of you have used the css property overflow. It works great for making divs scroll or getting that pixel perfect box. Well, I was rummaging through gmail’s source code (which they have made quite difficult to do) and ran across the text-overflow property, and what a beauty this one is. I usually go to our programmer when I need to have a limit on the number of characters allowed in a paragraph or table cell. No more do I need his help with this matter.

The text-overflow property defines what should happen when you have text that falls outside of it margins. See the example below.

.box {
        margin: 0 20px;
	text-overflow:ellipsis;
	overflow:hidden;
	white-space:nowrap;
	border: 1px dashed #333;
	width: 300px;
	padding: 10px;
	  }

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce et nulla sed nunc suscipit tincidunt. Cras ornare mi sed eros viverra vulputate. Nunc eleifend. Mauris posuere turpis et nulla. Curabitur tempor magna ut nibh. Suspendisse tristique porttitor velit. Donec sit amet purus. Maecenas commodo, metus nec commodo dignissim, tortor neque luctus wisi, eu congue ante wisi aliquam lacus. Donec pharetra ultricies arcu. Mauris vitae tellus. Sed pretium, metus in porta accumsan, augue lectus semper metus, vitae vehicula nisl dui ac nisl. Vivamus eget ipsum. Quisque congue lacus. Aliquam ligula dolor, placerat quis, varius ac, laoreet vel, massa.

You will note that it cut off the text after 300 pixels. Doesn’t the overflow property do that already, you ask? Yes, it does but open this page in Internet Explorer and you will see that it has added ellipsis (…) to the end of that line where it cut it off. The only caveat is that text-overflow only works in IE (you may proceed to open IE now) with the ellipsis but you still get the same effect in Mozilla just no ellipsis, and I can live with that. Now this works the same with table cells. See below and note that you must set table to have the table-layout: fixed property for it to work.

.tbl {
	table-layout:fixed;
	border-top: 5px solid #333;
	border-collapse: collapse;
	background: #fff;
	 }
.tbl td {
	border-bottom: 1px dashed #333;
	padding: 2px 5px;
	text-overflow:ellipsis;
	overflow:hidden;
	white-space:nowrap;
 } 
The Pioneer Sites of Standards Based Design
Name Website Location
Eric Meyer www.Meyerweb.com Ohio
L. Jeffrey Zeldman www.zeldman.com New York
Doug Bowman www.StopDesign.com San Francisco

Overall it is a great property that allows you to skip the step of asking a programming to limit number of characters on a line. If you have a gmail account you can check out how they used it on the inbox and their page is fluid so the overflow grows as you re-size your browser.

A Reader's Toolbox For webmaster, the need of high speed internet broadband increases all the more. As they have to perform seo, and upload javascripts or html templates, that requires a high connection. Especially who offer online certifications like EX0-100 and 640-863 or other kinds of computer software, the installation of wireless networking is a must.

Comments

Jeff Croft said:

Handy. As much as I hate to admit it, some of MS's extensions to CSS are pretty useful. Hopefully one day this (or something like it) will be part of the standard so we can all feel good about using it...

Posted on Jul. 21, 2004 07:53 #

France said:

I've been guilty of using MS-specific CSS properties here and there too (display:inline-block anyone?), but usually do so when I can provide an alternative for every other modern browser.

Two possibilities come to mind to emulate the ellipsis for EOMB™ - either through a background-image on the right or use :after pseudo-element - say, .box:after { content: "…"; }. I haven't tested it but this type of brainstorming is what we're all about, right?

Posted on Jul. 21, 2004 10:51 #

Zach said:

Off topic -

If you would put the letter 'o' in your name after the 'b', you would totally have the name of my neighborhood.

Scarborough.

Posted on Aug. 2, 2004 13:46 #

Justin Wood (Callek) said:

This is/was in one of the CSS3 specs at some point, I dont have the time to look it up right now, but I do remember reading it.

Posted on Sep. 7, 2004 02:53 #

Jim S said:

hey,
I am working on a project, building an aim chat client (html based). I was using gmail and noticed a really nice box on the left side that said "Lables". From that point on, I decided I would not settle on anything less. To make the box, they used a custom code. They called the function "D", and hid the content in a JavaScript file. I tried to get the JavaScript file with no success. One of you said you were going through Gmail's code. Did anyone get the JavaScript code? could anyone get it for me? I would be very thankful for any help you can give me. Thanks. The following is what they placed for the function.

D=(top.js&&top.js.init)
If you could either email me the source code in the JavaScript files (send it as an attachment) I would be very thankful! Please Help me

Posted on Sep. 7, 2004 16:03 #

James said:

OK, this is pretty cool. One problem, though: if you have spaces or hyphens in your text, it introduces line breaks anyway. Any ideas how to get around this? My current solution is to remove hyphens and convert all spaces to  , but that's sort of ugly.

Gosh, wouldn't it be nice if overflow: hidden just worked on TD elements?

Posted on Sep. 29, 2004 13:54 #