← Back to HTML & CSS, page 4

html-css-0079 · Medium mid

How does writing-mode change layout, and when would you use vertical text?

writing-mode sets whether lines run horizontally or vertically and which way blocks stack. It changes the inline and block axes, so logical properties follow it. Use it for vertical scripts and side labels, and prefer it over transform: rotate() when layout must adapt.

Full answer

writing-mode sets the direction in which lines of text are laid out and the direction in which blocks stack. The default is horizontal-tb: text runs left to right, and blocks stack top to bottom. With vertical-rl text runs top to bottom and new lines move toward the left. With vertical-lr they move toward the right.

This changes the meaning of "inline" and "block". The inline axis is the direction of the text. The block axis is where the next line goes. This is why logical properties such as inline-size and margin-inline follow the writing mode, and width and margin-left do not.

Uses:

  • Languages such as Japanese and Chinese are traditionally written vertically in some designs. Mongolian is vertical by script.
  • A short label along the side of a card or table header, such as "Sponsored" rotated to read upward.
  • Layout effects such as a vertical heading on the side of an article.

text-orientation: mixed is the default and keeps Latin letters sideways in vertical text. text-orientation: upright makes each letter stand up, which suits short strings. Do not use it for long English text, because it is hard to read.

Do not use transform: rotate() for a vertical label if you want layout to adapt: a rotated box keeps its original layout size. writing-mode changes the real size and the flow.

The first label reads top to bottom, and its box is tall and narrow, about one line wide. The row uses align-items: flex-start so the boxes keep their own size. The second shows each letter upright, one under the other. In the third, the rotated text looks vertical, but the grey parent box still has the width of the horizontal text, because the transform does not change layout.

Check the line direction with your content. Also test with real text from the target language when it matters.

html
<style>
  .row { display: flex; align-items: flex-start; gap: 16px; font: 16px/1.4 sans-serif; }
  .label { writing-mode: vertical-rl; background: #cde; padding: 4px; }
  .upright { writing-mode: vertical-rl; text-orientation: upright; background: #fdc; padding: 4px; }
  .rotated { transform: rotate(90deg); background: #dfc; display: inline-block; }
  .box { background: #eee; }
</style>
<div class="row">
  <span class="label" id="label">Sponsored</span>
  <span class="upright" id="up">ABC</span>
  <span class="box"><span class="rotated" id="rot">Sponsored</span></span>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • horizontal-tb is the default; vertical-rl and vertical-lr stack lines sideways.
  • writing-mode redefines the inline and block axes.
  • text-orientation: upright stands each letter up.
  • transform: rotate() does not change layout size.
  • Use logical properties to keep layouts working in any mode.

Common mistakes

  • Rotating text with transform and then fighting overlap, because layout still uses the unrotated size.
  • Using text-orientation: upright for long Latin text.
  • Mixing physical properties like width and margin-left in a vertical layout instead of logical ones.

Follow-ups

  • What is the difference between vertical-rl and vertical-lr?
  • Why does a rotated element still take its old space?

Tips

  • State that writing-mode redefines the inline and block axes.
  • Say why logical properties exist, and tie it back to writing-mode.