RTL Layout Support — i18n

Set lang and dir="rtl" on <html> from the locale (ar, he, fa, ur mean RTL), with the attribute, not CSS direction. Flex and grid follow the direction on their…

Mrs. Sri prints an Arabic version of the Mist Homestay brochure for guests from Egypt, and nearly everything changes place. An Arabic brochure opens from what is, to us, the back cover. The photo moves to the right, and every line starts on the right. But inside, the price "USD 70.00" and the date "2026-12-12" are still written left to right. Writing read from right to left like this is called RTL (right-to-left): Arabic, Hebrew, Persian and Urdu. Supporting RTL means the whole page is mirrored, not just the letters. The brochure's two rules are what this lesson is about: the layout is mirrored, the numbers aren't.

dir="rtl" on the html tag

A page's direction is set by the HTML dir attribute. Its values are ltr (left to right, the default), rtl (right to left) or auto (the browser guesses from the content). Put it on <html>, next to lang:

<html lang="ar-EG" dir="rtl">

In an app that can switch languages, set both from the locale:

const RTL = ['ar', 'he', 'fa', 'ur'];

function setDirection(locale) {
  const language = new Intl.Locale(locale).language; // 'ar-EG' becomes 'ar'
  document.documentElement.lang = locale;
  document.documentElement.dir = RTL.includes(language) ? 'rtl' : 'ltr';
}

setDirection('ar-EG'); // <html lang="ar-EG" dir="rtl">

Flex and grid follow the direction on their own

The good news: modern layout already reads the direction. display: flex with flex-direction: row puts the first child at the start of the row, and under dir="rtl" the start of the row is the right. Grid is the same: the first column of grid-template-columns appears on the right.

Mrs. Sri's length-of-stay buttons (.nights { display: flex; gap: 8px; }) in ar-EG line up as ليلة واحدة, ليلتان, ٣ ليالٍ from right to left. We measured where the buttons sit in Chromium 147, Firefox 146 and WebKit 26.4: in all three the first button is the rightmost, with no extra CSS. What does break it is writing flex-direction: row-reverse to "flip" the Arabic layout: flex already flipped it, so it ends up flipped twice.

CSS logical properties: start and end, not left and right

The trouble is the properties that name a physical side: left, right, margin-left, padding-right, border-left, text-align: left. Left stays left, whatever the writing direction. Logical properties name a side by the writing direction instead. Inline is the direction of a line (left to right, or right to left), block is the direction lines stack in (top to bottom). start is the beginning, end is the end.

Here is Mrs. Sri's card CSS, before and after. We read the results with getComputedStyle in Chromium 147, Firefox 146 and WebKit 26.4, and all three agree:

Mrs. Sri writes In ar-EG Logical version In ar-EG
padding-left: 20px the 20px padding stays on the left padding-inline-start: 20px the 20px padding is on the right
border-left: 4px solid the green stripe stays on the left border-inline-start: 4px solid the green stripe is on the right
right: 12px (the Favourite badge) the badge stays right, over the start of the title inset-inline-end: 12px the badge is on the left
margin-right: 6px (icon gap) the gap is on the wrong side margin-inline-end: 6px the gap is left of the icon

So on Mrs. Sri's card, the text and the order of the flex buttons move on their own, but the green stripe doesn't, because border-left says left.

