Examples of ruby

These are to accompany an HTML5Doctor.com article; “The ruby element and her hawt friends, rt and rp”.

Japanese with <ruby>

…without <rp> parentheses

攻殻こうかく機動隊きどうたい

…using <rp> parentheses

攻殻こうかく機動隊きどうたい

…using generated content instead of <rp>

攻殻こうかく機動隊きどうたい

While there’s a lot less markup, you can’t turn it off for supporting browsers — not recommended. We need Modernizr for <ruby>

Nested <ruby> example

こうかくどうたいKōkakukidōtai

Nesting <ruby> lets us achieve most of what ‘complex <ruby>’ was specced to do.

The only thing I’ve found that ‘complex <ruby>’ can do but HTML5 <ruby> can’t is Korean-style ruby text before base text, although this could be addressed in the CSS3 Ruby module. While it’s possible to fake using CSS, we don’t even need to if we add the hanja to <rt> and use hangul as the base text.

Chinese with ruby

Chinese bopomofo (not yet supported)

ㄕˋㄕㄤˋㄨˊㄋㄢˊㄕˋㄓˇㄆㄚˋㄧㄡˇㄒㄧㄣㄖㄣˊAnything can be done with enough perseverance

Currently no browser supports correct bopomofo display for horizontal text — the <rt> text should be displayed vertically beside each character. However IE6+ should display bopomofo correctly (to the right) for vertical text.

Note that in some browsers the ‘horizontal dash’ bopomofo character “” sometimes renders as vertical. Check the “Origin of zhuyin symbols” table in this Wikipedia article for the line “ i, y From ”. Webkit-based browsers: vertical here, horizontal on Wikipedia; Opera: horizontal on both; FF: vertical on both. Bugs eh.

I’ve faked horizontal bopomofo display via CSS below though…

Chinese Pinyin

The Grass mud horse草泥馬(Cǎo Ní Mǎ) — is … characterised as “lively, intelligent and tenacious”. However, their existence is said to be threatened by the “river crabs” — 河蟹 (héxiè) — which are invading their habitat.

Beware of the changes <rt> text makes to leading (<line-height>) in supporting browsers.

The second <ruby> instance here is styled to be de-emphasised in non-supporting browsers, without affecting supporting browser rendering. I used font-size: 60%; for <rt> (the same as Webkit’s default style), and added a space to each <rp>, which improves things for non-supporting browsers quite a bit. Should you wish to de-emphasise ruby text non-supporting browsers this is what I’d recommend.

Using <ruby> for English pronunciation, dictionary style

cromulent (crôm-yü-lənt), n. — Suitable, fitting, reasonable — often used in riposte by the erudite when their colloquy is cumbered by pedants, lingual or otherwise; e.g. “That is a perfectly cromulent usage”

Given dictionaries typically place the phonetic pronunciation in brackets after the defined term, I’ve faked that with rt, rp {display: inline; font-size: 100%;}. The W3 CSS3 Ruby Module had much better control, including ruby-position: right; but has zero implementation and is being completely rewritten.

As to whether this is a … cromulent use of <ruby>, that depends on whether you see it as a presentational hack allowed for internationalisation, or a legitimate way of annotating phonetic information, similar to <abbr>. As someone who has had to guess the pronunciation of a Japanese name from kanji more than once, I’m firmly in the latter, so I think associating pronunciation in English is appropriate. Hixie and Annevk both suggested that unless the semantic information was really necessary <ruby> is overkill for any content that would be displayed inline by default anyway.

Korean <ruby> via CSS

漢字(한자)

한자(漢字)

The top example is of old-style Korean ruby usage, with hangul following the hanja (smaller and to the right in vertical text). The bottom example is of current Korean ruby use, with the hangul appearing before the hanja in parentheses. I perceived ruby text as phonetic information for the base text as that’s how it is in Japanese, but the spec says:

Ruby annotations are short runs of text presented alongside base text, primarily used in East Asian typography as a guide for pronunciation or to include other annotations.

As the hangul is now the main content and the hanja only for disambiguation I think they fall under “other annotations”. While it’d be fine to add the parentheses via generated content, I’ve left them as <rp> in case styles are turned off.

Thanks to Gen of Mozilla and Channy for Korean advice.

Other experiments

Carl’s CSS for faking <ruby> via display:inline-table

攻殻こうかく機動隊きどうたい

This fakes native <ruby> display using display:inline-table and display:table-header-group. Unfortunately it fails in Chrome 5 due to some conflict between native <ruby> display and this CSS. From a comment by Carl in Mike Smith’s weblog.

Chrome demo; display:inline-table on <ruby>

攻殻こうかく

Chrome 5 currently doesn’t display the <rt> content (known and fixed bug, thanks Roland!) — Webkit and Safari do

Chrome demo; display:inline-table on <span>

攻殻こうかく

Chrome does display the <rt> content when using <span> instead of <ruby>. In Chrome/Webkit <rt> is unstyled as the default style is ruby > rt {font-size: 60%;}

hiragana.jp CSS

攻殻こうかく機動隊きどうたい

Another version of this CSS from Hiragana Megane, a service that adds hiragana to any page with Kanji. This one is based on ‘complex <ruby>’, uses <rb> (ruby base text) elements, and requires each ruby base/text combo to be in an individual <ruby> element.

Faking Chinese Bopomofo

ㄕˋㄤˋㄨˊㄢˊㄕˋㄓˇㄚˋㄡˇㄣˊAnything can be done with enough perseverance

(Or as Hixie would say “Things that are impossible just take longer” ;-)

I hacked something together in CSS to get a screenshot of what this should look like (manually marking up the bopomofo and positioning using vertical-align). Note that using a relative font size for <ruby> leads to the bopomofo popping out when it reaches minimum legibility size. An absolute size allows the bopomofo to scale into unreadability :-)

Thanks to Kenny Luck on WHATWG IRC for bopomofo advice.

Further reading