Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 139 ยท Text and Fonts

Writing Modes and Logical Properties

Learn how writing-mode and direction change the flow of text, and use logical properties like margin-inline and padding-block to build layouts that work in any language.

English runs left to right, top to bottom. Arabic and Hebrew run right to left. Traditional Chinese, Japanese and Mongolian can run in vertical columns. CSS supports all of these, and the tools that make it work also make your everyday stylesheets more robust.

This lesson covers writing-mode for vertical text, the direction of a line, and the big idea behind them: logical properties. Instead of saying "left" and "right", you say "start" and "end" of a line, and your layout adapts to whatever language and direction it is in.

Block and inline: the two axes

Every piece of text flows along two axes:

  • The inline axis is the direction a line of text runs. In English it's horizontal, left to right.
  • The block axis is the direction lines (and paragraphs) stack. In English it's vertical, top to bottom.

Each axis has a start and an end. In English, inline-start is the left, inline-end the right, block-start the top and block-end the bottom. In Arabic, inline-start becomes the right. In vertical Japanese, the inline axis runs top to bottom and blocks stack from right to left.

Logical properties are named after these axes, so they always point the right way.

writing-mode

writing-mode sets the block direction, and with it, whether lines are horizontal or vertical:

  • horizontal-tb (default): horizontal lines, stacking top to bottom.
  • vertical-rl: vertical lines, stacking right to left. Used for Chinese, Japanese and Korean vertical text.
  • vertical-lr: vertical lines, stacking left to right. Used for Mongolian.
  • sideways-rl and sideways-lr: like the vertical modes, but every glyph is rotated as a whole, as if horizontal text were turned on its side. These are newer; support arrived in Firefox first and recent versions of other browsers. Check before relying on them.
Writing modes

Look at the green underline on each label. It's set with border-block-end, the end of the block axis. In the horizontal box it's at the bottom; in the vertical boxes it moves to the side where the next line would go. You wrote one rule, and it followed the flow of the text.

Vertical text in real designs

You don't need to write in Japanese to use vertical modes. They're handy for:

  • Spine-style labels on bookshelves or tabs.
  • Narrow table headers that would otherwise force wide columns.
  • Decorative headings running up the side of a section.

For Latin text, writing-mode: vertical-rl reads top to bottom. To read bottom to top, which is common for spine labels, add transform: rotate(180deg), or use sideways-lr where supported.

For mixed content like numbers in vertical Japanese, text-orientation: upright stands each Latin letter upright, and text-combine-upright: all squeezes a short run like "12" into a single upright character cell.

direction and dir

The direction property sets the inline direction: ltr (left to right) or rtl (right to left). However, you should rarely set it in CSS. Text direction is part of the content's meaning, so it belongs in HTML, with the dir attribute:

HTML
<html lang="ar" dir="rtl">

The browser uses dir to set direction, and everything else follows: text aligns to the right, lists put bullets on the right, and the order of flex and grid items flips. If you used direction in CSS and the stylesheet failed to load, the text would show in the wrong direction. You can learn more about dir in Global Attributes.

To style based on direction, use the :dir() pseudo-class, which matches the element's actual resolved direction:

CSS
.icon-arrow:dir(rtl) {
  transform: scaleX(-1); /* flip the arrow icon in right-to-left pages */
}

Logical properties

Physical properties are named after the screen: margin-left, padding-top, border-right, left. Logical properties are named after the flow of the text. Here are the main mappings for horizontal, left-to-right text:

Physical Logical
width / height inline-size / block-size
margin-top / margin-bottom margin-block-start / margin-block-end
margin-left / margin-right margin-inline-start / margin-inline-end
padding-top + padding-bottom padding-block
padding-left + padding-right padding-inline
border-left border-inline-start
top / left inset-block-start / inset-inline-start
border-top-left-radius border-start-start-radius
text-align: left text-align: start

The two-value shorthands are especially pleasant. margin-inline: auto centers a block horizontally, padding-block: 1rem sets top and bottom padding, and inset-inline: 0 stretches a positioned element from side to side.

CSS
.card {
  max-inline-size: 40rem;
  margin-inline: auto;
  padding-block: 1.5rem;
  padding-inline: 2rem;
}

.badge {
  position: absolute;
  inset-block-start: 8px;
  inset-inline-end: 8px;
}

Logical properties are supported in every current browser. min-inline-size, max-block-size and the rest follow the same pattern.

One stylesheet, two directions

The second note uses the same CSS. The accent border, the rounded corners, the extra padding for the close button and the close button itself all move to the mirrored side, just because of dir="rtl". With border-left, padding-right and right: 8px, you'd need a second set of rules for right-to-left pages.

Logical properties in vertical writing modes

Logical properties also follow writing-mode. In a vertical-rl box, inline-size is the height and block-size is the width, because lines now run downward. That makes components rotate correctly when placed in vertical text.

Logical sizes follow the writing mode

The console shows that the two labels swap their width and height, while their CSS is identical apart from writing-mode.

Should you always use logical properties?

For any site that is, or might be, translated into a right-to-left language, logical properties save a huge amount of work. Even for English-only sites, many teams now prefer them by default:

  • margin-inline: auto and padding-block are short and expressive.
  • Components stay correct if they're ever placed in a vertical or RTL context.
  • Spacing written as "space after this block" (margin-block-end) describes intent better than "space at the bottom".

Physical properties are still right when something is tied to the screen rather than the text: a shadow that always falls downward, a fixed toolbar at the bottom of the viewport, or a background image positioned on the left of a photo. Use whichever matches what you mean.

Flexbox and Grid are already logical: justify-content: flex-start and grid column 1 begin at the inline start, so layouts mirror automatically in RTL. Combine them with logical spacing properties and most components need no RTL-specific CSS at all.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a left-to-right, horizontal page, which side does margin-inline-start set?

    Inline-start is where a line begins. In horizontal left-to-right text, that's the left.
  2. 2. Where should you set the right-to-left direction of an Arabic page?

    Direction is part of the content's meaning, so it belongs in the dir attribute. CSS direction would be lost if the stylesheet failed to load.
  3. 3. What does writing-mode: vertical-rl do?

    In vertical-rl, the inline axis is vertical and the block axis runs right to left.
  4. 4. In a vertical-rl element, what does inline-size control?

    Inline size is the length along a line. In vertical writing modes, lines run top to bottom, so inline size is the height.
  5. 5. Which declaration centers a block horizontally in a horizontal writing mode?

    margin-inline: auto sets both inline margins to auto, which centers a block that has a limited width. Padding can't be auto.

Summary

  • Text flows along an inline axis (the direction of a line) and a block axis (the direction lines stack), each with a start and an end.
  • writing-mode switches between horizontal and vertical text; vertical-rl and vertical-lr set which way lines stack.
  • Set text direction in HTML with dir; use :dir() in CSS to react to it.
  • Logical properties (margin-inline, padding-block, inset-inline-start, inline-size) follow the writing mode and direction.
  • One logical stylesheet mirrors correctly for right-to-left languages and rotates for vertical text.
  • Keep physical properties for things tied to the screen rather than the text.

Next: Backgrounds, the first lesson of the Backgrounds and Effects chapter.