Sub topic 5
inline-size and block-size
This chapter covers the logical sizing properties: inline-size, block-size, and their min- and max- forms. It assumes you know the box model, how width, height, min-width and max-width behave, and what writing-mode and direction do. The previous chapters introduced the block and inline axes. This one applies them to sizing.
Why sizing needs logical names
width always means the horizontal extent of the box. height always means the vertical extent. That is fine while text runs horizontally. In a vertical writing mode, a paragraph's lines stack side by side, and the length of a line is its vertical extent. A rule such as width: 40ch then limits the wrong dimension. It caps the number of lines you can fit, not the line length.
Logical sizing names dimensions by their role in the text flow:
inline-sizeis the extent along the inline axis, the direction in which a line of text runs.block-sizeis the extent along the block axis, the direction in which new lines and blocks are stacked.
How the mapping works
The writing-mode property decides which physical dimension each logical one maps to. The direction property (ltr or rtl) does not matter for sizing, because it only flips the start and end sides along the inline axis. It does not change which axis is inline.
| writing-mode | inline-size maps to | block-size maps to |
|---|---|---|
| horizontal-tb | width | height |
| vertical-rl | height | width |
| vertical-lr | height | width |
Sideways modes such as sideways-rl map the same way as the vertical modes.
The property family
Each physical sizing property has a logical counterpart.
| Physical | Logical, horizontal text |
|---|---|
| `width` | `inline-size` |
| `height` | `block-size` |
| `min-width` | `min-inline-size` |
| `max-width` | `max-inline-size` |
| `min-height` | `min-block-size` |
| `max-height` | `max-block-size` |
The logical and physical properties are two names for the same underlying values. If you set width: 10rem and inline-size: 20rem on the same element in horizontal text, the later declaration in the cascade wins, as with any two properties that share a computed value. They do not combine or add together.
A first example
The .card rule from the plan combines logical sizing with logical spacing.
.card {
inline-size: min(100%, 40ch);
margin-inline: auto;
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
border-inline-start: 4px solid steelblue;
}inline-size: min(100%, 40ch) means the card is as wide as its container, but never wider than about 40 characters of its font. In a horizontal mode that is the width. In a vertical mode it is the height, so the card still limits its line length. margin-inline: auto centers the box along the inline axis, which is what margin: 0 auto does in horizontal text.
Seeing the swap in a vertical mode
Here is a box in a vertical writing mode.
.vertical {
writing-mode: vertical-rl;
inline-size: 20rem;
block-size: 10rem;
}The element ends up 10rem wide and 20rem tall. The same two declarations in a horizontal element would give a box 20rem wide and 10rem tall. Nothing in the rule changed except writing-mode, and the box rotated with the text. This is the central benefit: the proportions you describe stay tied to the text, not the screen.
Here is a complete page you can open in a browser to compare both cases:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>inline-size demo</title>
<style>
.box {
inline-size: 20rem;
block-size: 10rem;
background: lightsteelblue;
margin-block-end: 1rem;
}
.vertical {
writing-mode: vertical-rl;
}
</style>
</head>
<body>
<div class="box">Horizontal: 20rem wide, 10rem tall</div>
<div class="box vertical">Vertical: 10rem wide, 20rem tall</div>
</body>
</html>Percentages and auto
Percentage values on inline-size resolve against the containing block's inline size. Percentages on block-size resolve against the containing block's block size. In a vertical context, inline-size: 50% is therefore half of the parent's height. This follows the same rules as width and height, with the axes swapped.
Two behaviors carry over from the physical properties and cause most surprises:
- A block-level box with
inline-size: autofills the available inline space. Withblock-size: autoit grows to fit its content. This is true even when the writing mode is vertical. - A percentage
block-sizeonly works when the containing block has a definite block size. If the parent's block size depends on its content, the percentage behaves likeauto.
Mixing writing modes
The mapping depends on the writing mode of the element whose size is being set, with a subtle point for percentages: the containing block's own writing mode determines the axes it uses. When a vertical element sits inside a horizontal parent, the child's inline-size is its height, but a percentage resolves against the parent's size in the same physical axis.
This is a common source of confusion with embedded vertical text, such as a Japanese quotation inside an English page. Keep the vertical element's sizing explicit, and give it a parent with a known size when you use percentages.
.quote-wrapper {
inline-size: 30rem;
block-size: 12rem;
}
.quote-wrapper .vertical-quote {
writing-mode: vertical-rl;
inline-size: 100%;
block-size: 100%;
}Here the child's physical height comes from the wrapper's height, even though the child calls that dimension inline-size. Check the result in the browser's developer tools rather than reasoning about it only on paper.
Common errors
- Mixing logical and physical sizes without a plan. Setting
widthin one rule andblock-sizein another makes the cascade hard to follow. In horizontal text,widthandinline-sizecollide. Use one family per component. - Assuming
inline-sizemeans width. It does only in horizontal modes. Do not use it for a value that is truly horizontal, such as a sprite sheet that is always drawn left to right. - Using
chandexunits and expecting them to track the axis. Units such aschandremare lengths, not directions.40chis the same length whether it appears inwidthorinline-size. The advantage ofinline-size: 40chis that the measure applies to the text line direction. - Forgetting the replaced-element case. Images and videos have an intrinsic aspect ratio. Setting
inline-size: 100%and leavingblock-size: autokeeps the ratio, aswidth: 100%; height: autodoes. - Expecting
directionto change sizes. Switchingdirection: rtldoes not alterinline-sizeorblock-size. It changes the start and end sides only.
Logical sizes and the aspect-ratio property
The aspect-ratio property is defined in terms of the box's width and height, written width / height. It stays physical. In a vertical writing mode, a box with aspect-ratio: 16 / 9 is still wider than it is tall, even though its inline axis now runs vertically. If you set inline-size on such a box, the browser derives the physical height from it, then computes the width. Keep this in mind when you combine ratios with vertical text.
Choosing between the two
Use logical sizing when the dimension relates to the text: line length, the height of a text container that grows as lines stack, or a card whose proportions should follow its content. Use physical sizing when the dimension relates to the screen or to an asset, such as a full-viewport hero, an icon drawn at a fixed pixel size, or a canvas. A later chapter covers these cases in more detail.
Exercise
An element has writing-mode: vertical-rl; inline-size: 30rem; block-size: 8rem;. What are its physical width and height?
Show answer
Its width is 8rem and its height is 30rem. In a vertical writing mode the inline axis is vertical, so inline-size sets the height, and block-size sets the width.
Exercise
You set direction: rtl on a horizontal block that has inline-size: 50%. Does the physical size of the block change compared to direction: ltr? Explain.
Show answer
No. Direction only decides which side of the inline axis is the start and which is the end. The inline axis is still horizontal, so 50% of the container's inline size is the same width in both cases. Only the side where the box is placed or aligned can change.
Exercise
Rewrite max-width: 60ch; min-height: 10rem; as logical properties for a component that must keep a readable line length in any writing mode.
Show answer
max-inline-size: 60ch; min-block-size: 10rem;