84
body { word-wrap: break-word;}

I've been using that code (above) to fit the text in the body into it's container. However what I don't like about it, is that it breaks up words.

Is there another way where it will not break up words and only line break after or before a word?

EDIT: This is for use within a UIWebView.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Joshua
  • 15,200
  • 21
  • 100
  • 172

14 Answers14

86

use white-space: nowrap;. If you have set width on the element on which you are setting this it should work.

update - rendered data in Firefox alt text

Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103
Vinay B R
  • 8,089
  • 2
  • 30
  • 45
  • 1
    Nope, still nothing. This code still displays the full text with horizontal scrollbar. `body { width: 768px; white-space: wrap;}` – Joshua Sep 23 '10 at 15:19
  • just setting width takes care of wrapping the text a you need(atleast on firefox). can you check behaviours on specific browsers and update the post – Vinay B R Sep 23 '10 at 18:06
  • i have updated my answer to include rendered result on firefox. – Vinay B R Sep 23 '10 at 18:13
  • This still does not work for me, I've edited my question to say what browser this is for. – Joshua Sep 23 '10 at 18:52
  • Actually got it working now! Was accidentally overriding the width without noticing! – Joshua Sep 25 '10 at 15:36
  • 6
    wrap is an invalid property value – Claudiu Creanga Aug 25 '16 at 08:30
  • 6
    This should not be the correct answer, `white-space: wrap;` isn't a valid property value – user1486133 May 04 '17 at 13:03
  • 7
    It's `white-space: nowrap` actually. – AlexioVay Dec 17 '17 at 12:46
  • 1
    1: This doesn't work. 2: Terrible screenshot - don't bother next time if this is the quality. Better: Just post a `jsfiddle`, or online debugger next time. It would have shown you (and everyone else) this doesn't work as you describe. Example of how this does NOT work: https://jsfiddle.net/9dfpyvxk/ -> You really think adjusting the "global" BODY WIDTH is a feasible fix for this? BODY WIDTH affects entire page. – B. Shea Apr 18 '22 at 16:40
30

Please use nowrap and wrap value didn't come for me. nowrap solved the issue.

white-space: nowrap;
Mechanic
  • 5,015
  • 4
  • 15
  • 38
Code
  • 1,574
  • 2
  • 23
  • 37
24

May be a bit late but you can add this css to stop word breaks:

.element {
    -webkit-hyphens: none;
    -moz-hyphens:    none;
    -ms-hyphens:     none;
    hyphens:         none;
}
Ralphonz
  • 633
  • 1
  • 9
  • 22
24

I had the same problem, I solved it using following css:

.className {
  white-space:pre-wrap;
  word-break:break-word;
}
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
sumit dubey
  • 377
  • 2
  • 5
7

You can try this...

body{
white-space: pre; /* CSS2 */
white-space: -moz-pre-wrap; /* Mozilla */
white-space: -hp-pre-wrap; /* HP printers */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: pre-wrap; /* CSS 2.1 */
white-space: pre-line; /* CSS 3 (and 2.1 as well, actually) */
word-wrap: break-word; /* IE */
}

{word-wrap:;} is an IE proprietary property, and not a part of css. firefox's handling is correct. Unfortunately, FF does not support a soft hyphen / . so that's not an option. You could possibly insert a hair or thin space,  /  (check me on the numeric entity) and  / , respectively.

Making {overflow: hidden;} would cut the overflow off, and {overflow: auto;} would cause the overflow to enable scrolling.

Pramendra Gupta
  • 14,667
  • 4
  • 33
  • 34
4

For me, the parent was smaller than the word. Adding will break on spaces, but not words :

    word-break: initial;
Andrew Magill
  • 2,254
  • 4
  • 21
  • 28
2

In my situation I am trying to ensure words wrap at proper word-breaks within table cells.

I found I need the following CSS to achieve this.

table {
  table-layout:fixed;
}
td {
  white-space: wrap;
}

also check that nothing else is changing word-break to break-word instead of normal.

Dave Sag
  • 13,266
  • 14
  • 86
  • 134
1

Use white-space: nowrap;

CSS white-space Property

white-space: normal|nowrap|pre|pre-line|pre-wrap|initial|inherit;

Know more about white-space example

sivahari
  • 37
  • 1
  • 6
1

Word break issue on Firefox browser. So you can use to prevent word-break:

-webkit-hyphens: none;
-moz-hyphens: none;
hyphens: none;
tbraun89
  • 2,246
  • 3
  • 25
  • 44
1

I use this Code for our website to stop the word-breaking:

body {
    -ms-hyphens: none;
    -webkit-hyphens: none;
    hyphens: none;
}
0

To stop words from breaking, you should unset word-break. The default is normal, which allows word breaks but at break points.

overflow-wrap controls when word breaks happen.

You should instead set the overflow-wrap property to always to break words only when they are too long to fit on a line (instead of overflowing).

The default normal to disable word breaks, allowing words too long to overflow the edge of the element.

If your element width is flexible (e.g. min-width, or display:flex), and you want too expand the element instead of line breaking, you can use the value break-word.

The property word-break determines whether words should only break at break points, or always (when they could overflow).

Helpful info and demo on overflow-wrap - MDN Web Docs

Info on word-break

Further things:

  • If you want to disable all word breaks, even when it can't line break, i.e. japanese text, you can use the word-break: keep-all
  • *The value break-word may have been unsupported and is now deprecated, as is the word-wrap CSS property (initially added by MS for this same function).
tutacat
  • 27
  • 4
-1

I faced a similar problem in my grid structure and I used, word-break: break-word; in my grid and my issue got resolved.

-1

This helped me with old Webkit - this one from phantomjs 2.1

.table td {
    overflow-wrap: break-spaces;
    word-break: normal;
}
AssyK
  • 37
  • 1
  • 6
-1
.className {
    hyphens: none;
    word-break: keep-all;
}