Ruby Styling (draft!)

Intended audience: XHTML/HTML coders (using editors or scripting), script developers (PHP, JSP, etc.), CSS coders, and anyone who wants to know how to use ruby annotations.

Ruby is the name given to the small annotations in Japanese and Chinese content that are rendered alongside base text, usually to provide a pronunciation guide, but sometimes to provide other information. We will assume that you are familiar with ruby, and how you want it to look. (If not, see the short overview of how ruby works.)

This article will only discuss how to use CSS styling to affect the rendering of ruby content. For information about how to create the markup needed to support ruby, see Ruby Markup.

The editor's draft of the CSS Ruby Layout Module Level 1 provides a number of initial properties for describing the placement of ruby text in relation to the base text. Later versions of the spec are expected to add more properties. Note that this specification is not yet finalized, so this page will aim to give you an idea of what to expect if it is fully implemented, as well as describing what is currently supported.

We won't simply reproduce the spec itself here, but rather provide guidance about how content authors can achieve key techniques. We will assume that your ruby content is marked up appropriately, so that the mapping between ruby bases and ruby text is correct.

Positioning ruby relative to the base text

This section looks at how you make the ruby text appear above, below or to the side of the ruby base text.

Positioning ruby text 'over' the base

By 'over' we mean above horizontal text and to the right of vertical text.

Horizontal ruby for 水芝居(みずしばい)Vertical ruby for 水芝居(みずしばい)

This is the default behavior, and you can expect browsers to produce this without CSS.

If you need to place the ruby explicitly in this position, use:

ruby { ruby-position: over; }

To position the ruby text below horizontal base text or to the left of vertical text, use ruby-position: under.

Positioning ruby text 'under' the base

By 'under' we mean below horizontal text and to the left of vertical CJK text.

Horizontal ruby for  水芝居(みずしばい)Vertical ruby for  水芝居(みずしばい)

This position is often used in Japanese for semantic information (as opposed to phonetic labelling). It is also used sometimes for pinyin annotations in Chinese. Here is an example from the moedict dictionary.

To produce this effect, use:

ruby { ruby-position: under; }

Positioning bopomofo ruby

In Traditional Chinese, bopomofo (zhùyīn fúhào) ruby appears to the right of the base character, whether the text is set horizontally or vertically. Furthermore, the bopomofo annotation is always set vertically and the tone marks (apart from the light tone) are displayed in an additional column to the right of the bopomofo characters.

Horizontal ruby for 電(ㄉㄧㄢˋ)腦(ㄋㄠˇ)Vertical ruby for 電(ㄉㄧㄢˋ)腦(ㄋㄠˇ)

What you have to do in CSS is indicate that this will be bopomofo ruby, so that the annotation doesn't appear 'over' or 'under' the base text. To do that, you need the inter-character value of ruby-position.

The vertical placement for the bopomofo and the relative position of the tone characters within the invisible column to the right of the Han base character rely on the browser, or possibly font information. You don't need to specify anything more in CSS.

ruby { ruby-position: inter-character; }

Positioning double-sided ruby

Occasionally, it is necessary to associate more than one annotations with the same base text. In this case, you need to specify which annotation goes where.

Horizontal ruby for 電(ㄉㄧㄢˋ)腦(ㄋㄠˇ)

This is complicated a little by the fact that there are two possible ways to create markup for double-sided ruby (see the Ruby Markup article for more details).

If you use the 'tabular' model of markup, the styling is reasonably straightforward and involves setting ruby-position on the appropriate rtc element. Given markup such as the following:


you could use CSS like this:

ruby { ruby-position: over; }
rtc { ruby-position: under; }

If you use nested markup, and you have markup such as the following:


you could use CSS like this:

ruby { ruby-position: under; }
ruby ruby { ruby-position: over; }

Rendering ruby text inline

In some situations you may want the annotations to appear inline, after the base text. For example, the Ruby markup article describes how complicated kanji characters with ruby on top can create accessibility issues. In other cases you may want to do this because the user interface is too small for ruby text to be legible, or because you want to repurpose the content for another type of application, etc.

When both base text and annotation are side by side on the same line, it's important to be able to identify which is the annotation, and where it starts and ends.

The chief issue with all these approaches is that there isn't one set of CSS rules that can be applied to all content. If your page mixes the rp, interleaved, and tabular approaches, you'll need to use classes to indicate which set of CSS rules to apply to that particular ruby element.

Working with rp markup

If you use rp markup to specify what characters to use as delimiters and where to place them, you need to render the annotation inline and make the delimiters (which are invisible by default) visible.

You could use this, which changes the display value for the rt elementand sets the font size to be the same as the base text (overriding the default size for the rt element set by the browser).

rt { display: inline; font: inherit; }
rp { display: inline; }

The rest of this section looks at how you can use CSS if your content doesn't have rp markup.

Working with interleaved markup

If you are quite happy for annotations to appear immediately after the base character(s) they are attached to, your content may be using the interleaved approach to ruby markup, and the expected outcome would be like this.

