Sub topic 4
Start and end instead of left and right
This chapter assumes you know the box model, the lang and dir attributes, and the idea of block and inline axes from the previous chapter. It covers how flow-relative sides replace left and right.
From four sides to four flow-relative sides
Physical properties name sides of the screen: top, right, bottom, left. Logical properties name sides of the content flow:
- block-start: where the first line of a block begins.
- block-end: where blocks stack toward.
- inline-start: where a line of text begins.
- inline-end: where a line of text ends.
The browser maps each flow-relative side to a physical side using writing-mode and direction.
| 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 |
One rule, two directions
Here the same rule serves both an Arabic and an English paragraph. The padding and border land on the right for rtl and on the left for ltr.
<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>With physical properties you would need a second rule such as [dir="rtl"] .note { padding-right: 2rem; border-right: ... }, and you would have to reset the left side too.
Positioning in a corner
Inset properties use the same names. This badge sits in the block-start, inline-end corner of its containing block. That is the top right in English and the top left in Arabic.
.badge {
position: absolute;
inset-block-start: 0;
inset-inline-end: 0;
}The element needs a positioned ancestor, as with any absolutely positioned element.
Keywords that follow the flow
Some properties take logical keywords instead of new property names. text-align: start aligns text to the inline-start side. float: inline-start floats an element to that side. The figure below keeps its gap on the opposite side in both directions.
.figure {
float: inline-start;
margin-inline-end: 1rem;
text-align: start;
}Common errors
- Mixing sides. Pairing
float: inline-startwithmargin-rightleaves the gap on the wrong side in right-to-left text. Change both to logical forms. - Forgetting the cascade. A physical and a logical property can set the same physical side. The one declared later wins, and the order of the two declarations matters.
- Assuming start means left. In
vertical-rl, inline-start is the top. Test with a vertical mode before relying on a mapping. - Using
text-align: leftfor body text. It stays left in right-to-left text. Usestartunless you truly need the left.
Exercise
A sidebar uses margin-left: 2rem and border-left: 4px solid gray. Rewrite both so they work in right-to-left pages, and say where the border appears when the sidebar has dir="rtl".
Show answer
Use margin-inline-start: 2rem and border-inline-start: 4px solid gray. In dir="rtl" horizontal text, inline-start is the right side, so the margin and border appear on the right.
Exercise
In writing-mode: vertical-rl with direction: ltr, which physical side does inset-block-start refer to?
Show answer
The right side. In vertical-rl, blocks stack from right to left, so block-start is the right edge.