Ronalds Vilciņš

Building for Right-to-Left Layouts With Logical CSS

A lot of CSS quietly assumes that content starts on the left and moves to the right. You write:

margin-left: 1rem;
padding-right: 2rem;
left: 0;

That is fine for languages such as English and Latvian. But languages such as Arabic and Hebrew commonly use right-to-left writing direction. The layout should often mirror.

Modern CSS logical properties make that much easier. Evil Martians has also highlighted RTL support as an area where small CSS choices can prevent a lot of later work.

Switch the same card between LTR and RTL
View code
HTML
<label>Text direction <select id="direction"><option value="ltr">Left to right</option><option value="rtl">Right to left</option></select></label>
<article dir="ltr"><span class="badge" aria-hidden="true">RV</span><div><h2>Account settings</h2><p lang="ar">إعدادات الحساب</p><p>Spacing and the accent border follow the inline start.</p></div></article>
CSS
button, select { padding: .35rem .6rem; border: 1px solid var(--clg); background: var(--cw); color: var(--cb); }
input[type="range"] { width: min(100%, 18rem); vertical-align: middle; }
h2, h3 { font-size: 1rem; }
.note { color: var(--clg); }

article { display: flex; gap: 1rem; margin-block-start: 1rem; padding-inline: 1rem; padding-block: 1rem; border-inline-start: 4px solid var(--cl); background: var(--cg); }
.badge { align-self: start; padding: .5rem; border: 1px solid var(--clg); }
h2 { margin: 0; }
p { margin-block: .5rem 0; }
JavaScript
document.querySelector('#direction').addEventListener('change', event => {
  document.querySelector('article').dir = event.target.value;
});

Physical vs logical properties

Physical CSS says:

left
right
top
bottom

Logical CSS says:

inline start
inline end
block start
block end

For normal English text:

inline-start = left
inline-end   = right

For RTL text:

inline-start = right
inline-end   = left

The CSS can adapt automatically.

Margin example

Instead of:

.icon {
  margin-right: 0.5rem;
}

use:

.icon {
  margin-inline-end: 0.5rem;
}

Now the gap appears after the icon in the current writing direction.

Padding

Instead of:

.card {
  padding-left: 2rem;
  padding-right: 2rem;
}

you can write:

.card {
  padding-inline: 2rem;
}

For vertical spacing:

padding-block: 1rem;

Positioning

Instead of:

.badge {
  right: 1rem;
}

use:

.badge {
  inset-inline-end: 1rem;
}

Now the badge can move to the opposite side in RTL layouts.

Borders

Instead of:

border-left: 4px solid blue;

use:

border-inline-start:
  4px solid blue;

A callout border now follows the start side of the text direction.

Text direction comes from HTML

The document can declare:

<html dir="rtl">

or:

<html dir="ltr">

Individual sections can also set direction when necessary. CSS logical properties then follow that writing mode.

Not everything should mirror

An icon such as:

play
download
camera

usually means the same thing in both directions. A directional arrow may need to mirror. For example:

[dir="rtl"] .arrow {
  transform: scaleX(-1);
}

Do this only for icons whose direction carries meaning.

Flexbox already understands direction

A normal:

display: flex;

layout follows the writing direction in many cases. Avoid forcing:

flex-direction: row-reverse;

only to imitate RTL unless you understand how it interacts with source order. The DOM should still remain logical.

Test with real text

Do not test RTL support by adding:

direction: rtl;

to English text and assuming everything is fine. Use real Arabic or Hebrew content. Different scripts expose spacing, font, and alignment issues that fake RTL testing can miss.

Long text matters too

Internationalization is not only left vs right. Translations can be:

Defensive layout and logical properties work well together.

Useful logical properties

The ones I use most:

margin-inline
margin-block
padding-inline
padding-block
inset-inline
inset-block
border-inline
border-block
inline-size
block-size

You do not need to replace every physical property immediately. Start with components where direction really matters.

A more future-friendly card

Instead of:

.card {
  padding-left: 1rem;
  padding-right: 1rem;
  margin-bottom: 2rem;
}

write:

.card {
  padding-inline: 1rem;
  margin-block-end: 2rem;
}

The intent becomes:

space on the inline axis
space after the component in block flow

rather than assuming a specific physical direction.

Logical properties reduce direction-specific overrides

Logical properties are not only for sites that already have an Arabic version. They make the stylesheet describe layout in a more general way. If a site ever gains another language or writing mode, you have fewer left/right assumptions to undo.

Using logical properties now leaves fewer left/right rules to change when another language is added.