2012-05-04 6:28, Michael Harrison wrote:
> Consider this HTML:
>
> <div style="width:10em; border:solid 1px #000; word-wrap:break-word">
> yt-html5-player-modules::subtitlesModuleData::display-settings
> </div>
The word-wrap: break-word setting wasn't really designed for use like
this. It is meant to specify that in emergency situations, where the
text would overflow from its container, a line break is allowed at any
point, after any character. It is not meant to suppress normal line
breaking behavior.
> The text contains no spaces, but all the browsers I've tested will
> wrap it after the '-' between 'player' and 'modules',
You didn't test on sufficiently many browsers. Breaking after a hyphen
is relatively new in browserworld - an old feature in IE, newer in
Firefox. And even in newest Firefox, the feature is conditional: Firefox
breaks (when needed) after hyphen in foobar-blabla but not in foo-bla,
still less in f-b (which may get broken by IE!). This makes sense for
normal text: you want to let long hyphenated compounds to be broken
after the hyphen, but short compounds are better kept together.
Line breaking behavior in browsers is an ugly mess, see
http://www.cs.tut.fi/~jkorpela/html/nobr.html
> even though
> there's space remaining on the line.
Yep, that's because word-wrap: break-word is for emergencies only, and
there's no emergency here.
> How can I force
You cannot force anything in CSS. See
http://www.cs.tut.fi/~jkorpela/css-caveats.html
> content like this
> to only wrap at the end of the lines?
For the specific case, it would suffice to replace each hyphen by the
nonbreaking hyphen U+2011 (which you might want to enter as ‑:,
especially since the nasty Windows clipboard seems to change the
nonbreaking hyphen to a normal hyphen...). Beware that the nonbreaking
hyphen is present in a few fonts only and may mess up line spacing if
you don't say line-height: 1.3 or something like that.
But if your string contained, say, "(" or "%", you would have the
problem that some browsers break after them, too. And U+FEFF doesn't
seem to be honored by browsers. So this leaves us...
... the magnificent ZWSP, the Zero-Width Space!
Insert it, i.e. U+200B, after each character. If you use the character
reference, your HTML code will look real crap, but it will work (on
modern browsers in most days):
y​t​-​h​t​m​...
If you generate the HTML code programmatically, you can just emit U+200B
as a UTF-8 encoded character, saving some bytes.
Sorry, non-CSS solution. You don't even need word-wrap:break-word if you
use this solution (though you might keep it too, just in case some odd
browser honors it but doesn't honor the magnificent ZWSP).
--
Yucca,
http://www.cs.tut.fi/~jkorpela/