Sub topic 7
Logical borders
This chapter assumes you know the box model, the physical border-left and border-top families, and the dir attribute. It also assumes you have read the earlier chapters on writing modes and on the block and inline axes. Borders are the easiest place to start with logical properties. Most border decoration is directional: a quote bar, a divider under a heading, an accent on one edge. That is where physical properties break.
The problem with border-left
Suppose you mark a quotation with border-left: 4px solid steelblue. In English the bar sits at the start of each line, next to where the reader begins. In Arabic or Hebrew the text starts on the right, so the bar ends up at the end of the lines, away from where reading begins. Often it also collides with padding you wrote for the other side.
Logical border properties name the side by its role in the text flow. The browser resolves that role to a physical edge using the element's writing-mode and direction.
The twelve longhand-style side properties
Each of the four logical sides has a shorthand that sets width, style and color together:
border-inline-startborder-inline-endborder-block-startborder-block-end
Each side also has three longhands. For border-inline-start they are border-inline-start-width, border-inline-start-style and border-inline-start-color. The other three sides follow the same pattern.
The mapping to physical edges depends on the writing mode:
| Logical side | horizontal-tb, ltr | horizontal-tb, rtl | vertical-rl, ltr | vertical-lr, ltr |
|---|---|---|---|---|
| block-start | top | top | right | left |
| block-end | bottom | bottom | left | right |
| inline-start | left | right | top | top |
| inline-end | right | left | bottom | bottom |
Block sides follow the writing mode, which sets the direction in which lines stack. Inline sides follow direction, which sets the direction in which text runs along a line. That is why switching direction swaps the left and right edges but leaves top and bottom alone.
One rule, both directions
This example uses a single rule for two paragraphs with opposite directions:
<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 bar and padding on the right. The English paragraph gets them on the left. You write no [dir="rtl"] override. Without logical properties you would need .note { border-left: ... } plus .note:dir(rtl) { border-left: 0; border-right: ... }, and you would have to remember to keep the padding in sync.
Block borders and vertical text
The block sides are useful for dividers between stacked items:
.list-item {
border-block-end: 1px solid #ddd;
}
.list-item:last-child {
border-block-end: 0;
}In horizontal text this draws a line under each item. In vertical-rl text, where lines stack from right to left, the block-end edge is the left edge of each item. The divider still lies between one line and the next, and your intent holds. If you had written border-bottom, the divider would run along the bottom of vertical columns and separate nothing.
Shorthands that cover both sides of an axis
border-inline and border-block set both sides of an axis at once.
.panel {
border-inline: 2px solid #444; /* start and end */
border-block: 1px dashed #999; /* top and bottom in horizontal text */
}These shorthands accept one width, one style and one color. They apply that value to both sides, and they cannot give each side a different value. To get different values on the two sides, use the per-axis longhands, which take one or two values:
.panel {
border-inline-style: solid;
border-inline-width: 1px 6px; /* start 1px, end 6px */
border-inline-color: #888 tomato; /* start gray, end tomato */
}The two-value order is always start then end. The same order applies to border-block-width, border-block-style and border-block-color. This differs from the physical border-width, which takes values in the order top, right, bottom, left.
You can also combine a shorthand with a more specific rule that comes later:
.tab {
border-block-end: 3px solid transparent;
}
.tab[aria-selected="true"] {
border-block-end-color: royalblue;
}Mixing logical borders with other logical properties
Logical borders usually appear alongside logical spacing and sizing. Here is the card from the sizing and spacing chapters with a border accent:
.card {
inline-size: min(100%, 40ch);
margin-inline: auto;
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
border-inline-start: 4px solid steelblue;
}The two-value padding-inline gives 1.5rem at the start, where the accent bar sits, and 1rem at the end. Because every property in the rule is flow-relative, the whole card mirrors correctly under dir="rtl".
Common errors
Setting a width with no style
The initial value of border-style is none, and a none border has a computed width of zero. These rules draw nothing:
.a { border-inline-start-width: 4px; }
.b { border-inline-start: 4px steelblue; }Rule .a sets no style. Rule .b omits the style, so the shorthand resets it to none. Always include a style such as solid in the shorthand.
Mixing physical and logical rules for the same edge
Logical and physical properties are different names for the same underlying edge. When both are set, they compete in the normal cascade. The one with higher specificity wins, and with equal specificity the later declaration wins:
.box {
border-inline-start: 4px solid red;
border-left: 1px solid black; /* later, so it wins in ltr horizontal text */
}In an LTR horizontal context, the left border ends up 1px black. In RTL, the border-left rule affects the opposite edge from the logical one, so both declarations show up on different sides. Mixing the two families in one rule produces results that change with direction. Pick one family per component.
Resetting with the wrong property
border: 0 and border: none reset all four sides and work in any writing mode. If you want to remove only one logical side, reset that side:
.item:last-child {
border-block-end: none;
}Writing border-bottom: none here would remove the wrong edge in vertical text and leave the divider on the other side.
Expecting every border-related property to be logical
Some related features have no logical form or only a partial one:
border-imageand its longhands use physical sides.outlinehas no per-side form, so there is nothing to convert.- Corner rounding uses its own logical names, such as
border-start-start-radius, which are covered in a later chapter.
Migrating border rules
For an existing stylesheet, a safe conversion follows these steps:
- Search for
border-left,border-right,border-topandborder-bottom, including their-width,-styleand-colorforms. - For each hit, decide whether the side means "where text starts or ends" (convert it) or "a fixed edge of the screen" (keep it).
- Convert horizontal-text rules using the table above: left to
inline-startand right toinline-endfor LTR, top toblock-start, and bottom toblock-end. - Delete any
[dir="rtl"]overrides that only mirrored the border. - Check the result in
dir="rtl"and in a vertical writing mode before moving on.
Exercise
A tab bar uses .tab { border-bottom: 3px solid transparent; } and .tab.active { border-bottom-color: royalblue; }. A designer adds a vertical-rl version of the site in which the tab indicator should sit on the side of each tab that faces the following line of tabs. Rewrite the two rules with logical properties, and say which physical edge the indicator will occupy in vertical-rl text.
Show answer
Use .tab { border-block-end: 3px solid transparent; } and .tab.active { border-block-end-color: royalblue; }. In vertical-rl, lines stack from right to left, so block-end is the left edge. The indicator appears on the left side of each tab. In ordinary horizontal text it remains the bottom edge.
Exercise
What does border-inline-width: 2px 8px; do in dir="rtl" horizontal text, and why does border-inline: 2px 8px solid red; not do the same thing?
Show answer
The first sets the inline-start border to 2px and the inline-end border to 8px. In RTL horizontal text, inline-start is the right edge, so the right border is 2px and the left is 8px. The second is invalid. The border-inline shorthand takes one width, one style and one color and applies them to both sides, so the extra width value makes the declaration fail.