Sub topic 5
inline-size and block-size
This chapter assumes you know width, height and the box model. You should also know that writing-mode and dir change how text flows, which earlier chapters covered. Here you will replace width and height with their flow-relative equivalents.
What the two properties mean
inline-size is the size of an element along the inline axis, the direction text runs within a line. block-size is the size along the block axis, the direction lines stack. Which physical dimension each one controls depends on writing-mode.
| writing-mode | `inline-size` sets | `block-size` sets |
|---|---|---|
| `horizontal-tb` (default) | width | height |
| `vertical-rl` | height | width |
| `vertical-lr` | height | width |
The dir attribute does not change this table. Right-to-left text reverses the inline direction but not the axis, so inline-size is still the width in Arabic or Hebrew.
Seeing the swap
In this rule, the numbers stay the same but the shape of the box changes with the writing mode.
.vertical {
writing-mode: vertical-rl;
inline-size: 20rem;
block-size: 10rem;
}The inline axis now runs top to bottom, so the element is 20rem tall and 10rem wide. With width: 20rem; height: 10rem it would be wide and short, and the text would overflow it.
The same idea works in the default horizontal mode. The card from the earlier example uses it:
.card {
inline-size: min(100%, 40ch);
margin-inline: auto;
}In English this is a centered box at most 40 characters wide. If a parent switches to a vertical mode, the card limits its height instead.
Minimums and maximums
Each sizing property has flow-relative versions of its limits:
min-inline-sizeandmax-inline-sizereplacemin-widthandmax-width.min-block-sizeandmax-block-sizereplacemin-heightandmax-height.
Keywords such as auto, fit-content and min-content work the same way. Percentages resolve against the containing block's size on the same axis. For example, inline-size: 50% is half of the parent's inline size.
Common errors
- Mixing both families on one element.
widthandinline-sizecan map to the same physical property. The declaration later in the cascade wins, and that is hard to notice when you edit one line. Use one family per element. - Assuming
inline-sizeis always the width. It is only the width in horizontal writing modes. If you want a width whatever the text direction, usewidth. That is the physical case covered in a later chapter. - Converting only some properties. An element with
inline-sizeandheightcan still break in a vertical mode, because one dimension follows the flow and the other does not. Convert sizes, margins and padding together. - Forgetting that writing mode is inherited. A child with
inline-size: 100%inside a vertical parent fills the parent's height. This is correct behavior, but it can look wrong if you did not expect the mode to change.
Exercise
An element has writing-mode: vertical-rl; inline-size: 300px; block-size: 120px;. What are its rendered width and height? Does adding dir="rtl" to it change the answer?
Show answer
Its width is 120px and its height is 300px, because in a vertical mode the inline axis is vertical, so inline-size is the height and block-size is the width. Adding dir="rtl" does not change the sizes. It only reverses the inline direction, so lines would start at the bottom instead of the top.