Sub topic 15
Testing with dir=rtl and vertical writing modes
This chapter assumes you know the box model, the lang and dir attributes, and the logical properties covered earlier in this tutorial: inline-size, margin-inline, padding-block, inset-inline-end, and the start and end keywords. Here you will learn how to check that a layout built from those properties adapts to right-to-left text and to vertical writing modes.
Why you must test, not assume
Logical properties are only a promise that a rule follows the flow. The promise breaks silently in three ways:
- A physical property is still hiding in the stylesheet, in a third-party component, or in an inline style.
- A value that looks logical is not. A
transform: translateX(10px)or abackground-position: leftstill moves in physical space. - Content assumes a direction, such as an arrow icon that always points right.
You will not see these defects in an English, left-to-right review. You need to flip the document and look.
Flipping direction with dir and lang
The cheapest test is to change dir on the root element. The dir attribute sets the base text direction, and lang tells the browser which language the text is in. Set both so that fonts, hyphenation and quotation marks also behave correctly.
<!doctype html>
<html lang="ar" dir="rtl">
<head>
<meta charset="utf-8">
<title>RTL check</title>
<style>
.note {
padding-inline-start: 2rem;
border-inline-start: 3px solid tomato;
margin-block-end: 1rem;
}
</style>
</head>
<body>
<p class="note">هذا نص تجريبي لاختبار الاتجاه.</p>
<p class="note" dir="ltr" lang="en">This paragraph overrides to left to right.</p>
</body>
</html>In the first paragraph the padding and the red border sit on the right. In the second, the local dir="ltr" puts them on the left. Both come from the same rule.
You can also flip an existing page without editing files. In the browser developer tools, select the <html> element and edit its attributes to add dir="rtl". The page reflows immediately.
Direction in HTML versus CSS
Prefer the HTML dir attribute over the CSS direction property. Direction is part of the document's meaning, and the attribute keeps working when stylesheets fail to load. The attribute also enables the browser's bidirectional handling of mixed content, such as a Latin brand name inside Arabic text. Use CSS direction only for cases where the markup cannot change.
Forcing a vertical writing mode
Direction is only half the story. Vertical writing mode is set in CSS with writing-mode. To test it, apply it to a container or to the whole page.
.vertical {
writing-mode: vertical-rl;
inline-size: 20rem;
block-size: 10rem;
}With vertical-rl, the inline axis runs top to bottom, so inline-size: 20rem makes the box 20rem tall. The block axis runs right to left, so block-size: 10rem makes it 10rem wide. Lines stack from the right edge toward the left.
The table shows where block-start and inline-start land for the combinations you will test most.
| `writing-mode` | `direction` | Inline-start | Block-start |
|---|---|---|---|
| `horizontal-tb` | `ltr` | left | top |
| `horizontal-tb` | `rtl` | right | top |
| `vertical-rl` | `ltr` | top | right |
| `vertical-rl` | `rtl` | bottom | right |
| `vertical-lr` | `ltr` | top | left |
Use this table as the expected result when you inspect a page. If a badge that should be at block-start, inline-end appears somewhere else, a physical value is involved.
.badge {
position: absolute;
inset-block-start: 0;
inset-inline-end: 0;
}In horizontal-tb with rtl, this badge is at the top left. In vertical-rl with ltr, block-start is the right edge and inline-end is the bottom, so the badge sits at the bottom right.
A reusable test harness
Instead of editing the page by hand each time, build a small harness that switches modes with attributes. This also gives you a base for visual regression tests.
<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Mode switcher</title>
<style>
html[data-wm="vertical-rl"] { writing-mode: vertical-rl; }
html[data-wm="vertical-lr"] { writing-mode: vertical-lr; }
.card {
inline-size: min(100%, 40ch);
margin-inline: auto;
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
border-inline-start: 4px solid steelblue;
}
</style>
</head>
<body>
<p>
<button data-dir="ltr">LTR</button>
<button data-dir="rtl">RTL</button>
<button data-wm="horizontal-tb">Horizontal</button>
<button data-wm="vertical-rl">Vertical RL</button>
<button data-wm="vertical-lr">Vertical LR</button>
</p>
<div class="card">Sample card content for the layout check.</div>
<script>
const root = document.documentElement;
document.addEventListener("click", (event) => {
const button = event.target.closest("button");
if (!button) return;
if (button.dataset.dir) root.setAttribute("dir", button.dataset.dir);
if (button.dataset.wm) root.dataset.wm = button.dataset.wm;
});
</script>
</body>
</html>Open the file and click through each combination. The card's border should always sit on the inline-start side, and its asymmetric padding should flip with it.
Reading the results
For each mode, check these points:
- The start-side decoration (border, padding, icon) is on the start side of the text.
- Spacing between siblings is on the correct axis. A vertical stack in English should become a horizontal row of columns in
vertical-rl, and the gaps should follow. - Text alignment follows the direction.
text-align: startshould align to the right in RTL. - Floated items go to the start side, with their gap on the opposite side.
- Nothing overflows or gets clipped, since
inline-sizenow maps to a different physical dimension.
.figure {
float: inline-start;
margin-inline-end: 1rem;
text-align: start;
}If the figure floats to the wrong side in RTL, search for float: left or text-align: left that may be overriding this rule.
Finding leftover physical properties
Visual checks catch what you look at. A search catches the rest. Run a text search over your stylesheets for physical names.
grep -nE "(margin|padding|border)-(left|right|top|bottom)|\b(left|right|top|bottom)\s*:|text-align:\s*(left|right)|float:\s*(left|right)|clear:\s*(left|right)" src/*.cssEach hit is a candidate, not necessarily a bug. Section "When to keep physical values" in an earlier chapter lists legitimate uses, such as a drop shadow tied to a light source. Review every hit and either convert it or leave a comment explaining why it stays physical.
A linter can enforce this over time. Stylelint's plugin/use-logical-properties-and-values (from the stylelint-use-logical plugin) reports physical properties, and it can fix many of them automatically.
Computed values show the truth
The developer tools show the computed style for each element. Inspect the badge above in RTL. The computed right and left values are physical numbers, so you can confirm which side the logical property resolved to. A value of 0px for left and auto for right tells you the badge landed on the left.
Browsers also let you highlight the box model. Padding and margin overlays show the physical result of each logical declaration, which is the quickest way to confirm a mapping against the table above.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Border stays on the left in RTL | `border-left` used, or a later physical rule overrides the logical one | Use `border-inline-start` and check rule order |
| Box is clipped in vertical mode | A fixed `height` or `width` assumed a horizontal flow | Use `block-size` and `inline-size`, and allow growth |
| Arrow icon points the wrong way | Icon is an image or glyph with a fixed direction | Mirror it with a rule scoped to `:dir(rtl)` |
| Slide animation goes the wrong way | `translateX` is physical | Switch the sign by direction, for example with a custom property |
| Shadow or gradient looks wrong | Physical offsets are intentional | Decide whether the light source should flip |
The :dir() pseudo-class matches elements by their resolved direction, including direction inherited from ancestors. It is the right selector for icon mirroring.
.icon-next:dir(rtl) {
transform: scaleX(-1);
}Here the mirror is deliberate. Mirror only directional icons such as arrows. Do not mirror logos, clocks, or media controls that have fixed conventions.
Automating the check
Once you have the harness, automate it. A browser test runner can load the page in each mode and take a screenshot to compare against a stored baseline.
import { test, expect } from "@playwright/test";
const modes = [
{ name: "ltr", dir: "ltr", wm: "horizontal-tb" },
{ name: "rtl", dir: "rtl", wm: "horizontal-tb" },
{ name: "vertical-rl", dir: "ltr", wm: "vertical-rl" },
];
for (const mode of modes) {
test(`card layout in ${mode.name}`, async ({ page }) => {
await page.goto("http://localhost:3000/");
await page.evaluate(({ dir, wm }) => {
document.documentElement.setAttribute("dir", dir);
document.documentElement.style.writingMode = wm;
}, mode);
await expect(page.locator(".card")).toHaveScreenshot(`card-${mode.name}.png`);
});
}This assumes a dev server on port 3000 serving the page and the Playwright test runner installed. The first run creates the baseline images, and later runs fail when the layout changes.
Practical checklist
- Test at least four states: LTR, RTL,
vertical-rl, and one mixed case with a nesteddiroverride. - Use real text in the target language when you can. Placeholder Latin text hides line-breaking and font problems.
- Test with long strings. Translated labels are often longer than English ones, and vertical text needs room along its inline axis.
- Keep physical exceptions documented in the stylesheet.
Exercise
A badge uses position: absolute; inset-block-start: 0; inset-inline-end: 0;. In which physical corner does it appear when the page has writing-mode: vertical-rl and dir="ltr"? Which declaration would you search for if it appeared in the wrong corner?
Show answer
In vertical-rl with ltr, block-start is the right edge and inline-end is the bottom edge, so the badge sits at the bottom right. If it appeared elsewhere, search for physical top, right, bottom or left declarations that override the logical ones, and check rule order.
Exercise
You run the grep command and it reports box-shadow: 4px 4px 8px rgba(0,0,0,.3) is not matched, but text-align: left is. Should you convert text-align: left in every case?
Show answer
No. Convert it to text-align: start when the text should follow the reading direction, which is the usual case. Keep left only when the alignment is tied to the physical screen, such as a caption that must align with a fixed image on the left, and add a comment saying why.