views:

2063

answers:

5

give a relatively simple css:

<div style="width:150px;">
  12333-2333-233-23339392-332332323
</div>

How do I make it so that the string stays constrained to the width of 150, and simply wraps to a newline on the hyphen?

+25  A: 

Replace your hyphens with this:

&shy;

It's called a "soft" hyphen.

Ryan Fox
... and it will not show up in the display unless there is an actual break there. So it is no good if you always want the hyphen displayed.
Chris Marasti-Georg
+5  A: 

Word-wrapping is part of CSS3 (read not yet fully supported) and you can find information on it at http://www.css3.info/preview/word-wrap/. Another option is the wbr tag, &shy;, and &#8203; none of which are fully supported either.

John Downey
+3  A: 

Your example works as expected in Google Chrome, Safari (Windows), and IE8. The text breaks out of the 150px box in Firefox 3 and Opera 9.5.

Additionally &shy; won't work for your example, as it will either:

  • work when word-breaking but when not word-breaking not display any hyphens, or

  • work when not word-breaking but display two hyphens when word-breaking since it adds a hyphen on a break.

davebug
+4  A: 

In this specific instance (where your string is going to contain hyphens) I'd transform the text to this server-side:

<div style="width:150px;">
  <span>12333-</span><span>2333-</span><span>233-</span><span>23339392-</span><span>332332323</span>
</div>
Peter LaComb Jr.
This shouldn't make a difference, right?Or, unless you put those spans on nowrap.
Lajla
A: 

Depending on what you want to see exactly, you can use a combination of hyphen, "soft hyphen", and/or "zero width space".

On a soft hyphen, your browser can word-break (adding an hyphen). On a zero width space, your browser can word break (without adding anything).

Thus, if your code is something like :

111111&shy;222222&shy;-333333&#8203;444444-&#8203;555555

then your browser will show this with no word-break :

1111112222222-33333334444444-5555555

and this will every possible word-break :

111111-
222222-
-333333
444444-
555555

Just pick up the option you need. In your case, it may be the one between 4s and 5s.

Orion