Sub topic 14
Logical properties for margin and padding
This chapter assumes you can write CSS rules with selectors and declarations, and that you know the box model layers (content, padding, border, margin), box-sizing, and common length units such as px, rem, and %. It covers how to write spacing that follows the text direction instead of fixed screen sides.
Physical and logical thinking
Properties like margin-left and padding-top are physical. They name a side of the screen: top, right, bottom, left. This works well for English, which runs left to right and top to bottom.
Not every language works this way:
- Arabic and Hebrew run right to left.
- Traditional Japanese, Chinese, and Korean can run top to bottom, with lines stacked from right to left.
- Mongolian runs top to bottom, with lines stacked from left to right.
If an Arabic page uses margin-left: 2rem to separate an icon from its label, the gap lands on the wrong side. You would need extra rules to fix it.
Logical properties name sides relative to the flow of text. They use two axes:
- The inline axis is the direction text runs within a line.
- The block axis is the direction new lines and block boxes stack.
Each axis has a start and an end. In English, inline-start is the left and inline-end is the right. Block-start is the top and block-end is the bottom. In Arabic, inline-start is the right. The logical names stay the same, and the browser maps them to physical sides.
The property names
Each physical property has a logical counterpart. The table lists the margin and padding properties.
| Physical | Logical (longhand) | Logical (shorthand) |
|---|---|---|
| `margin-left` | `margin-inline-start` | `margin-inline` (start and end) |
| `margin-right` | `margin-inline-end` | `margin-inline` (start and end) |
| `margin-top` | `margin-block-start` | `margin-block` (start and end) |
| `margin-bottom` | `margin-block-end` | `margin-block` (start and end) |
| `padding-left` | `padding-inline-start` | `padding-inline` (start and end) |
| `padding-right` | `padding-inline-end` | `padding-inline` (start and end) |
| `padding-top` | `padding-block-start` | `padding-block` (start and end) |
| `padding-bottom` | `padding-block-end` | `padding-block` (start and end) |
The mapping in the physical column holds only for horizontal text running left to right. In other modes the logical side points somewhere else.
Size has logical names too. inline-size is the width in horizontal text and the height in vertical text. block-size is the opposite. The min- and max- versions are min-inline-size, max-inline-size, min-block-size, and max-block-size.
Shorthand syntax
The logical shorthands take one or two values. They do not take the four-value clockwise form, because each one covers only one axis.
.note {
/* One value: both sides of the axis get 1rem */
margin-block: 1rem;
/* Two values: start, then end */
padding-inline: 2rem 0.5rem;
}For all four sides at once, the standard shorthands margin and padding are still physical. They are not logical. The inset family for positioned elements has logical versions too, but this chapter stays with margin and padding.
A direction-aware example
The spine's example centres a column and constrains it with logical properties.
.article {
max-inline-size: 40rem;
margin-inline: auto;
padding-block: 1rem;
}Reading it line by line:
max-inline-size: 40remlimits the box along the inline axis. In horizontal text this ismax-width.margin-inline: autosets both inline margins toauto. A block box with a constrained width then centres, as it does withmargin: 0 auto.padding-block: 1remadds 1rem of padding at the block-start and block-end. In horizontal text these are the top and bottom.
In a normal English page this renders exactly like the physical version. The difference shows when the writing mode changes.
Seeing the flip
The next page uses one icon-and-label rule. It runs as shown, and you can switch dir between ltr and rtl to compare.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Logical spacing</title>
<style>
*,
*::before,
*::after {
box-sizing: border-box;
}
.row {
display: flex;
align-items: center;
border: 2px solid #333;
padding-inline: 1rem;
}
.icon {
margin-inline-end: 0.75rem;
font-size: 1.5rem;
}
</style>
</head>
<body>
<div class="row" dir="ltr">
<span class="icon">★</span>
<span>Left-to-right row</span>
</div>
<br>
<div class="row" dir="rtl">
<span class="icon">★</span>
<span>Right-to-left row</span>
</div>
</body>
</html>In the first row the icon sits at the left and margin-inline-end puts the gap on its right. In the second row the flex container reverses its main axis because of dir="rtl". The icon moves to the right edge, and the same margin-inline-end now puts the gap on its left. A single rule serves both directions. With margin-right, the second row would have its gap on the wrong side, touching the label.
Writing modes
Direction is only one input. The writing-mode property changes which axis is inline.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Vertical writing</title>
<style>
.vertical {
writing-mode: vertical-rl;
padding-block: 1rem;
padding-inline: 0.25rem;
border: 2px solid #333;
block-size: 12rem;
}
</style>
</head>
<body>
<p class="vertical">縦書きのテキスト</p>
</body>
</html>With vertical-rl, the inline axis is vertical, so padding-block: 1rem now adds space on the right and left. padding-inline: 0.25rem adds space at the top and bottom. The rules did not change, but the physical sides they affect did.
Margin collapsing and logical properties
Margin collapsing happens on the block axis, so it applies to margin-block-start and margin-block-end. The earlier rule is unchanged: adjoining block-axis margins collapse to the larger one. In vertical writing modes, this means the collapse happens between left and right margins, not top and bottom. Inline-axis margins never collapse, in any writing mode.
This is the reason to use the right axis in your head. "Vertical margins collapse" is a shorthand for "block-axis margins collapse," and it is only true for horizontal text.
Mixing logical and physical properties
You can use both in one stylesheet, but they share the same underlying values. If two declarations set the same side, the cascade picks the winner by specificity and source order, as with any two properties that conflict.
.box {
margin-left: 10px;
margin-inline-start: 20px;
}In left-to-right text, both declarations target the left margin. The later one wins, so the margin is 20px. If you reverse the order, the margin is 10px. In right-to-left text, margin-inline-start targets the right margin, and the left margin stays 10px.
For this reason, pick one approach for a given component. A mix makes it hard to tell which side a rule will change.
Common errors
- Using four-value syntax on a logical shorthand.
margin-inline: 1px 2px 3px 4pxis invalid and the declaration is dropped. Use at most two values. - Assuming block means vertical. Block is vertical only in horizontal writing modes. In
vertical-rlit is horizontal. - Mixing
margin-leftandmargin-inline-starton the same element. The result depends on direction and source order. Choose one. - Forgetting that
autostill centres on the inline axis.margin-inline: autocentres a box that has a constrained inline size. Without a constraint such asmax-inline-size, a block box already fills the line and has nothing to centre. - Expecting
border-leftto change with direction. Borders have logical forms too, such asborder-inline-startandborder-block-end, but the physical ones stay fixed. Convert them as well if you want the whole box to adapt. - Using logical properties without checking support. Current evergreen browsers support them. Very old browsers do not, and they ignore the declarations. If you must support them, put a physical declaration first and the logical one after it as an override.
.legacy {
margin-left: 1rem;
margin-inline-start: 1rem;
}Older browsers skip the unknown logical declaration and use the physical one. Newer browsers apply both, and the logical one wins because it comes later.
When to choose logical properties
Use logical properties when:
- The site is or may be translated into right-to-left or vertical languages.
- You build reusable components, such as buttons with icons, where the "start" side matters more than the "left" side.
- You want one stylesheet to serve every language version.
Stay with physical properties when:
- The design depends on a real screen side regardless of language. A drop shadow cast toward the bottom right, or a sticky bar fixed to the physical left edge, is about the screen and not the text.
- You work in a codebase that is physical throughout and gains nothing from a partial switch.
Exercise
A component uses margin-right: 1rem to space an icon from its label. The page is translated to Arabic and the page root gets dir="rtl". The icon now touches the label. Rewrite the rule with a logical property, and say which physical side it will affect in the Arabic page.
Show answer
Use margin-inline-end: 1rem. In right-to-left text, inline-end is the left side, so the gap appears on the icon's left, between the icon and the label that follows it in reading order.
Exercise
In an element with writing-mode: vertical-rl, you set padding-block: 2rem and padding-inline: 0.5rem. Which physical sides receive 2rem, and which receive 0.5rem?
Show answer
In vertical-rl, the block axis is horizontal, so padding-block: 2rem applies to the right and left sides. The inline axis is vertical, so padding-inline: 0.5rem applies to the top and bottom.
Exercise
Is margin-inline: 10px 20px 30px 40px valid? If not, what is the fix for setting four different sides?
Show answer
It is not valid, because the logical shorthands accept one or two values for one axis. To set four sides, use margin-inline: 10px 20px plus margin-block: 30px 40px, or the four longhands such as margin-inline-start, margin-inline-end, margin-block-start, and margin-block-end.