Stop word-wrap dividing words
Asked Answered
R

15

96
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.

Resound answered 23/9, 2010 at 6:11 Comment(3)
What do you suppose "break-word" means?Laughingstock
I knew that, but I say "Is there another way".Resound
alas word-wrap: normal; doesn't help. Nor does white-space: wrap; alone help if you are trying to wrap words in a table cell (my situation).Devinne
W
102

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

Winebaum answered 23/9, 2010 at 8:8 Comment(9)
Nope, still nothing. This code still displays the full text with horizontal scrollbar. body { width: 768px; white-space: wrap;}Resound
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 postWinebaum
i have updated my answer to include rendered result on firefox.Winebaum
This still does not work for me, I've edited my question to say what browser this is for.Resound
Actually got it working now! Was accidentally overriding the width without noticing!Resound
wrap is an invalid property valueAlviani
This should not be the correct answer, white-space: wrap; isn't a valid property valueEckman
It's white-space: nowrap actually.Ambrosius
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: jsfiddle.net/9dfpyvxk -> You really think adjusting the "global" BODY WIDTH is a feasible fix for this? BODY WIDTH affects entire page.Bussell
A
33

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

.className {
  white-space:pre-wrap;
  word-break:break-word;
}
Anabal answered 3/7, 2018 at 17:20 Comment(1)
Third answer is the one that actually answers the question. Both accepted and second answer will cause the text to not wrap at all.Polychaete
P
30

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

white-space: nowrap;
Perdurable answered 15/11, 2017 at 7:19 Comment(1)
This prevents a word from breaking - but it also prevents the whole line of text from breaking.Nitre
S
29

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;
}
Snowinsummer answered 27/7, 2016 at 10:32 Comment(0)
F
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.

Funicle answered 23/9, 2010 at 6:18 Comment(4)
None of the examples you gave me actually work, the only one that does is word-wrap.Resound
alternative is {overflow: hidden;} and {overflow: auto;} but there is a problem to that as well.Funicle
Yeh, but that isn't what I'm looking for as overflow: hidden; would hide everything off the edge. :/Resound
This does not answer the question - which is how to prevent word breaks.Nitre
B
5

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

    word-break: initial;
Basilio answered 18/7, 2020 at 6:38 Comment(0)
M
3

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).
Malaspina answered 24/1, 2023 at 10:24 Comment(1)
This was very helpful for a project in which the prior developers applied this CSS rule to the body element without me realizing it.Trichroism
D
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.

Devinne answered 12/2, 2013 at 23:24 Comment(0)
H
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

Halicarnassus answered 2/8, 2019 at 4:58 Comment(0)
H
1

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

-webkit-hyphens: none;
-moz-hyphens: none;
hyphens: none;
Harri answered 6/1, 2020 at 10:27 Comment(0)
D
1

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

body {
    -ms-hyphens: none;
    -webkit-hyphens: none;
    hyphens: none;
}
Dree answered 27/8, 2020 at 19:8 Comment(0)
H
0
.className {
    hyphens: none;
    word-break: keep-all;
}
Hondo answered 6/6, 2022 at 13:0 Comment(1)
Please add supporting details to your answer to improve it.Whose
M
0

What worked for me was setting overflow-wrap to normal:

overflow-wrap: normal;
Moral answered 27/1 at 13:53 Comment(0)
I
-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.

Isopiestic answered 12/8, 2016 at 9:6 Comment(2)
There's no break-word value for word-break property.Eijkman
This does not answer the question - which is how to prevent word breaks.Nitre
A
-1

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

.table td {
    overflow-wrap: break-spaces;
    word-break: normal;
}
Azotic answered 8/12, 2020 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.