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.
This section looks at how you make the ruby text appear above, below or to the side of the ruby base text.
By 'over' we mean above horizontal text and to the right of vertical text.
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:
To position the ruby text below horizontal base text or to the left of vertical text, use
By 'under' we mean below horizontal text and to the left of vertical CJK text.
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:
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.
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
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.
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.
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:
If you use nested markup, and you have markup such as the following:
you could use CSS like this:
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.
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).
The rest of this section looks at how you can use CSS if your content doesn't have
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.
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
after pseudo-elements to surround it with parentheses.
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.
Producing inline annotations in this case is a little more complicated if you're not using
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
It's best to use this property on the
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.
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.)
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
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 (
Note, however, that the justification outcome differs according to what scripts are involved.
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
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
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-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 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
Note that the latter behaviour is the default if you label your Chinese content as
yue. Only the generic
zh... label produces the automatic centering.
You may want to attach ruby annotations to a word such as 振り仮名 (furigana) without adding the redundant り.
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.