68

I tried word-wrap: break-word;, but it separates lines mid word.

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
user784756
  • 2,363
  • 4
  • 28
  • 44

3 Answers3

102

In order to use word-wrap: break-word, you need to set a width (in px). For example:

div {
    width: 250px;
    word-wrap: break-word;
}

word-wrap is a CSS3 property, but it should work in all browsers, including IE 5.5-9.

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Serina Patterson
  • 1,121
  • 2
  • 6
  • 4
29

As long as you specify a width on the element, it should wrap itself without needing anything else.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • 2
    The error was that it was part of a pre element. Once I changed pre to p this answer worked. – user784756 Dec 04 '11 at 11:44
  • What happens if you set the `width: auto`? My button's text still gets cut off at the right edge of it's parent container. Any ideas? – andy4thehuynh Dec 03 '13 at 20:50
  • @andystructible That's probably going to be hard to answer, lacking context (what other styles are applied to it). I'd suggest opening a new question. One possibility is that at some point `overflow: hidden` was applied to the button, or the button is not in the document flow (if, for instance, it's been floated). – Tieson T. Dec 03 '13 at 22:46
  • 6
    If the text doesn't wrap by itself the problem may be that you have `white-space: nowrap;` somewhere in the styles. – Velimir Mar 19 '18 at 17:15
  • thanks @Velimir that one was the solution for mine – nrmb May 18 '18 at 10:30
  • @Velimir Thanks buddy. – Christopher Nolan Oct 18 '21 at 17:29
16
word-wrap: break-word; 

add this to your container that should do the trick

Damathryx
  • 2,706
  • 3
  • 25
  • 48