Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readCSS

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: 500

Under 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-left and margin-right become margin-inline-start and margin-inline-end.
  • padding-left and padding-right become padding-inline-start and padding-inline-end.
  • border-left and border-right become border-inline-start and border-inline-end.
  • left and right become inset-inline-start and inset-inline-end.
  • text-align: left and right become text-align: start and end.
.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.