Sub topic 4
Start and end instead of left and right
This chapter assumes you know the box model, can read basic CSS, and know what the lang and dir attributes do in HTML. You also need the previous chapter's idea that every element has a block axis and an inline axis. Here you learn the vocabulary that names the sides of those axes. Later chapters apply it to margin, padding, borders and positioning.
Four sides, named by flow
Physical CSS names sides by where they appear on the screen: top, right, bottom, left. Logical CSS names them by the direction text flows. Each axis has two ends, so there are four flow-relative sides.
- block-start is the side where the next block line or box would come from. In English it is the top.
- block-end is the opposite side. In English it is the bottom.
- inline-start is the side where a line of text begins. In English it is the left.
- inline-end is the side where a line of text ends. In English it is the right.
The words start and end always follow the flow of content. "Start" is where reading begins on that axis, wherever that is on the screen.
How writing mode and direction pick the side
Two computed values decide which physical side each logical side maps to:
writing-modesets the block axis. Inhorizontal-tb, blocks stack top to bottom. Invertical-rl, blocks stack from right to left. Invertical-lr, blocks stack from left to right.directionsets which end of the inline axis is the start.ltrstarts at the left or top.rtlstarts at the right or bottom.
The browser reads these two values on the element and resolves each logical side to one physical side. The author never writes that mapping.
The mapping table
This table is the main reference for the chapter. Read each row as "in this mode, this logical side lands on this physical side."
| writing-mode | direction | block-start | block-end | inline-start | inline-end |
|---|---|---|---|---|---|
| horizontal-tb | ltr | top | bottom | left | right |
| horizontal-tb | rtl | top | bottom | right | left |
| vertical-rl | ltr | right | left | top | bottom |
| vertical-rl | rtl | right | left | bottom | top |
| vertical-lr | ltr | left | right | top | bottom |
| vertical-lr | rtl | left | right | bottom | top |
Three patterns are worth remembering:
- In horizontal text,
directiononly swaps left and right. The block sides stay top and bottom. - In vertical text, the inline axis runs vertically, so
inline-startis a top or bottom edge. - Switching
vertical-rltovertical-lrswaps only the block sides. Lines of text still run top to bottom.
Seeing it in a page
The same rule can produce different physical results. This page uses one class on two paragraphs.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Start and end</title>
<style>
.note {
padding-inline-start: 2rem;
border-inline-start: 3px solid tomato;
margin-block-end: 1rem;
}
</style>
</head>
<body>
<p dir="rtl" lang="ar" class="note">هذه فقرة تبدأ من اليمين.</p>
<p dir="ltr" lang="en" class="note">This paragraph starts from the left.</p>
</body>
</html>The Arabic paragraph gets its padding and tomato border on the right. The English paragraph gets them on the left. The stylesheet has no [dir="rtl"] override. The dir attribute changed the computed direction, and the browser remapped inline-start.
Prefer the HTML dir attribute over the CSS direction property when you mark up a language. The attribute carries meaning that survives when CSS fails to load, and it works with the browser's bidirectional text handling.
The property names
Flow-relative names follow a fixed pattern. The axis comes first, then the side:
margin-inline-start,margin-inline-end,margin-block-start,margin-block-endpadding-inline-startand the other three padding sidesborder-inline-start-width,border-block-end-colorand so oninset-inline-start,inset-block-endand so on
For a single side, write the axis and the side in the name. Each axis also has a two-value shorthand, such as margin-inline, which sets both ends at once. Those shorthands get their own chapter.
The start and end words also appear as keyword values. text-align: start aligns text to the beginning of the line, and float: inline-start floats to the start side:
.figure {
float: inline-start;
margin-inline-end: 1rem;
text-align: start;
}In left-to-right text the figure floats left and keeps its gap on the right. In right-to-left text it floats right and keeps the gap on the left. The rule says what you mean, "image at the start, gap on the other side", and the browser works out which edge that is.
Which element's mode counts
The mapping uses the element's own computed writing-mode and direction, not the page's. Both properties inherit, so most elements share their parent's values. They differ when you override them on a descendant.
<div dir="ltr">
<p>Padding on my left.</p>
<p dir="rtl">Padding on my right.</p>
</div>If a shared stylesheet gives p the rule padding-inline-start: 2rem, the first paragraph is padded on the left and the second on the right. The outer div has no effect on the second paragraph. This is the behavior you want for quoted text in another language, and it explains surprising results when a component sets its own dir.
Common errors
Mixing physical and logical for the same side. margin-left and margin-inline-start do not create two separate margins. Both set the same underlying margin side for a given element. When both are declared with equal specificity, the one later in the source wins, and the other is overridden.
.item {
margin-inline-start: 1rem;
margin-left: 0;
}In left-to-right text, margin-left: 0 comes last and wins, so the logical rule does nothing. In right-to-left text the two rules touch different sides, so you get a margin on the right and none on the left. Pick one vocabulary per component.
Assuming start means left. text-align: left and text-align: start look the same in an English page and diverge in Arabic or Hebrew. If the text should always align to the reading start, use start.
Forgetting vertical text swaps the axes. padding-inline-start in vertical-rl is padding at the top of the box. Code that assumed "inline means horizontal" will space the wrong edge.
Setting direction on body to fake localisation. It changes the inline start for the whole subtree, including parts that should not flip, such as code samples and embedded content in another language. Set dir on the specific elements whose language needs it.
Summary
block-start,block-end,inline-startandinline-endname sides by the direction content flows.writing-modesets the block axis anddirectionsets which end of the inline axis comes first. Together they map each logical side to one physical side.- The element's own computed values decide the mapping, so a child with
dir="rtl"flips inside anltrparent. - A physical and a logical property can target the same side, so keep each component to one vocabulary.
Exercise
An element has writing-mode: vertical-rl and direction: ltr. It has border-inline-start: 4px solid red and margin-block-start: 2rem. Which physical sides do the red border and the margin land on?
Show answer
In vertical-rl with ltr, the inline axis runs top to bottom, so inline-start is the top and the red border is on the top edge. Blocks stack from right to left, so block-start is the right side and the 2rem margin is on the right.
Exercise
A stylesheet sets .tag { margin-left: 8px; margin-inline-start: 0; } on an element inside an English page. What margin does the element have, and why?
Show answer
It has 0 left margin. In left-to-right horizontal text, margin-inline-start and margin-left set the same side. They have equal specificity, so the later declaration, margin-inline-start: 0, wins and replaces the 8px.