Sub topic 9
Reading the Two-Value Shorthands
This chapter assumes you know the box model, the margin, padding and border properties, and the idea of block and inline axes from earlier chapters.
The pattern
Logical shorthands group two sides of one axis. The axis name comes first, and the values follow in a fixed order.
margin-inline,padding-inlineandborder-inlinecover the inline-start and inline-end sides.margin-block,padding-blockandborder-blockcover the block-start and block-end sides.inset-inlineandinset-blockdo the same for positioning.
With two values, the first is the start side and the second is the end side. With one value, both sides get it.
.card {
padding-block: 1rem; /* start and end both 1rem */
padding-inline: 1.5rem 1rem; /* start 1.5rem, end 1rem */
}Do not confuse these with the classic padding: 1rem 2rem. That shorthand takes values in top, right, bottom, left order, and a two-value form means vertical then horizontal. A logical shorthand is about one axis only, so two values always mean start then end.
What start and end become
The physical sides depend on writing-mode and direction.
| Context | inline-start | inline-end | block-start | block-end |
|---|---|---|---|---|
| English (`horizontal-tb`, `ltr`) | left | right | top | bottom |
| Arabic (`horizontal-tb`, `rtl`) | right | left | top | bottom |
| `vertical-rl`, `ltr` | top | bottom | right | left |
Worked example
Here is the same rule applied to two paragraphs with different directions.
<p dir="rtl" lang="ar" class="note">...</p>
<p dir="ltr" lang="en" class="note">...</p>
<style>
.note {
padding-inline: 2rem 0.5rem;
border-inline-start: 3px solid tomato;
}
</style>In the English paragraph, the left side has 2rem of padding and the tomato border. In the Arabic paragraph, the right side has them. One declaration serves both.
Common errors
- Reading two values as top and right. In
margin-inline: 0 auto, the values are start and end, not vertical and horizontal. - Using three or four values. A logical shorthand accepts only one or two. Anything else makes the declaration invalid and the browser ignores it.
- Expecting a one-value shorthand to centre.
margin-inline: autocentres a block with a setinline-sizebecause both sides areauto.margin-inline: 0 autopushes the block toward the start side. - Forgetting that the box does not rotate. In a vertical writing mode,
padding-blockadds space on the left and right, not the top and bottom.
Setting a single side
You can still set one side with a longhand such as padding-inline-start. The shorthand is a convenience for setting both sides together. A later longhand overrides the matching side from an earlier shorthand.
.panel {
margin-inline: 1rem;
margin-inline-end: 3rem; /* start stays 1rem */
}Exercise
In a page with direction: rtl and writing-mode: horizontal-tb, an element has margin-inline: 0 2rem. Which physical side gets 2rem, and which gets 0?
Show answer
The first value is inline-start and the second is inline-end. In right-to-left text, inline-start is the right side and inline-end is the left side. So the right margin is 0 and the left margin is 2rem.
Exercise
Why is padding-block: 1rem 2rem 1rem invalid, and what would you write to give 1rem at the block-start and 2rem at the block-end?
Show answer
Logical shorthands accept at most two values, one for each side of the axis. The valid form is padding-block: 1rem 2rem.