CSS ruby-align Property

The CSS ruby-align property controls how ruby annotation text is aligned or distributed within the space available for its associated base text.

Ruby annotations are small text labels commonly used to provide pronunciation or explanatory information for East Asian characters. The ruby-align property determines how the annotation and base text are aligned when their widths differ.

ruby-align Syntax

The ruby-align property accepts keywords that control the alignment or distribution of ruby content.

Basic Syntax

selector {
  ruby-align: space-around;
}

Available Values

ruby {
  ruby-align: space-around;
}
ruby {
  ruby-align: space-between;
}
ruby {
  ruby-align: start;
}
ruby {
  ruby-align: center;
}

CSS-Wide Keywords

ruby {
  ruby-align: initial;
}
ruby {
  ruby-align: inherit;
}
ruby {
  ruby-align: unset;
}
ruby {
  ruby-align: revert;
}
ruby {
  ruby-align: revert-layer;
}

The ruby-align property does not accept lengths, percentages, or multiple alignment keywords.

Property Values

Value Description
space-around Distributes extra space around the ruby content, with half-sized spacing at the start and end edges. This is the initial value.
space-between Distributes extra space between the characters or typographic units, without adding space at the outer edges.
start Aligns the ruby content with the start edge of the available space.
center Centers the ruby content within the available space.
initial Resets the property to space-around.
inherit Uses the computed value from the parent element.
unset Inherits the value from the parent because the property is inherited.
revert Reverts the property according to CSS cascade origin rules.
revert-layer Reverts the property according to CSS cascade layer rules.

Property Characteristics

Characteristic Value
Initial value space-around
Inherited Yes
Applies to Ruby bases and ruby annotations
Computed value As specified
Animation type Discrete
Percentages Not applicable

Valid Values

ruby {
  ruby-align: center;
}
ruby {
  ruby-align: start;
}
ruby {
  ruby-align: space-between;
}
ruby {
  ruby-align: space-around;
}

Invalid Values

ruby {
  ruby-align: left;
}
ruby {
  ruby-align: right;
}
ruby {
  ruby-align: justify;
}
ruby {
  ruby-align: 20px;
}
ruby {
  ruby-align: center start;
}

The property accepts one of its defined alignment keywords or a CSS-wide keyword. General text alignment values such as left, right, and justify are not valid.

How It Works

Ruby layout associates annotation text with a base character or sequence of characters. The base and annotation can have different widths, creating extra space within the ruby segment.

The ruby-align property determines how content is positioned or distributed within that extra space.

Basic Ruby Example

<ruby>
  漢字
  <rt>かんじ</rt>
</ruby>

The Japanese characters are the base text, while the smaller hiragana characters provide their pronunciation.

Applying Alignment

ruby {
  ruby-align: center;
}

The browser centers the ruby content within its available alignment space.

Available Space

When the base text is wider than its annotation, the annotation may have extra horizontal space. When the annotation is wider, the base text may have extra space instead.

Writing Mode

Ruby alignment follows the inline direction of the text. In vertical writing modes, the relevant alignment direction follows the vertical inline axis.

Important Distinction

The ruby-align property controls alignment within ruby layout. It does not determine whether annotations appear above, below, or beside the base text. That behavior is controlled by ruby-position.

Understanding Ruby Markup

HTML provides dedicated elements for representing ruby annotations.

Element Purpose
<ruby> Creates a ruby annotation structure containing base text and associated annotations.
<rt> Contains the ruby annotation text.
<rp> Provides optional fallback parentheses for browsers that do not support ruby annotations.

Basic Ruby Markup

<ruby>
  日本語
  <rt>にほんご</rt>
</ruby>

The annotation provides the pronunciation of the Japanese word.

Ruby With Fallback Parentheses

<ruby>
  漢字
  <rp>(</rp>
  <rt>かんじ</rt>
  <rp>)</rp>
</ruby>

Browsers that support ruby layout display the annotation according to ruby formatting rules. Browsers without ruby support can display the fallback parentheses around the annotation.

Multiple Ruby Segments

<ruby>東<rt>とう</rt></ruby>
<ruby>京<rt>きょう</rt></ruby>

Each ruby element associates a base character with its corresponding pronunciation.

Important Consideration

Use semantic ruby markup rather than manually positioning small text with absolute positioning. Native ruby layout preserves the association between base text and annotation.

space-around Value

The space-around value distributes extra space around the content within a ruby base or annotation.

CSS Example

ruby {
  ruby-align: space-around;
}

How Spacing Is Distributed

Extra space is distributed around the characters or typographic units, with half as much space at each outer edge as between adjacent units.

