Sub topic 9
Reading the two-value shorthands
This chapter assumes you know the box model, the margin, padding and inset properties, and the block and inline axes from the earlier chapters. The goal is to read a declaration like padding-inline: 1.5rem 1rem and say which physical sides it affects.
What a two-value logical shorthand is
Logical shorthands set the two sides of one axis in a single declaration. The property name picks the axis, and the values are always in the order start, then end.
| Shorthand | Axis | First value | Second value |
|---|---|---|---|
| `margin-inline` | inline | inline-start margin | inline-end margin |
| `margin-block` | block | block-start margin | block-end margin |
| `padding-inline` | inline | inline-start padding | inline-end padding |
| `padding-block` | block | block-start padding | block-end padding |
| `inset-inline` | inline | inline-start offset | inline-end offset |
| `inset-block` | block | block-start offset | block-end offset |
| `border-inline-width` | inline | inline-start width | inline-end width |
| `border-block-width` | block | block-start width | block-end width |
The -style and -color versions of the border shorthands follow the same pattern.
If you give one value, it applies to both sides of the axis. padding-block: 1rem means 1rem at the block start and 1rem at the block end.
Reading a declaration step by step
Take the card from the topic's first example:
.card {
inline-size: min(100%, 40ch);
margin-inline: auto;
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
border-inline-start: 4px solid steelblue;
}To read it, do three things:
- Find the axis from the property name.
padding-inlineis the inline axis. - Assign the values in order.
1.5remis the start side and1remis the end side. - Resolve start and end with the element's
writing-modeanddirection.
In horizontal-tb with direction: ltr, inline-start is the left. The padding is 1.5rem on the left and 1rem on the right. With direction: rtl, inline-start is the right, so the padding is 1.5rem on the right and 1rem on the left. The CSS did not change. Only the mapping did.
The mapping in each writing mode
The block axis and inline axis rotate with the writing mode. The inline axis follows the direction in which text runs. The block axis follows the direction in which lines stack.
| `writing-mode` | `direction` | inline-start | inline-end | block-start | block-end |
|---|---|---|---|---|---|
| `horizontal-tb` | `ltr` | left | right | top | bottom |
| `horizontal-tb` | `rtl` | right | left | top | bottom |
| `vertical-rl` | `ltr` | top | bottom | right | left |
| `vertical-lr` | `ltr` | top | bottom | left | right |
So margin-block: 0 2rem puts the 2rem gap below the element in English. In vertical-rl it puts the gap on the left, which is where the next line of text would go.
You can check this mapping in a browser. This page marks each side with a different border color. Add dir="rtl" to the <html> element, or change writing-mode, and watch the colors move:
<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Shorthand mapping</title>
<style>
.probe {
padding: 1rem;
border-style: solid;
border-width: 6px;
border-inline-color: crimson royalblue;
border-block-color: seagreen goldenrod;
}
</style>
</head>
<body>
<p class="probe">Inline-start is crimson. Inline-end is blue.
Block-start is green. Block-end is gold.</p>
</body>
</html>In left-to-right English, the left border is crimson and the right border is blue. Set dir="rtl" on <html> and they swap.
The `border-inline` trap
border-inline and border-block look like they should take two values for the two sides. They don't. They are shorthands for width, style and color, and they apply the same result to both sides of the axis:
.a {
/* both inline sides get 2px solid gray */
border-inline: 2px solid gray;
}
.b {
/* two values per side need the specific properties */
border-inline-width: 4px 1px;
border-inline-color: steelblue transparent;
border-inline-style: solid;
}
.c {
/* one side only */
border-inline-start: 4px solid steelblue;
}To give the two sides different widths, use border-inline-width, -style or -color with two values. To style a single side, use border-inline-start or border-inline-end.
`margin-inline: auto` and `inset` shorthands
Keywords work in these shorthands the same way they do in the physical versions. margin-inline: auto centers a block with a set inline-size in any writing mode, because it makes both inline margins absorb the free space:
.panel {
inline-size: 30rem;
max-inline-size: 100%;
margin-inline: auto;
}You can also mix a keyword with a length. margin-inline: auto 0 pushes the box toward the inline-end side. In English that is the right edge, and in Arabic the left edge.
For positioning, inset-inline and inset-block accept two values and the keyword auto. A full-width banner pinned to the top might use:
.banner {
position: fixed;
inset-block-start: 0;
inset-inline: 0;
}inset-inline: 0 is the same as inset-inline: 0 0. It stretches the banner from one inline edge to the other.
Interaction with physical properties
A logical property and its physical counterpart set the same underlying value. They are not separate settings. margin-inline-start and margin-left are the same property in an LTR horizontal element, and margin-right in an RTL one. The cascade resolves a conflict by specificity and then source order, as it always does:
.item {
margin-inline: 1rem 2rem;
margin-left: 0;
}In LTR, margin-left: 0 comes later and wins over the start margin, so the left margin is 0 and the right margin is 2rem. In RTL, margin-left is the end side, so it overrides the 2rem. The element gets 1rem on the right and 0 on the left. This is a common source of "it works in English only" bugs when a stylesheet mixes both styles. Pick one family per component, and convert the rest in the same change.
A shorthand also resets the longhands it covers. If padding-inline: 1rem comes after padding-inline-start: 3rem on the same element, the later shorthand wins and the 3rem is gone. To override only one side, put the longhand after the shorthand.
Common mistakes
- Reading the order as left, right. The first value is start, not left. In RTL it lands on the right.
- Using
margin-inlinewith four values. It accepts one or two. Four values belong to the physicalmargin. - Mixing
margin-leftwithmargin-inline-*on one element, which hides the logical rule in one direction. - Assuming
blockalways means vertical. Invertical-rl,padding-blockaffects the left and right sides. - Expecting
border-inlineto take two widths. Useborder-inline-widthfor that.
Exercise
A paragraph has dir="rtl", the default horizontal-tb writing mode, and padding-inline: 3rem 1rem; padding-block: 0.5rem 2rem;. How much padding is on each physical side?
Show answer
In RTL horizontal text, inline-start is the right and inline-end is the left, so the right padding is 3rem and the left padding is 1rem. The block axis runs top to bottom regardless of direction, so the top padding is 0.5rem and the bottom padding is 2rem.
Exercise
You want a box with a 6px border on its inline-start side, a 1px border on its inline-end side, and no border on the block sides. Write the CSS using the fewest declarations that is still correct.
Show answer
Use border-inline-width with two values, plus a style so the borders render:
.box {
border-inline-width: 6px 1px;
border-inline-style: solid;
}
The block borders stay at their default width and style, which draws no border. Writing border-inline: 6px 1px solid would be invalid, because border-inline applies one width to both sides.