Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css word-wrap: break-word won't work

Tags:

html

css

I have two inline span. code sample:

    <div class="comment_content">

        <span class="comment_author"><?= $child_comment['comment_author'] ?></span>
        <span class="comment_text"><?= $child_comment['comment_content'] ?></span>

    </div>

And scss sample:

.comment_content {        
    word-wrap: break-word;
}
.comment_author {
    display: inline-block;
    vertical-align:top;
}
.comment_text {            
    display: inline-block;
    word-wrap: break-word;
    width: 100%;
}

If my expected view has to be: Expected result

If user enters string without spaces, string won't break. And breaks design: Problem

How properly break long words ??

like image 727
Ramūnas Pabrėža Avatar asked Apr 05 '16 06:04

Ramūnas Pabrėža


People also ask

Why is word-wrap not working in CSS?

Normal in CSSBecause there is no soft wrap opportunity in it, and the value of the overflow-wrap property is normal , the word overflows its container. It describes the default line-breaking behavior of the system.

How do I fix word-break in CSS?

For English and many other languages, the correct breaking means hyphenation, with a hyphen added at the end of a line when a break occurs. In CSS, you can use hyphens: auto , though you mostly still need to duplicate it using vendor prefixes.

How do you force text wrap in CSS?

Today I'm going to talk about a rarely used but extremely useful CSS property, the word-wrap. You can force long (unbroken) text to wrap in a new line by specifying break-word with the word-wrap property.

How do I force text wrap in word?

Enable or disable text wrapping for a text box, rich text box, or expression box. Right-click the control for which you want to enable or disable text wrapping, and then click Control Properties on the shortcut menu. Click the Display tab. Select or clear the Wrap text check box.


2 Answers

white-space: nowrap will prevent word-break from taking effect. Some templates apply white-space: nowrap which necessitates overriding this attribute with white-space: normal.

like image 142
Peter Lindsten Avatar answered Oct 21 '22 22:10

Peter Lindsten


To properly break long-words it requires to combine several CSS properties, I would suggest defining and applying helper class like this:

.break-long-words {
  overflow-wrap: break-word;
  word-wrap: break-word;
  word-break: break-all;
  word-break: break-word;
  hyphens: auto;
}

Properties explained:

  • overflow-wrap and word-wrap are aliases for same thing but some browsers support one and not the other so we need them both. They are offical "correct" way to break words but they don't have any effect on elements with dynamic width so we need more...
  • word-break is originally intended for requiring a particular behaviour with CJK (Chinese, Japanese, and Korean) text but works similar to word-wrap but in WebKit break-all value breaks everything and everywhere. For that reason we must use non-standard and poorly documented WebKit-only break-word value.
  • and optionally, if it makes sense for break words to have hypens (ie. for URLs it probably doesn't) we can use hypens in browsers that support them (at the moment Firefox, Safari, Edge and IE 10+). Also note that in Firefox hypens don't work if you have word-brake property so unless you have fixed-width container you must choose between hypens on FF or legacy support.

Note that I omitted vendor prefixes but if you don't use something like Autoprefixer than this is full verison:

.break-long-words {
  overflow-wrap: break-word;
  word-wrap: break-word;
  -ms-word-break: break-all;
  word-break: break-all;
  word-break: break-word;
  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
}
like image 34
Teo Dragovic Avatar answered Oct 21 '22 22:10

Teo Dragovic