New CSS3 Properties to Handle Text and Word Wrapping

New CSS3 Properties to Handle Text and Word WrappingAbout a year and a half ago, I wrote about CSS3′s word-wrap property. The angle of the article was the fact that it was a feature that was new in CSS3 that didn’t exist in CSS2.1 and it worked in just about every browser, including old IE.

Well, now that’s all changed, which I discovered while researching additions to my CSS3 Click Chart. The word-wrap property has been removed from the CSS3 spec and other related properties have been added.

Text-Wrap

Update (Jan. 7, 2013) This property is no longer in the spec.

The text-wrap property “specifies the mode for text wrapping”. Well, that description doesn’t tell us a whole lot, so let’s break down the values.

normal
This is the initial, or default, value for text-wrap. This is what happens when you don’t have any text wrap settings specified, so a value of “normal” is what you see occurring in any given element that has text.

none
This tells the browser that text in that element should not break at all. So if the text reaches the end of the parent container, it will overflow its parent and any overflow settings would handle the text’s visibility.

avoid
This value will still allow line breaking to occur, but this tells the browser to be selective as to where breaks happen. A nice example is shown in the spec in this section which basically shows that the browser will interpret parts of the markup so as to decide where to break lines and where to avoid breaking them.

Browser Support: None that I know of.

Overflow-Wrap

The overflow-wrap property is the replacement for the now-obsolete word-wrap property. The values are either “normal” or “break-word” — the same values that word-wrap uses.

This particular property, like word-wrap, handles what the spec refers to as “emergency wrapping”. The spec explains: “This property specifies whether the UA may break within a word to prevent overflow when an otherwise-unbreakable string is too long to fit within the line box.”

The spec also explains that if text-wrap is set to “none”, then overflow-wrap will have no effect. Also, although this is for breaking a line in the middle of a word, the word will not be hyphenated. More on hyphenation below.

Browser Support: None that I know of.

Line-Break and Word-Break

According to the spec, these two properties have been introduced to help deal with the problems that arise from browsers assuming that lines and words need to break as is common in the English language. For example, in English a browser will not break a line within a word, but only where there is a space or certain punctuation marks. But in other languages, these rules don’t apply.

As the spec explains, although English lines may break at space characters or punctuation, this is not true in other writing systems that don’t use spaces and punctuation. In these writing systems “a line break opportunity is based on character boundaries, not word boundaries”. Thus, line breaks often have to be supressed within certain character combinations.

I honestly don’t fully understand all the values for these properties, so I won’t attempt to expand on them here. And judging by the description, it seems these two properties would be most useful to those dealing with content in languages that require different line breaking rules than that of English.

Browser Support: None that I know of.

Text-Overflow

The text-overflow property defines how to handle text that overflows its parent when the parent’s overflow is set to something other than “visible”. It accepts the following values:

clip
With this value (the default), the text will be cut off and the remainder would be invisible. This happens by default in all browsers now. Keep in mind that in order for this to take effect, the text would have to be inside an element that, for whatever reason, doesn’t allow natural line breaking.

ellipsis
With this value, the text will still be cut off but the fact that it’s incomplete will be indicated by an ellipsis (i.e. three dots “…”).

As shown below, browser support is very good for this property. Old versions of IE did include support, but it seems the implentation has changed and so newer IE uses prefixes. For more on this and a working live example, see this section on the click chart.

Browser Support: IE6+, Firefox 7+, Chrome 1+, Safari 1.3+, Opera 11+

Hyphens

And finally, CSS3 now introduces a hyphens property that can be used to tell the browser whether or not to automatically insert hyphens.

none
A value of “none” tells the browser to never hyphenate, even if break points are somehow defined.

manual
This value is the default and is how browsers naturally do hyphenation. This means that the browser won’t do any hyphenation unless there are hyphenation characters indicated. You can actually do this using the soft hyphenation character, which, inside of a word, looks like this: hy­phens.