/* Before: physical left and right */
.card  { position: relative; padding: 16px 16px 16px 20px; border-left: 4px solid #15803d; }
.badge { position: absolute; top: 12px; right: 12px; }
.icon  { margin-right: 6px; }

/* After: start and end, one stylesheet for both directions */
.card  { position: relative; padding-block: 16px; padding-inline: 20px 16px; border-inline-start: 4px solid #15803d; }
.badge { position: absolute; top: 12px; inset-inline-end: 12px; }
.icon  { margin-inline-end: 6px; }

Icons: mirror the arrows, not the clock or the logo

Icons that point in a direction must be mirrored, because "back" in RTL means to the right. Icons that draw a thing must not.

Icon In RTL Why
The "All rooms" arrow mirrored, pointing right Back means toward the start, and the start is on the right
The clock on the check-in line unchanged Clock hands turn the same way everywhere. Mirrored, 2 o'clock becomes 10 o'clock.
The Mist Homestay logo unchanged A logo is a brand; its lettering would read backwards
A checkmark, a play button unchanged Their shape doesn't point along the reading direction
.back .icon:dir(rtl) { transform: scaleX(-1); }

Numbers stay left to right: the bidi algorithm

Text that mixes right-to-left and left-to-right is called bidi (bidirectional). Browsers order it with the Unicode Bidirectional Algorithm (UAX #9). Every character has a direction type: Arabic letters are right to left, Latin letters left to right, while digits and punctuation follow their neighbours. A run of digits is always written left to right, but where that run sits in the sentence is decided by its neighbours.

We put three sentences on a dir="rtl" page, then ordered their characters by where they sit on screen, from left to right (Arabic letters replaced by _). The results are the same in Chromium 147, Firefox 146 and WebKit 26.4:

Written What shows, read from the left Why
الدفع عند الوصول: USD 70.00 USD 70.00 :_ _ _ "USD" is Latin, so "USD 70.00" is one left-to-right piece. Safe.
تسجيل الوصول: 2026-12-12 12-12-2026 :_ _ Digits after Arabic letters are treated as Arabic numbers. Each group stays left to right, but the groups are ordered right to left, so the year moves to the right end.
The guest's note: Arriving around 22.00, thanks! !Arriving around 22.00, thanks The paragraph inherits rtl, so the exclamation mark at the end of the English sentence moves to the far left.

Intl output for ar-EG is safe too: "‏٧٠٫٠٠ US$" and "١٢‏/١٢‏/٢٠٢٦" carry the invisible mark U+200F (RLM, right-to-left mark) that keeps their order. What breaks is a plain ISO date and Latin text from a guest dropped straight into an RTL page.

bdi and dir="auto" for text from guests

<bdi> (bidi isolate) fences off its content: its direction comes from its own content, and it doesn't pull its neighbours along. dir="auto" makes an element pick its direction from its first strong letter: a Latin letter means ltr, an Arabic letter means rtl.

<p>تسجيل الوصول: <bdi>2026-12-12</bdi></p>
<p dir="auto">Arriving around 22.00, thanks!</p>
<p dir="auto">سأصل حوالي الساعة 22.00، شكرًا!</p>

Check in three browsers

The bidi algorithm in the three browser engines gave the same order for every example above, but Arabic fonts, letter joining and line breaking can differ. So look at the result in Chromium, Firefox and WebKit (Safari's engine). The quick way: in DevTools, add dir="rtl" to <html>. The repeatable way: one Playwright script, three screenshots.

// check-rtl.mjs, run with: node check-rtl.mjs
import { chromium, firefox, webkit } from 'playwright';

for (const engine of [chromium, firefox, webkit]) {
  const browser = await engine.launch();
  const page = await browser.newPage({ locale: 'ar-EG' });
  await page.goto('http://localhost:5173/ar/terrace-room');
  await page.screenshot({ path: `card-ar-${engine.name()}.png`, fullPage: true });
  await browser.close();
}

engine.name() gives chromium, firefox or webkit, so each browser gets its own file. What to check in each screenshot: the green stripe on the right, the badge on the left, the back arrow pointing right, the clock still at 2 o'clock, the price and date readable in one piece, no text running over other text.

⚠️ Why is the Arabic page still half mirrored?

TL;DR: Set lang and dir="rtl" on <html> from the locale (ar, he, fa, ur mean RTL), with the attribute, not CSS direction. Flex and grid follow the direction on their own, so don't use row-reverse. Physical properties (padding-left, border-left, right, margin-right, text-align: left) become logical properties (padding-inline-start, border-inline-start, inset-inline-end, margin-inline-end, text-align: start): one stylesheet for both directions. Mirror direction icons with :dir(rtl), never the clock or the logo. Numbers are always left to right, but a plain ISO date after Arabic letters shows as 12-12-2026; wrap it in <bdi>, and use dir="auto" for text from guests. Check the result with screenshots in Chromium, Firefox and WebKit.

⚡ dir on the html tag

Write:

<html lang="ar-EG" dir="rtl">
Guess first: with only lang="ar-EG", does the page flip?

No. lang names the language, dir names the direction. lang="ar-EG" alone flips nothing.

Meaning: The dir="rtl" attribute on <html> aligns text right and starts lines on the right. Use the attribute, not CSS direction: rtl.

⚡ Start and end, not left and right

Write:

.card  { position: relative; padding-block: 16px; padding-inline: 20px 16px; border-inline-start: 4px solid #15803d; }

Meaning: border-left stays on the left, whatever the writing direction. Logical properties name a side by the writing direction, so one stylesheet works for both directions. Flex and grid already follow the direction, so don't use row-reverse.

⚡ Mirror the arrow, not the clock

Write:

.back .icon:dir(rtl) { transform: scaleX(-1); }

Meaning: Icons that point in a direction are mirrored, because "back" in RTL means to the right. The clock, the logo and the checkmark stay as they are.

⚡ Numbers stay left to right

Meaning: An ISO date after Arabic letters, such as تسجيل الوصول: 2026-12-12, shows as 12-12-2026. Wrap it in <bdi>, and use dir="auto" for text a guest wrote.

What you will learn