Default Behavior

space-around is the initial value of the ruby-align property.

When to Use space-around

Use this value when you want balanced distribution of ruby text across the available alignment space.

Important Consideration

The visible result depends on the relative widths of the ruby base and annotation. When little or no extra space is available, the difference between alignment values may be difficult to see.

space-between Value

The space-between value distributes extra space between the characters or typographic units without adding spacing at the outer edges.

CSS Example

ruby {
  ruby-align: space-between;
}

How Spacing Is Distributed

The first and last units align with the available start and end edges, while the remaining space is distributed between them.

Single-Unit Content

When there is only one typographic unit, there are no internal spaces to distribute. The content is aligned at the start edge.

When to Use space-between

Use this value when the ruby content should extend across the available space without additional spacing at its outer edges.

Important Consideration

Distributing extra space between characters can change the appearance of pronunciation text. Check readability when using this value with longer annotations.

start Value

The start value aligns the ruby content with the start edge of its available alignment space.

CSS Example

ruby {
  ruby-align: start;
}

Horizontal Writing Mode

In a left-to-right horizontal writing mode, the start edge is normally the left edge. In a right-to-left horizontal writing mode, it is normally the right edge.

Vertical Writing Mode

In vertical writing, the start edge follows the inline progression direction of the text.

When to Use start

Use this value when the beginning of the annotation should align with the beginning of the available ruby space.

Important Consideration

The start value is a logical alignment keyword. It should not be confused with a fixed physical left alignment.

center Value

The center value centers ruby content within the available alignment space.

CSS Example

ruby {
  ruby-align: center;
}

How Centering Works

When the ruby annotation is narrower than its base, the remaining space is distributed equally on both sides of the annotation.

When to Use center

Use this value when you want the annotation to remain visually centered without distributing extra spacing between its characters.

Center Versus space-around

center preserves the normal spacing between characters and places the extra space around the content. space-around distributes extra space between and around the typographic units.

Important Consideration

Centering can provide a compact appearance for pronunciation annotations, particularly when the annotation is shorter than its base text.

Comparing Alignment Values

The four values produce different distributions of available space within ruby layout.

Value Alignment Behavior Extra Space
space-around Distributes content across the available space. Placed between and around typographic units.
space-between Distributes content between the start and end edges. Placed between typographic units.
start Aligns content with the logical start edge. Remains at the end side.
center Centers content within the available space. Distributed equally on both sides.

Comparing CSS Declarations

.ruby-around {
  ruby-align: space-around;
}
.ruby-between {
  ruby-align: space-between;
}
.ruby-start {
  ruby-align: start;
}
.ruby-center {
  ruby-align: center;
}

Choosing an Alignment

Use space-around for balanced distribution, space-between for spacing between characters, start for logical start alignment, or center for compact centered content.

Important Consideration

The visual differences are most noticeable when the ruby base and annotation have significantly different widths. Browser support for individual alignment values can also affect the result.

ruby-align Example

The following example compares the four ruby-align values using Japanese ruby annotations.

HTML Example

<div class="ruby-align-demo" lang="ja">
  <div class="ruby-example">
    <strong>space-around</strong>
    <ruby class="ruby-around">日本語<rt>にほんご</rt></ruby>
  </div>
  <div class="ruby-example">
    <strong>space-between</strong>
    <ruby class="ruby-between">日本語<rt>にほんご</rt></ruby>
  </div>
  <div class="ruby-example">
    <strong>start</strong>
    <ruby class="ruby-start">日本語<rt>にほんご</rt></ruby>
  </div>
  <div class="ruby-example">
    <strong>center</strong>
    <ruby class="ruby-center">日本語<rt>にほんご</rt></ruby>
  </div>
</div>

CSS Example

.ruby-align-demo {
  display: grid;
  gap: 15px;
}
.ruby-align-demo .ruby-example {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 20px;
  padding: 20px;
  border: 1px solid #cbd5e1;
  background-color: #f8fafc;
}
.ruby-align-demo .ruby-example strong {
  min-width: 110px;
  font-size: 14px;
}
.ruby-align-demo ruby {
  font-size: 28px;
}
.ruby-align-demo rt {
  font-size: 12px;
}
.ruby-align-demo .ruby-around {
  ruby-align: space-around;
}
.ruby-align-demo .ruby-between {
  ruby-align: space-between;
}
.ruby-align-demo .ruby-start {
  ruby-align: start;
}
.ruby-align-demo .ruby-center {
  ruby-align: center;
}
Browser Result
space-around 日本語にほんご
space-between 日本語にほんご
start 日本語にほんご
center 日本語にほんご

