15

I have a string of 30 characters

I want to only display the first 10 followed by ...

so for example


thisisastringthatis30characterslong

would become

thisisastr...


Is this possible using CSS?

Cheers!

Franco
  • 2,846
  • 7
  • 35
  • 54

3 Answers3

11

It´s called ellipsis and you can use it on a block element.

However it does not work in Firefox and you cannot set the width to exactly 10 characters as you cannot specify the width in characters in css.

If you want exactly 10 characters and Firefox compatibility you will have to use javascript or a server-side solution.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • that's awesome - I need this kind of functionality quite a lot - I had never bothered to ask if it existed because I subsumed that it wouldn't. Nice to know about it. – stephenmurdoch Mar 03 '11 at 14:36
  • Cool cheers for that, ill just use a preg_match on the first ten characters and then add on the ... thanks again – Franco Mar 03 '11 at 14:38
8

There is a CSS3 based solution text-overflow:ellipsis; claimed to be crossbrowser. But also it will show as many chars as fit, not exactly 10.

Draco Ater
  • 20,820
  • 8
  • 62
  • 86
  • 2
    It seems that will stop working in Firefox 4: http://stackoverflow.com/questions/4927257/text-overflowellipsis-in-firefox-4 – jeroen Mar 03 '11 at 14:36
0
#containerx {
     display: block;
     overflow: hidden;
     text-overflow: ellipsis;
     max-width: 60%;
}

by adjusting the width you can control number of characters to display

no ai please
  • 732
  • 3
  • 11
  • 24