Horizontal ruby for 電(ㄉㄧㄢˋ)腦(ㄋㄠˇ)Vertical ruby for 電(ㄉㄧㄢˋ)腦(ㄋㄠˇ)

In addition to making the rt content display appropriately inline (as mentioned earlier), you will also need to surround each annotation with something, to set it off from the base text. Here we use the before and after pseudo-elements to surround it with parentheses.

rt { display: inline; font: inherit; }
rt::before { content: '('; }
rt::after { content: ')'; }

Working with tabular markup

If you want all the annotations for a given word to follow that word, grouped together as shown here, your content will use the tabular markup approach.

Horizontal ruby for 電(ㄉㄧㄢˋ)腦(ㄋㄠˇ)Vertical ruby for 電(ㄉㄧㄢˋ)腦(ㄋㄠˇ)

Producing inline annotations in this case is a little more complicated if you're not using rp markup.

rt { display: inline; font: inherit; }

/* Parens around <rtc> */
rtc::before { content: "("; } rtc::after { content: ")"; }

/* Parens before first <rt> not inside <rtc> */
rb + rt::before, rtc + rt::before { content: "("; }

/* Parens after <rt> not inside <rtc> */
rb ~ rt:last-child::after, rt + rb::before { content: ")"; }
rt + rtc::before { content: ")("; }

Aligning annotations and bases

When the ruby text annotation is longer than the ruby base it belongs to, or vice-versa, there can be several different ways of dealing with the extra space that is lying around. The CSS Ruby spec deals with this mostly through the use of the ruby-align property.

It's best to use this property on the ruby element.

If you are working with inter-character bopomofo ruby, none of this is relevant, since the positioning of the bopomofo characters and tone marks is fixed.

Aligning to one edge

Picture of start-aligned text.

If you want to align the edges of the annotation and the base, you can use the start value of ruby-align. (An older version of the Ruby CSS spec also included an end value, but that has been removed from the current version.)

ruby { ruby-align: start; }

Centering shorter items of text

Picture of start-aligned text.

If you want the shorter of the ruby base or ruby text to be centered, with the characters set solid, rather than aligned with an edge, it will hardly be a surprise to learn that you just need to set the value of ruby-align to center.

ruby { ruby-align: center; }

Applying justification to shorter items of text

If you want to use up redundant space by stretching the shorter text, be it the annotation or the base, you have two choices: you can justify the shorter text from edge to edge of the longer one (space-between), or justify across a space that is slightly smaller than the full width available (space-around).

Note, however, that the justification outcome differs according to what scripts are involved.

Justifying East Asian characters

Let's look first at what happens if the characters in your ruby annotation are Han characters and kana characters. This is likely to be the case for Japanese ruby.

To widen the shorter text to exactly the same width as the longer text, stretching the inter-character spaces equally, use the space-between value of ruby-align.

Picture of start-aligned text.

ruby { ruby-align: space-between; }

Or you can stretch the text in the same way, but not quite so far, leaving half a character width of space on either side, as shown here. For this you use the space-around value.

Picture of start-aligned text.

ruby { ruby-align: space-around; }

Justifying other characters

Here we look at what to expect if either the base or the annotation uses a script such as Latin for pinyin. For Chinese ruby that doesn't use bopomofo, this is the most likely case.

We are still working with the space-between and space-around values of the ruby-align property, and they still affect whether the text is justified to the edge of the longer text or not. But the result varies according to whether or not the justified text contains spaces.

If a single word is used, you should expect it to be centered, as shown below on the left.

If there are spaces in the shorter text, then it will be justified in the normal way for that script, ie. you can expect to see inter-word spacing for Latin text.

There is no difference in styling for the two examples above – we simple removed the spaces from the pinyin on the left.

The default alignment in browsers

The CSS specification contains a default style sheet for browsers, which says that if your content is Chinese – more specifically, if you have set the lang attribute to something beginning with zh – then the default behaviour will be that of ruby-align:center. Otherwise, the default behaviour is the same as ruby-align:space-around.

Note that the latter behaviour is the default if you label your Chinese content as cmn or yue. Only the generic zh... label produces the automatic centering.

Handling gaps in annotations

You may want to attach ruby annotations to a word such as 振り仮名 (furigana) without adding the redundant り.

Horizontal ruby for 振り仮名(ふりがな)Vertical ruby for 振り仮名(ふりがな)

One way to achieve this would be to simply leave a blank rt element, like this:


This would pose a problem, however, if you wanted to display the annotation inline: instead of 振り仮名(ふりがな) you would see 振り仮名(ふがな). It can also present problems if you want to merge ruby annotations across a compound noun (something which may be introduced later).

The CSS spec proposes a solution for this. If the base text and corresponding annotation are identical, browsers should not show the annotation unless it is part of an inline sequence. So you could use the following code to produce this effect, without any special styling, if the browser supports it.



To do