Each example applies a different alignment value to the same ruby text. The visible differences depend on the relative widths of the base and annotation, the selected font, and browser support.

Changing the Alignment

.ruby-align-demo ruby {
  ruby-align: center;
}

This provides a shared centered alignment when no more specific rule overrides it.

Using a Shared Default

ruby {
  ruby-align: space-around;
}

This applies the default distribution style to ruby content throughout the document unless another rule overrides it.

Browser Compatibility Note

Some browsers may ignore unsupported ruby-align values and use their built-in ruby alignment behavior. The annotations remain associated with their base text even when the requested alignment is unavailable.

Accessibility Considerations

Ruby annotations provide useful pronunciation and explanatory information, particularly for readers of languages that use complex writing systems.

Use Semantic Ruby Elements

Use <ruby> and <rt> to preserve the relationship between base text and its annotation.

Provide Language Information

Use the lang attribute to identify the language of the content and help browsers and assistive technologies interpret the text correctly.

Maintain Readable Annotation Text

Avoid making annotations so small that they become difficult to read, particularly at reduced viewport sizes.

Preserve Text Relationships

Do not use manually positioned text as a replacement for semantic ruby markup when the text is intended to annotate a base character or phrase.

Support Zoom and Reflow

Test ruby annotations at increased zoom levels to ensure they remain legible and do not overlap surrounding content.

Consider Assistive Technology Differences

Screen readers and other assistive technologies may handle ruby annotations differently. Test important pronunciation content with the technologies used by your audience.

Important Consideration

The ruby-align property changes visual alignment only. The semantic relationship between ruby base text and annotation is established by the HTML markup.

Common Mistakes

Using ruby-align on Ordinary Text

p {
  ruby-align: center;
}

The property does not center ordinary paragraph text. Use text-align for normal text alignment.

Using Unsupported Alignment Keywords

ruby {
  ruby-align: left;
}

Use supported values such as start or center.

Confusing ruby-align With ruby-position

The ruby-align property controls alignment within the available ruby space. The ruby-position property controls which side of the base text contains the annotation.

Using Length Values

ruby {
  ruby-align: 10px;
}

The property accepts alignment keywords, not lengths or percentages.

Expecting Identical Results in Every Browser

Browser support for the individual alignment values varies. Unsupported declarations may be ignored.

Using Nonsemantic Annotation Markup

Manually positioning small text with spans or absolute positioning can lose the semantic relationship between base text and pronunciation.

Ignoring Writing Direction

The start value follows the logical inline direction rather than always referring to the physical left side.

Using Text That Is Too Small

Ruby annotations should remain readable at normal and increased zoom levels.

Important Considerations

Keep the following points in mind when using ruby-align:

  • It controls the alignment and distribution of ruby base and annotation content.
  • Its initial value is space-around.
  • The property is inherited.
  • It applies to ruby bases and ruby annotations.
  • space-around distributes extra space between and around typographic units.
  • space-between distributes extra space between typographic units.
  • start aligns content with the logical start edge.
  • center centers content within the available space.
  • The property does not accept lengths or percentages.
  • It does not accept ordinary text alignment keywords such as left or right.
  • Its visible effect depends on the relative widths of the base and annotation.
  • Differences between values may be subtle when little extra space is available.
  • Alignment follows the inline direction of the writing mode.
  • The start value is a logical alignment keyword.
  • The ruby-position property controls annotation placement.
  • The <ruby> element establishes ruby markup.
  • The <rt> element provides annotation text.
  • The <rp> element can provide fallback parentheses.
  • Ruby annotations are commonly used for pronunciation guidance.
  • Semantic ruby markup preserves the association between base text and annotation.
  • Language attributes help identify the content's language.
  • Browser support for individual alignment values may vary.
  • Unsupported values may be ignored by the browser.
  • Ruby alignment does not change the semantic meaning of the text.
  • Annotation text should remain readable at increased zoom levels.
  • Test ruby layouts in the browsers used by your audience.

Browser Support

The CSS ruby-align property has Limited availability and is not supported consistently across all major browsers.

Support for the individual values and their alignment behavior varies. Some browsers may render ruby annotations using their default alignment rules even when a ruby-align declaration is present.

For current compatibility information, visit Can I Use? and search for ruby-align.

Use semantic HTML ruby markup so annotations remain meaningful when the requested CSS alignment is unsupported.

Specifications

The ruby-align property is defined in CSS Ruby Annotation Layout Module Level 1.

CSS Ruby Annotation Layout Module Level 1

The specification defines how ruby base text and annotations are aligned or distributed within their available inline space.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: ruby-align