tags:

views:

142

answers:

2

I have a paragraph of text:

<p>Lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum</p>

How can I make sure that no more than 30 characters are shown on one line with CSS?

+4  A: 

That's not possible with CSS, you will have to use the Javascript for that. Although you can set the width of the p to as much as 30 characters and next letters will automatically come down but again this won't be that accurate and will vary if the characters are in capital.

Sarfraz
So how would I go about doing this in JS? Is it complicated or is it just a one-liner?
Pieter
@Pieter: you can use the javascript's `substr` function for that i suppose and it won't be a single liner (unless a regex might do the trick) but i would suggest you to just setting the width of the paragraph if the requirement is not that strict.
Sarfraz
+1 That is how I solve this problem -- only annoyance, most fonts are variable width, so if some wonderful person puts in MMMMMMMM... they usually break your design or have overflow. If you make the <p> the width of 30 Ms, then there is ofter a lot of space at the end of the div. You need to make a judgement call.
Christopher Altman
+2  A: 

If you use CSS to select a monospace font, the problem of varying character length is easily solved.

Pino
But impacts your visual design.
Christopher Altman