79

Given a relatively simple CSS:

div {
  width: 150px;
}
<div>
  12333-2333-233-23339392-332332323
</div>

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

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Karl Seguin
  • 21,574
  • 5
  • 44
  • 49

11 Answers11

75

Replace your hyphens with this:

&shy;

It's called a "soft" hyphen.

div {
  width: 150px;
}
<div>
  12333&shy;2333&shy;233&shy;23339392&shy;332332323
</div>
Nhan
  • 3,595
  • 6
  • 30
  • 38
Ryan Fox
  • 10,103
  • 5
  • 38
  • 48
  • 35
    ... 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 Oct 20 '08 at 22:02
  • 1
    I was wondering about the browser compatibility of `­`. [According to Quirksmode](http://www.quirksmode.org/oddsandends/wbr.html) (see compatibility table), it does well, with the only issues arising in older browsers such as FF2 and IE5.5-6. – evanrmurphy Oct 23 '12 at 21:05
34

In all modern browsers* (and in some older browsers, too), the <wbr> element is the perfect tool for providing the opportunity to break long words at specific points.

To quote from that link:

The Word Break Opportunity (<wbr>) HTML element represents a position within text where the browser may optionally break a line, though its line-breaking rules would not otherwise create a break at that location.

Here's how it could be used to in the OP's example (or see it in action at JSFiddle):

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

*I've tested it in IE9, IE10, and the latest versions of Chrome, Firefox, and Opera, and Safari.

div {
  width: 150px;
}
<div>
  12333-<wbr>2333-<wbr>233-<wbr>23339392-<wbr>332332323
</div>
Nhan
  • 3,595
  • 6
  • 30
  • 38
aeskr
  • 3,436
  • 1
  • 15
  • 10
18

As part of CSS3, it is not yet fully supported, but you can find information on word-wrapping here. Another option is the wbr tag, &shy;, and &#8203; none of which are fully supported either.

HopelessN00b
  • 452
  • 2
  • 8
  • 22
John Downey
  • 13,854
  • 5
  • 37
  • 33
8

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>
juzraai
  • 5,693
  • 8
  • 33
  • 47
Peter T. LaComb Jr.
  • 2,935
  • 2
  • 29
  • 44
  • 4
    This shouldn't make a difference, right? Or, unless you put those spans on nowrap. – Zorf Jun 08 '10 at 02:19
8

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.

Dave Rutledge
  • 5,525
  • 7
  • 27
  • 24
7

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.

Up_One
  • 5,213
  • 3
  • 33
  • 65
Orion
  • 71
  • 1
  • 1
  • Beware of browser incompatibilities. See the compatibility table here: http://www.quirksmode.org/oddsandends/wbr.html – Tim Abell Apr 11 '12 at 15:50
  • Just a note, `­` is by far the better option here. All the others break font text kerning, for instance. – Marius May 15 '17 at 13:01
2

You can also use :

word-break:break-all;

ex.

<div style='width:10px'>ababababababbabaabababababababbabababa</div>

output:

abababababa
ababababbba
abbabbababa
ababb

word-break is break all the word or line even if no-space in sentence that not feets in provided width or height. nut for that you must be provide a width or height.

Ajay Gupta
  • 2,867
  • 23
  • 28
0

The non-breaking hyphen works well.

HTML Entity (decimal)

&#8209;
ImaJedi4ever
  • 3,291
  • 1
  • 18
  • 11
0

Instead of - you can use &hyphen; or \u2010.

Also, make sure the hyphens css property was not set to none (The default value is manual).

<wbr> is not supported by Internet Explorer.

TObject
  • 41
  • 2
0

Hope this may help

use <br>(break) tag where you want to break the line.

Mohammed Wahed Khan
  • 836
  • 2
  • 14
  • 35
0

You can use 0 width space after hyphen character:

div {
  width: 150px;
}
<div>
  12333-&#8203;2333-&#8203;233-&#8203;23339392-&#8203;332332323
</div>

if You want line break before hyphen use &#8203;- instead.

jcubic
  • 61,973
  • 54
  • 229
  • 402