Ever since switching from TABLE-layout to DIV-layout, one common problem remains:
PROBLEM: you fill your DIV with dynamic text and inevitably there is a super-long word that extends over the edge of your div column and makes your site look unprofessional.
RETRO-WHINING: This never happened with table layouts. A table cell will always nicely expand to the width of the longest word.
SEVERITY: I see this problem on even the most major sites, especially on German sites where even common words such as ‘speed limit’ are very long (‘Geschwindigkeitsbegrenzung’).
Does anyone have a workable solution to this?
Soft hyphen
You can tell browsers where to split long words by inserting soft hyphen (
­):may be rendered as
or
A nice regular expression can ensure you won’t be inserting them unless neccessary:
Browsers and search engines are smart enough to ignore this character when searching text, and Chrome and Firefox (haven’t tested others) ignore it when copying text to clipboard.
<wbr>elementAnother option is to inject
<wbr>, a former IE-ism, which is now in HTML5:Breaks with no hyphen:
You can achieve the same with zero-width space character
​(or​).FYI there’s also CSS
hyphens: autosupported by latest IE, Firefox and Safari (but currently not Chrome):However that hyphenation is based on a hyphenation dictionary and it’s not guaranteed to break long words. It can make justified text prettier though.
Retro-whining solution
<table>for layout is bad, butdisplay:tableon other elements is fine. It will be as quirky (and stretchy) as old-school tables:overflowandwhite-space: pre-wrapanswers below are good too.