Posted over 3 years ago. Visible to the public. Repeats.

HTML: Making browsers wrap long words

By default, browsers will not wrap text at syllable boundaries. Text is wrapped at word boundaries only.

This card explains some options to make browsers wrap inside a long word like "Donaudampfschifffahrt".

Option 1: hyphens CSS property

Modern browsers are able to hyphenate natively with the CSS property hyphens:

Copy
hyphens: auto

There is also hyphens: none (disable hyphenations even at ­ entities) and hyphens: manual (hyphenation at ­ only).
This feature was integrated just recently in Chrome 88 while every other major browser already supports it for a long time.

For the hyphens property to work, your document language must be declared, e.g. with <html lang="de">.

Until older versions of Chrome are phased out, you can use a MIT-based polyfill called Hyphenopoly. It will apply JS-based hyphenation if a feature test shows that CSS based hyphenation is not supported for the given locale. Note that Hyphenopoly is primarily a NodeJS module and targeted at server-side hyphenation, but it can be configured for the client-side as well.

Option 2: Soft hyphens

Unicode has a soft hyphen character you can use to mark optional word division opportunities. The soft hyphen is an invisible character with zero width. Only when the browser decides to wrap at a soft hyphen, it is turned into a regular hyphen character ().

The HTML entity for a soft hyphen is &shy;. Use it to mark division opportunities in a long word like so:

Copy
Do&shy;nau&shy;dampf&shy;schiff&shy;fahrt

Caveat: Unexpected copy & paste behavior

Note that when copying a text with soft hyphens, the soft hyphen character will be copied as a zero-width character. This might lead to confusion since you can no longer search for the full word in the pasted text.

Note that test browsers will also see the soft hyphens, which makes testing uncomfortable.

Automatic insertion of soft hyphens

You can use JavaScript libraries like hypher (includes LGPL patterns!) to automatically insert soft hyphens into the text of a DOM node.

Note that integrating such a library well requires some additional work on your part:

  • Loading the correct hyphenization patterns for the language you're using
  • JavaScript to apply the library function to the DOM nodes you want hyphenated
  • Manually adding exceptions to words you don't want hyphenized. E.g. brand names like makandra should not be hyphenated.

Option 3: wbr-tags

HTML has a <wbr> tag you can use to mark word wrap opportunities:

Copy
Do<wbr>nau<wbr>dampf<wbr>schiff<wbr>fahrt

Like a soft hyphen, the <wbr> tag is invisible. Unlike a soft hyphen, the browser will not insert a hyphen character when wrapping at a <wbr>. This makes it a good choice to wrap non-natural language like URLs or source code.

Unlike soft hyphens a <wbr> tag will not be included in the clipboard when copying text.

Option 4: word-wrap (or overflow-wrap) CSS property

You can use the CSS property word-wrap to allow browsers to force words that are wider than their container to be broken.

Copy
word-wrap: break-word

A few things:

  • word-wrap was renamed to overflow-wrap in CSS3. To support Internet Explorer or Edge, use the legacy name word-wrap.
  • Do not confuse this with the word-break CSS property which sounds similar, but actually controlls white-space breaking. A word-break: break-all will break after the last character that fits in a line and is rarely what you want.
  • We recommend you use word-wrap: break-word when (uncontrollable) texts might break your layout, e.g. in a list of URLs.
  • Here is an example: https://jsfiddle.net/51dyzyxm/

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Henning Koch
Last edit:
5 months ago
by Michael Leimstädtner
Keywords:
wrap, silbentrennung, bindestrich, viertelgeviertstrich
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more