auto
With a value of “auto”, the browser will automatically insert word breaks and hyphenation in accordance with the language defined for that page.

Hyphens have some support so you can see a working example with the accompanying code here.

Browser Support: IE10+, Firefox 6+, Safari 5.1+ (all require vendor prefixes; thanks to Hans for letting me know about support in Safari)

Conclusion

Although browser support is not great for these new features, it’s nice to know that in the future we’ll have some powerful tools at our disposal to handle overflow and line breaks in a more fine-grained manner.

Advertise Here

16 Responses

  1. Jon B:

    What I think is missing is a ‘text-overflow: ellipsis’ that works with vertically-sized containing elements also. For example, if there is too much text to fit into a box with a fixed height, the text that would overflow or be ‘cropped’ would be hidden and the standard ‘…’ appended. Currently this only works for text that overflows horizontally – which is only possible if for words not to wrap :(

    • Jack:

      Yeah, that’s really what we need. Ellipses are great for single line items like blog titles in a list or something, but for excerpts of text and such the height is the greater concern, not a single line that fits horizontally and ends…

    • Frank:

      I second that \ :D \

  2. Thanks for the share, I have been reading everything I can find about CSS3 lately.

  3. Hans:

    I did not know that word-wrap was removed. Thanks for telling us!

    CSS3 hyphens are supported in Safari 5.1 but still not in Chrome 18. I’ve made this fiddle to check and blogged about the whole hyphenation thing some time ago.

  4. SelenIT:

    > it worked in just about every browser, including old IE.
    > Well, now that’s all changed…

    WHY??? What was the need to drop the well-known, well-documented and widely supported for more than a decade feature in favor of the other, that is exactly the same except of the name, which is much longer and less memorable? As for me, ‘overflow-wrap’ sounds more confusing, making me think first about vertical overflow of the box, while ‘word-wrap’ is directly connected with the handling of long words (to make them fit their wrappings) and looks more descriptive for its result. Why do CSS3 editors believe that ‘overflow-wrap’ “is a better name”? I hope this is not only because ‘word-wrap’ was first introduced by Microsoft?

    I can agree that things like flexboxes can change (new suggestions in fact look better than those that had experimental, prefixed support in Gecko and Webkit), but replacing the working feature with the not working one just because of perfectionism seems to be a big mistake. I also hope that the good sense will win and this mistake will be corrected (the well-supported name will be returned) before the spec matures.

  5. thelincster:

    Has anyone checked out the Bacon jquery plugin that wraps text on a bezier curve?

    http://baconforme.com/

  6. Though it would improve text-align:justify, it would be still not as good as in books as we have no manual control over kerning/tracking, especially if user can resize text.

    Word breaking used in books to save space, to make book smaller. There is no reason to make sites smaller because text fit into one page anyway.

  7. Excellent post.

    The way this is headed, CSS will soon be it’s own programming language without the need of implementing other scripts in order for a page to display and function beautifully.

  8. Carlos Atencio:

    Thank you very much!! This was a life saver

  9. canciller:

    thanks a lot.

    this is a very interesting website. love when content is updated.

    definetly bookmarked.

  10. damu:

    My project manager is fan of your nav mouse hover effect :-)

  11. W3C validator reports “Property text-wrap doesn’t exist : none”, but you cant use a substitute since there is none.

    • Yes, I’ve added a note in that section that says it’s no longer in the spec. I don’t think you need it so I doubt a substitute is necessary.

Leave a Reply

Comment Rules: Please use a real name or alias. Keywords are not allowed in the "name" field. If you use keywords, your comment will be deleted, or your name will be replaced with the alias from your email address. No foul language, please. Thank you for cooperating.

Instructions for code snippets: Wrap inline code in <code> tags; wrap blocks of code in <pre> and <code> tags. When you want your HTML to display on the page in a code snippet inside of <code> tags, make sure you use &lt; and &gt; instead of < and >, otherwise your code will be eaten by pink unicorns.