Use logical properties for RTL layouts
Use logical CSS properties for spacing and offsets that should follow the reading direction.
You gave a card a 100px indent with margin-left, then switched the page to dir="rtl" for Arabic or Hebrew. The text now starts at the right edge, but the indent is still on the left. Use a logical property for spacing that should follow the text direction.
Measure the wrong side
Install playwright, run npx playwright install chromium, and save the script as measure.mjs. Run node measure.mjs to compare the boxes’ left edges under both directions.
import { chromium } from 'playwright'
const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 800, height: 600 } })
const html = (dir) => `<!doctype html><html dir="${dir}">
<style>
body { margin: 0; }
.box { width: 200px; height: 40px; }
#physical { margin-left: 100px; }
#logical { margin-inline-start: 100px; }
</style>
<div class="box" id="physical"></div>
<div class="box" id="logical"></div>
</html>`
for (const dir of ['ltr', 'rtl']) {
await page.setContent(html(dir))
const lefts = await page.evaluate(() =>
['physical', 'logical'].map((id) => document.getElementById(id).getBoundingClientRect().left),
)
console.log(dir, 'margin-left:', lefts[0], 'margin-inline-start:', lefts[1])
}
await browser.close()
// prints ltr margin-left: 100 margin-inline-start: 100
// prints rtl margin-left: 600 margin-inline-start: 500Under ltr the two boxes match. Under rtl the 200px box sits flush against the right edge of the 800px viewport at 600, and margin-left adds nothing you can see. margin-inline-start leaves a 100px gap on the right, so the box starts at 500.
Rename the side
With the default horizontal writing mode, inline-start is left in ltr and right in rtl. In vertical writing modes the inline axis runs vertically. inline-end is the opposite side. Swap the physical side for one of those words and the browser picks the edge.
margin-leftandmargin-rightbecomemargin-inline-startandmargin-inline-end.padding-leftandpadding-rightbecomepadding-inline-startandpadding-inline-end.border-leftandborder-rightbecomeborder-inline-startandborder-inline-end.leftandrightbecomeinset-inline-startandinset-inline-end.text-align: leftandrightbecometext-align: startandend.
.card {
margin-inline-start: 100px;
padding-inline-start: 50px;
border-inline-start: 10px solid;
text-align: start;
}I ran each pair from the list under both directions. In rtl the computed padding-right was 50px and border-right-width was 10px for the start forms.
For a 20px-wide box positioned against the 800px viewport, inset-inline-start: 30px puts its left edge at 30px in LTR and 750px in RTL. text-align: start moved the text to the right side while left kept it on the left, and margin-inline-end became a computed margin-left of 100px.
Use the logical names for anything that should follow reading direction, such as an indent, an icon gap or an accent border.