My client has requested to enable auto-hyphenation on this page: http://carlosdinizart.com/biography/ , and I realized I've never actually seen it done on a web-page.
Is it possible to set up auto-hyphenation in an HTML document with just HTML/CSS? If not - what are the options?
CSS3 provides some support for this. Source: http://drublic.de/blog/css3-auto-hyphenation-for-text-elements/ You can check the w3c documentation here: http://www.w3.org/TR/2011/WD-css3-text-20110901/#hyphenation
CSS3 adds six properties to the list of useful thing. These are:
hyphens
.hyphenate-resource
so the browser has a better chance to render your text with the right hyphenation.hyphenate-before
sets a minimum number of characters before the hyphenation.hyphenate-after
does the same ashyphenate-before
but for characters after the hyphenation.hyphenate-lines
defines about how many lines a hyphenated word is written at a maximum. withhyphenate-character
you can specify which HTML-entity should be used, e.g.\2010
.The main property of this stack is
hyphens
. It accepts one of three values:none
,manual
orauto
. The default one is manual, where you can set hyphens via­
.auto
it the better one for continuous text while words get split if possible and available. Andnone
does not hyphenate at all even if there is a character set for a possible line break in a certain word.Update:
Browser support information here: http://caniuse.com/css-hyphens
At present my css for
<p>
isThis doesn't work for Chrome 39 on Mac. Known not to work on Opera. Works for Firefox, iOS Safari.
This is NOT foolproof: Narrow columns (under 6 words) are ugly, but overall it makes the layout look far more like properly set type.
An option is to insert soft hyphens into the text in places where it may be broken. The soft hyphen is represented by the entity
­
in HTML. You may find libraries/tools that can prepare text automatically with­
s in the right places, otherwise you'll have to do it manually.To deal with one page that has fixed width for text, the practical move would be to add a couple of SOFT HYPHEN characters (U+00AD), using the entity reference
­
if you find it more comfortable than entering the (invisible) character itself. You can rather quickly find out which words need to be hyphenated to produce a good result.In a more complex case (several pages, flexible width), use a preprocessor, or server-side code, or client-side code that adds soft hyphens. The client-side approach is simplest and can be applied independently of server-side technologies and authoring tools. Beware that automatic hyphenation may go wrong and needs some help: the language(s) of the text need to be indicated in markup (or otherwise, depending on the library used).
At the minimum, you could just put the attributes
lang=en class=hyphenate
into the<body>
tag and the following code in thehead
part:Demo: http://bytelevelbooks.com/code/javascript/hyphenation.html (flexible-width text, with just maximum width set, so you can test it varying the browser window width).