Sub topic 1
Why physical properties break across languages
Prerequisites
This chapter assumes you know the following:
- Basic CSS syntax and the box model: content, padding, border and margin.
- How
width,height,margin,paddingandpositionbehave. - Basic HTML, including the
langanddirattributes. - Some languages are written right to left (Arabic, Hebrew) or top to bottom (traditional Chinese, Japanese).
You do not need to know any logical properties yet. This chapter explains why they exist. Later chapters cover each one.
The problem in one rule
Here is a common rule. It adds a colored bar and some space on the left of a note, so the bar sits before the text.
<p lang="en" dir="ltr" class="note">Read this first.</p>
<p lang="ar" dir="rtl" class="note">اقرأ هذا أولاً.</p>
<style>
.note {
padding-left: 2rem;
border-left: 3px solid tomato;
margin-right: 1rem;
}
</style>In the English paragraph, the bar sits at the start of each line. Text begins right after it, and that is what the designer wanted.
In the Arabic paragraph, the text starts at the right edge of the box and runs leftward. The bar and the padding are still on the left. They now sit at the end of each line, not the start. The margin-right gap, meant to sit at the end, is now at the start side. Every side in the rule is wrong, and the browser followed your instructions exactly.
Physical properties name screen sides
A physical property names a side of the screen: top, right, bottom or left. The side does not change with the language of the content. margin-left always means the left edge of the box.
That is the right behavior when you care about the screen. It is the wrong behavior when you care about reading order. Most spacing decisions are about reading order:
- "Put the icon before the label."
- "Indent the first level of a list."
- "Leave a gap after the avatar."
- "Pin the close button to the end of the title bar."
Each phrase uses "before", "after", "first" or "end". Those words refer to the direction the text flows. Physical properties cannot express them.
What changes between languages
Two settings decide how text flows.
- Direction is the order in which characters run along a line. It is
ltr(left to right) for English andrtl(right to left) for Arabic and Hebrew. You set it with thedirattribute or the CSSdirectionproperty. - Writing mode is the axis along which lines run, and the order in which new lines are stacked. You set it with the CSS
writing-modeproperty. The defaulthorizontal-tbmeans lines run horizontally and stack top to bottom. The valuevertical-rlmeans lines run vertically and stack from right to left. That is the usual mode for traditional Japanese and Chinese.
| Setting | Lines run | Lines stack | Example |
|---|---|---|---|
| `horizontal-tb`, `ltr` | left to right | top to bottom | English |
| `horizontal-tb`, `rtl` | right to left | top to bottom | Arabic, Hebrew |
| `vertical-rl`, `ltr` | top to bottom | right to left | Japanese, Chinese |
| `vertical-lr`, `ltr` | top to bottom | left to right | Mongolian |
Physical properties ignore both settings. A layout that depends on them is correct for exactly one row of this table.
Failure 1: spacing lands on the wrong side
The note example above is the simplest failure. Any margin-left, padding-right, border-left or similar property that carries a reading-order meaning will flip its meaning in right-to-left text. Common cases include:
- The indent on a blockquote or nested list.
- The accent border on a callout.
- The gap between an icon and its label.
- Padding that makes room for a button inside an input.
The last case produces overlap. Suppose an input reserves space on the right for a clear button:
.search input {
padding-right: 2.5rem;
}
.search button {
position: absolute;
right: 0.5rem;
}In right-to-left text the typed characters start at the right edge. The reserved padding is still on the right, but the button now sits on top of the first characters the user types. The visual result is text hidden under a control.
Failure 2: alignment and floats
text-align: left and float: left are physical too. A left-aligned paragraph in Hebrew has a ragged right edge where readers expect a flush right edge. A floated image stays on the left, so text wraps on the wrong side of the image relative to the reading start. The layout is not broken, but it feels backwards to a native reader.
Failure 3: sizing in vertical text
Sizing breaks in a different way. In a vertical writing mode, text runs along the vertical axis. Lines are tall and the stack of lines grows sideways.
.column {
writing-mode: vertical-rl;
width: 20rem;
height: 10rem;
}Here width is horizontal, which is the direction new lines are added. Content that was designed to fill a 20rem line length gets only 10rem of line length, because the line length is now along the height. You would have to swap the two values by hand every time you change the writing mode. A name like "the length along the line" would stay correct. width does not.
Why per-language overrides do not scale
The usual fix is a set of overrides for each direction:
.note {
padding-left: 2rem;
border-left: 3px solid tomato;
}
[dir="rtl"] .note {
padding-left: 0;
border-left: 0;
padding-right: 2rem;
border-right: 3px solid tomato;
}This works, but it has costs:
- Every physical rule needs a mirrored twin. A stylesheet with 300 spacing rules needs about 300 overrides.
- You must remember to reset the original side. Forgetting
padding-left: 0leaves padding on both sides. - The override selector adds specificity, which complicates later rules.
- Vertical modes need another set of overrides, and combinations multiply the work.
- The two copies drift apart over time, so a later edit to one side is easy to miss in the other.
Some teams automate this with a build step that flips every left and right in a generated stylesheet. That helps for left-to-right versus right-to-left, but it still cannot handle vertical text, and it cannot tell which left is a reading-order rule and which one is a true screen-side rule.
The idea behind logical properties
Logical properties describe layout relative to the flow of text, not the screen. Instead of "left", you say "start of the line". Instead of "width", you say "size along the line". The browser reads the element's writing-mode and direction, then maps each logical name to a physical side.
The note, written logically:
<p lang="en" dir="ltr" class="note">Read this first.</p>
<p lang="ar" dir="rtl" class="note">اقرأ هذا أولاً.</p>
<style>
.note {
padding-inline-start: 2rem;
border-inline-start: 3px solid tomato;
margin-inline-end: 1rem;
}
</style>One rule now serves both paragraphs. In the English one, the start side is the left. In the Arabic one, it is the right. No override is needed.
The diagram shows the mapping the browser performs. You write the rule once, and the writing mode and direction pick the physical side. The next chapters define writing modes, the block and inline axes, and each logical property family.
Common mistakes to avoid
- Treating
diras cosmetic. Settingdir="rtl"changes how the browser resolves logical properties. Physical ones ignore it, so mixed rules give mixed results. - Mirroring everything. Not every
leftis a reading-order concern. A drop shadow cast toward a light source on the left of the screen stays physical. A later chapter covers when to keep physical values. - Assuming only Arabic and Hebrew matter. Vertical text and mixed-direction pages, such as an English interface that shows Arabic user content, expose the same problem.
Summary of the chapter
- Physical properties name screen sides, so they ignore direction and writing mode.
- Most spacing intent concerns reading order, such as before, after, start and end.
- Right-to-left text misplaces spacing, borders, alignment and floats.
- Vertical text breaks
widthandheightas stand-ins for line length and stack depth. - Manual overrides double the CSS and do not cover every writing mode.
- Logical properties let one rule adapt, because the browser maps each logical name to a physical side.
Exercise
A stylesheet gives .tag the rules margin-right: 0.5rem and border-left: 2px solid gray so the border leads the tag and the gap follows it. A page in Hebrew (dir="rtl") uses the same class. On which physical sides do the border and gap end up, and where did the author intend them? Rewrite the two declarations so they work in both directions.
Show answer
The border stays on the left and the gap stays on the right, because physical properties ignore dir. In Hebrew the text starts at the right, so the border now sits at the end of the tag and the gap at the start. The author intended the border at the start and the gap at the end. The logical version is margin-inline-end: 0.5rem and border-inline-start: 2px solid gray.
Exercise
A container has writing-mode: vertical-rl, width: 20rem and height: 10rem. The designer wanted the lines of text to be 20rem long. What is the line length actually, and why does the same CSS give a different result in horizontal text?
Show answer
The line length is 10rem. In vertical text, lines run along the vertical axis, so the line length is the height. In horizontal text, lines run along the horizontal axis, so the same width: 20rem would give a 20rem line length. width and height are tied to the screen, not to the text flow.