Sub topic 14
Logical properties for margin and padding
This chapter assumes you know how to write CSS rules, use px, rem, and % units, and understand the four box layers (content, padding, border, margin). It also assumes you know how box-sizing affects width.
Physical and logical directions
Properties such as margin-left and padding-top are physical. They point at a fixed side of the screen. That works for English, which flows left to right and top to bottom. It breaks down for Arabic or Hebrew, which flow right to left, and for vertical scripts such as Japanese in vertical mode.
Logical properties describe sides relative to the writing mode. Two axes replace the four sides:
- The inline axis is the direction text runs within a line.
- The block axis is the direction lines stack.
In a left-to-right horizontal writing mode, inline is horizontal and block is vertical. In a right-to-left mode, the inline start moves to the right side. In a vertical writing mode, the axes swap.
The property names
Each physical property has a logical counterpart. The table shows the common ones for left-to-right horizontal text.
| Physical | Logical | Meaning |
|---|---|---|
| `margin-left` | `margin-inline-start` | Margin at the start of a line |
| `margin-right` | `margin-inline-end` | Margin at the end of a line |
| `margin-top` | `margin-block-start` | Margin before the first line |
| `margin-bottom` | `margin-block-end` | Margin after the last line |
| `padding-left` and `padding-right` | `padding-inline` | Padding on both inline sides |
| `padding-top` and `padding-bottom` | `padding-block` | Padding on both block sides |
| `width` | `inline-size` | Size along the inline axis |
| `max-width` | `max-inline-size` | Maximum size along the inline axis |
The shorthands margin-inline, margin-block, padding-inline, and padding-block take one or two values. With two values, the first is the start side and the second is the end side.
A worked example
This rule centres an article and limits its line length:
.article {
max-inline-size: 40rem;
margin-inline: auto;
padding-block: 1rem;
}max-inline-size caps the width at 40rem in horizontal writing modes. margin-inline: auto centres the box along the inline axis. padding-block: 1rem adds 1rem above and below the content.
The next example sets different values on each inline side:
.note {
margin-inline: 0 2rem;
padding-inline: 1rem 0.5rem;
border-inline-start: 4px solid #36c;
}The first value applies to the inline start and the second to the inline end. In a left-to-right page, the accent border sits on the left. If you set dir="rtl" on a parent element, the border, margin, and padding all flip to the right side. You write the CSS once.
.note-demo {
writing-mode: vertical-rl;
padding-block: 1rem;
}Here the writing mode is vertical, so the block axis runs horizontally. padding-block then adds space to the left and right of the content, not the top and bottom.
Common errors
- Mixing both families on one element. If you set
margin-left: 1remandmargin-inline-start: 2rem, both target the same side in left-to-right text. The one that appears later in the stylesheet wins, so the result depends on rule order. - Assuming logical means "horizontal" and "vertical".
blockis only vertical in horizontal writing modes. Checkwriting-modebefore you reason about direction. - Forgetting that margin collapsing follows the block axis. Adjoining margins in the block direction collapse. Margins in the inline direction never do.
- Switching everything blindly. Logical properties help when text direction can change. A fixed physical offset, such as a shadow tied to a light source, may still suit physical properties.
Practice
Exercise
A page uses dir="rtl". A rule sets padding-inline: 2rem 0.5rem;. Which side gets 2rem of padding?
Show answer
The right side. The first value is the inline start, and in a right-to-left page the inline start is on the right. The left side gets 0.5rem.
Exercise
Rewrite margin-top: 1rem; margin-bottom: 1rem; margin-left: auto; margin-right: auto; using logical shorthands.
Show answer
margin-block: 1rem; margin-inline: auto;