Sub topic 6
Logical margin and padding
Prerequisites and scope
This chapter assumes you know the box model, the margin and padding properties, and the lang and dir attributes. It also assumes you have read the earlier chapters on writing modes and on the block and inline axes. Here we apply that model to spacing. Borders, inset, and the shorthand syntax each get their own chapters, so they appear only briefly.
The idea: spacing follows the flow
margin-left always means the left side of the screen. In a right-to-left page, the "start" of a line is on the right, so a left margin now sits at the end of the line. Logical margin and padding avoid this by naming sides relative to the text flow:
inline-startis where a line of text begins.inline-endis where a line of text ends.block-startis where the next block begins, which is the side blocks flow from.block-endis the opposite side.
The browser resolves these names to physical sides using the element's writing-mode and direction. You write the rule once, and the mapping changes with the content.
| Writing mode and direction | inline-start | inline-end | block-start | block-end |
|---|---|---|---|---|
| `horizontal-tb`, `ltr` (English) | left | right | top | bottom |
| `horizontal-tb`, `rtl` (Arabic, Hebrew) | right | left | top | bottom |
| `vertical-rl`, `ltr` | top | bottom | right | left |
| `vertical-lr`, `ltr` | top | bottom | left | right |
The longhand properties
Each physical property has a logical counterpart.
| Physical | Logical (margin) | Logical (padding) |
|---|---|---|
| `margin-top` / `padding-top` | `margin-block-start` | `padding-block-start` |
| `margin-bottom` / `padding-bottom` | `margin-block-end` | `padding-block-end` |
| `margin-left` / `padding-left` | `margin-inline-start` | `padding-inline-start` |
| `margin-right` / `padding-right` | `margin-inline-end` | `padding-inline-end` |
The table pairs top with block-start and left with inline-start only for horizontal left-to-right text. These pairings are a starting point for translating old code, not a fixed rule. If you convert margin-left to margin-inline-start, the result matches the old rule in English and flips in Arabic. That flip is usually what you want for things like an icon gap or a list indent, but not always (see the section on keeping physical values).
The shorthands
You can set both sides of an axis in one declaration:
margin-inlinesetsmargin-inline-startandmargin-inline-end.margin-blocksetsmargin-block-startandmargin-block-end.padding-inlineandpadding-blockwork the same way.
With one value, both sides get it. With two values, the first is the start side and the second is the end side.
.card {
inline-size: min(100%, 40ch);
margin-inline: auto;
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
border-inline-start: 4px solid steelblue;
}In this card, padding-inline: 1.5rem 1rem gives 1.5rem at the start of the line and 1rem at the end. In English that is left 1.5rem and right 1rem. In Arabic it is right 1.5rem and left 1rem. The two-value form is the one that surprises people, because it does not read "left then right". It reads "start then end".
The margin-inline: auto line centers the card on the inline axis, and it works in every writing mode. Centering with margin: 0 auto also works in horizontal text, but in vertical text it would center along the wrong axis.
Seeing it flip
The same rule can serve two directions without any overrides.
<p dir="rtl" lang="ar" class="note">...</p>
<p dir="ltr" lang="en" class="note">...</p>
<style>
.note {
padding-inline-start: 2rem;
border-inline-start: 3px solid tomato;
}
</style>The Arabic paragraph gets its padding and border on the right. The English paragraph gets them on the left. With physical properties you would need a second rule, such as .note:dir(rtl) or [dir="rtl"] .note, to mirror the first. Logical properties remove that second rule.
Vertical writing modes
In vertical text the block axis runs horizontally and the inline axis runs vertically. This changes what each shorthand controls:
.vertical {
writing-mode: vertical-rl;
inline-size: 20rem;
block-size: 10rem;
padding-inline: 2rem;
margin-block: 1rem;
}Here inline-size: 20rem makes the box 20rem tall, and block-size: 10rem makes it 10rem wide. padding-inline: 2rem adds space at the top and bottom of the box, because those are the inline start and end. margin-block: 1rem adds space on the left and right.
If you wrote padding-left and padding-right here, the spacing would apply to the sides that are now the block axis. The text would still be readable, but the padding would no longer follow the line of text.
Which element's mode counts
Logical properties resolve against the writing mode and direction of the element they are set on, not its parent. Two cases cause confusion.
- Inherited direction.
directionandwriting-modeinherit, so most children match their parent. If a child sets its owndir="rtl"orwriting-mode, its logical properties follow the child's values. - Margins in a different mode. A margin is applied by the element, but the layout it affects belongs to the parent. If a vertical child sits inside a horizontal parent, the child's
margin-inline-startresolves using the child's mode, which may point to a different physical side than the parent'smargin-inline-startwould.
The second case is rare, but it explains odd results when mixing modes, such as a vertical label inside a horizontal toolbar. When the spacing should relate to the parent's flow, consider setting it on the parent (with gap or padding) instead.
Margin collapsing
Margin collapsing is defined in terms of the block axis. Adjacent margin-block-end and margin-block-start values between siblings collapse, as margin-bottom and margin-top do in horizontal text. Inline margins never collapse. In a vertical writing mode, the collapsing margins are the horizontal ones, because that is where the block axis points.
Logical longhands and physical ones that refer to the same side are the same property under the hood. They share one computed value, and the declaration that wins the cascade decides the result. This leads to the main pitfall of this chapter.
Common errors
- Mixing physical and logical on the same side. Suppose you write
margin-left: 1rem; margin-inline-start: 2rem;. In left-to-right text both target the left margin, and the later one in source order wins, subject to specificity. In right-to-left text they target different sides, so both apply. The result depends on direction, which is hard to debug. Pick one system per property group. - Reading two-value shorthands as left then right.
padding-inline: 1rem 3remis start then end. In Arabic, 1rem is on the right. - Using
margin-blockfor horizontal centering. Auto margins center along the axis you name. Usemargin-inline: autoto center an element between the left and right sides in horizontal text. - Forgetting that
autoand percentages need care. Percentage margins and padding resolve against the containing block's inline size, in any writing mode. Sopadding-block: 10%is 10% of the width of a horizontal container, and 10% of the height of a vertical one. It is not a percentage of the block size. - Assuming
margin-block-startis always the top. It is the top only when the writing mode ishorizontal-tb.
Fallbacks for older browsers
Logical margin and padding are supported in current browsers. If you must support an older one, keep the physical declaration as a fallback and override it where the logical property exists:
.box {
margin-left: 1rem;
}
@supports (margin-inline-start: 1rem) {
.box {
margin-left: 0;
margin-inline-start: 1rem;
}
}The reset to margin-left: 0 is needed. Without it, in right-to-left text a supporting browser would apply both the old left margin and the new inline-start margin on the right.
Keeping physical values
Use physical properties when the side is tied to the screen and not to the text. Examples include a drop shadow offset that mimics a light source from the top left, a sticky sidebar pinned to a specific screen edge for a design reason, or a margin that matches a fixed illustration. If the layout should mirror for right-to-left readers, use logical properties. If it should not, keep physical ones.
Exercise
A component has padding-inline: 0.5rem 2rem; and is rendered inside <div dir="rtl" lang="he">. Which physical side gets 2rem of padding? What would be the answer if the same component were inside a left-to-right horizontal-tb container?
Show answer
In the right-to-left container, the inline end is the left side, so the left padding is 2rem and the right padding is 0.5rem. In the left-to-right container, the inline end is the right side, so the right padding is 2rem and the left padding is 0.5rem.
Exercise
An element has writing-mode: vertical-rl and margin-block: 1rem 0;. Which physical side receives the 1rem margin?
Show answer
In vertical-rl, the block axis runs right to left, so block-start is the right side. The right margin is 1rem and the left margin is 0.
Exercise
You are migrating this rule: .alert { margin: 0 auto 1rem 2rem; padding: 1rem 1.5rem; }. Rewrite it with logical properties so that it mirrors in right-to-left text. State which parts, if any, you would keep as they are.
Show answer
Use .alert { margin-block: 0 1rem; margin-inline: 2rem auto; padding-block: 1rem; padding-inline: 1.5rem; }. The old margin order is top, right, bottom, left, so the left margin of 2rem becomes inline-start and the right margin of auto becomes inline-end. The block values are top 0 and bottom 1rem. The padding is the same on both axes in each direction, so single values are enough. Nothing needs to stay physical unless the design requires the margin to stay on the left in right-to-left text.