Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I indicate long text into a smaller fixed column with CSS?

How do I fit long text into a fixed width column where I have room for only one line of text? The text needs to be cut to a fixed width (lets say to 100px) and I would like to add dots "..." at the end. Something like this for example:

Given string:

Some really long string that I need to fit in there! 

Desired output in fixed-width-column should be:

Some really long string... 
like image 398
Primoz Rome Avatar asked Jan 21 '10 11:01

Primoz Rome


People also ask

How do you cut a long text in CSS?

text-overflow: ellipsis; white-space: nowrap; overflow: hidden; in CSS (or hard-code the style, but CSS is cleaner). If you want to completely cut the text off, use clip instead of ellipsis .

How do you shorten text in CSS?

Solution # 1: Truncate text for single line This solution works for single-line truncation. Force text to be on a single line: white-space: nowrap; Now, our text should be on the same line and should overflow from the box if it's long enough and wrapped before.


1 Answers

You can do this with CSS alone:

.box {     -o-text-overflow: ellipsis;   /* Opera */     text-overflow:    ellipsis;   /* IE, Safari (WebKit) */     overflow:hidden;              /* don't show excess chars */     white-space:nowrap;           /* force single line */     width: 300px;                 /* fixed width */ } 

Note: You will want to check on the latest browser support.

like image 160
Gordon Avatar answered Oct 02 '22 13:10

Gordon