views:

262

answers:

4

I have texts that most of the time fit in a div, but sometimes it doesn't fit. There is no way to determine how many characters/words/paragraphs fit in the div, so I can't cut it off that way.

The div had a fixed height and width, so can we do something with that? Or do you have any suggestion how to get everything in the div, the correct way?

A: 

Yes, you can do something with that. Give the div in question an overflow: hidden. Then set the div height to some multiple of the line-height (this will depend on how many lines you want to show) so you make sure to not cut off the bottom portions of your text.

dclowd9901
A: 

You can use different overflow values in css, "auto" should cause a scrollbar to appear when text is bigger than the height/width will allow, or "hidden" to cut it off.

see: http://www.quirksmode.org/css/overflow.html

Chad
A: 

I've written a truncate function in JavaScript that starts with the block of text and if it detects overflow (scrollheight > height) it starts removing words until it fits, then adds a "view more" link.

Diodeus
+5  A: 

If the text isnt going to fit, you could use a css elipses to make it look a bit cleaner. Perhaps place the text in a hidden element and measure the height to see if you need to include a (more) link.

See http://mattsnider.com/css/css-string-truncation-with-ellipsis/

James Westgate
+1 will be interesting to see if `text-overflow` makes it into CSS3.
bobince
There is always jQuery for when it doesn't. http://www.bramstein.com/projects/text-overflow/
Chad
@Bob: or even a -moz-text-overflow ...
James Westgate
Thanks for the answer! I managed to get it working, but now, the cut offi s after 1 line of text (example: http://shup.com/Shup/324977/110328112012-Home-Roepingen-Mozilla-Firefox.png ). ANy suggestion?
koko
@koko Can you link html instead of image?
James Westgate
@James This is the Url: http://www.toko01.be/nl/statisch/pagina/1/title/homeThanks!
koko