Sub topic 7
Logical borders
This chapter assumes you know the CSS box model, the border shorthand, and how writing-mode and direction decide the block and inline axes. Here you replace border-left and its relatives with flow-relative equivalents.
Why borders need logical names
A physical border such as border-left stays on the left in every language. A callout with an accent stripe on the left looks right in English. In Arabic or Hebrew the text starts on the right, so the stripe sits at the end of the line, away from where reading begins.
Logical border properties name the side by its role in the text flow:
| Physical | Logical (horizontal, LTR) |
|---|---|
| `border-top` | `border-block-start` |
| `border-bottom` | `border-block-end` |
| `border-left` | `border-inline-start` |
| `border-right` | `border-inline-end` |
The mapping changes with the writing mode and direction. In direction: rtl, inline-start is the right side. In writing-mode: vertical-rl, block-start is the right side and inline-start is the top.
Property families
Each logical side has the same longhands as the physical one: width, style and color.
border-inline-start,border-inline-end,border-block-startandborder-block-endset width, style and color together.border-inline-start-width,border-inline-start-styleandborder-inline-start-colorset one aspect of one side. The other sides follow the same pattern.border-inlineandborder-blockset both sides of an axis at once.border-inline-width,border-inline-styleandborder-inline-colorset one aspect on both sides of the inline axis. Theborder-block-*versions do the same for the block axis.
The two-value forms take the start value first and the end value second. A single value applies to both sides.
.card {
inline-size: min(100%, 40ch);
margin-inline: auto;
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
border-inline-start: 4px solid steelblue;
}The stripe is on the left in English and on the right in Arabic, with no extra rules.
Direction-aware example
The same rule can serve both directions. Here one class styles a right-to-left paragraph and a left-to-right paragraph.
<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 border and padding on the right. The English paragraph gets them on the left.
Common mistakes
- Mixing physical and logical declarations for the same side. If
border-leftandborder-inline-startboth resolve to the same edge, the later declaration in the cascade wins. Pick one family per rule. - Assuming the shorthand
border-inlinemeans the start side only. It sets both inline sides. Useborder-inline-startfor one side. - Forgetting that the style is
noneby default. Setting onlyborder-inline-start-width: 4pxdraws nothing until a style such assolidis set. - Expecting radius to follow
border-inline. Corner rounding uses separate logical properties, covered in a later chapter.
Exercise
A blockquote uses border-left: 4px solid gray; padding-left: 1rem;. Rewrite it with logical properties, and say on which physical side the border appears when the blockquote has dir="rtl".
Show answer
Use border-inline-start: 4px solid gray; padding-inline-start: 1rem;. In a right-to-left blockquote the inline start side is the right, so the border and padding appear on the right.
Exercise
In writing-mode: vertical-rl with direction: ltr, which physical side does border-block-end draw on?
Show answer
The left side. In vertical-rl the block axis runs right to left, so block-start is the right side and block-end is the left side.