Logical properties

Topic 4 of 4 in Normal flow6 min readBeginnerFree course

Overview

margin-left: 1rem means “space on the left” — but in Arabic, Hebrew or Persian, text runs right to left, and that space belongs on the right. Logical properties describe sides by their role in the text flow rather than their physical position, so one stylesheet works in every language. They’re supported in every modern browser, and many teams now use them by default.

Two axes

Every piece of text has two directions:

  • the inline axis — the direction text runs along a line (left→right in English),
  • the block axis — the direction lines and blocks stack (top→bottom in English).

Each axis has a start and an end:

In English (horizontal-tb, ltr), block-start is the top, block-end is the bottom, and inline runs left to right, so inline-start is the left. In Arabic (horizontal-tb, rtl), block-start is still the top, but inline runs right to left, so inline-start is on the right. In vertical Japanese (vertical-rl), inline-start is at the top and blocks run right to left, so block-start is on the right. Example: margin-inline-start: 1rem; padding-block: 2rem; inline-size: 20rem.

Physical (English) Logical
top block-start
bottom block-end
left inline-start
right inline-end
width inline-size
height block-size

The logical properties

CSS
/* margins, padding, borders */
margin-block-start: 2rem;         /* margin-top in English */
margin-inline: auto;              /* left and right */
padding-block: 1rem;              /* top and bottom */
padding-inline: 1rem 2rem;        /* start, end */
border-inline-start: 4px solid;   /* the left border in LTR, right in RTL */
border-start-start-radius: 8px;   /* top-left corner in LTR */

/* sizes */
inline-size: 20rem;               /* width */
max-inline-size: 65ch;            /* max-width */
min-block-size: 100dvh;           /* min-height */

/* positioning */
inset-block-start: 0;             /* top */
inset-inline-end: 1rem;           /* right in LTR */
inset: 0;                         /* all four sides */

/* text and floats */
text-align: start;
float: inline-start;

The two-value shorthands (margin-inline, padding-block, inset-inline…) are handy even if you never support right-to-left languages: margin-inline: auto is shorter than margin-left: auto; margin-right: auto.

Right-to-left in practice

Set the direction in HTML, not CSS, so it’s part of the content:

HTML
<html lang="ar" dir="rtl">

With logical properties, the whole layout mirrors automatically: margins, padding, borders, text alignment, floats and positioned elements flip. With physical properties, you’d need a second set of overrides for every rule:

CSS
/* the old way */
.card { border-left: 4px solid; }
[dir="rtl"] .card { border-left: none; border-right: 4px solid; }

/* the logical way */
.card { border-inline-start: 4px solid; }

Mixed content works too: an English page can contain an Arabic quote with dir="rtl", and logical properties inside it adapt.

Vertical writing modes

writing-mode changes the direction lines run. It’s used for Chinese, Japanese, Korean and Mongolian vertical text, and occasionally for design effects like rotated headings:

CSS
.vertical-label {
  writing-mode: vertical-rl;   /* lines run top to bottom; new lines go right to left */
}

In a vertical writing mode, inline-size becomes the height and block-size becomes the width — logical properties keep making sense.

Flexbox and grid are already logical

Flexbox and grid were designed after writing modes, so their alignment keywords are logical:

  • justify-content: start and flex-direction: row follow the inline direction — in RTL, a row starts on the right,
  • grid line 1 is on the inline-start side,
  • align-items: start follows the block direction.

Only left and right keywords, where they exist, are physical.

When to use physical properties

Logical properties describe the relationship between content and its direction. Physical properties still make sense for things tied to the screen, not the text:

  • a shadow that should always fall to the bottom-right,
  • an element pinned to the physical bottom of the screen,
  • transforms and animations that move things in screen space.

Adopting them

You don’t need to convert an existing codebase in one go. For new CSS:

  • use margin-inline/padding-inline/margin-block/padding-block instead of left/right/top/bottom pairs,
  • use inline-size/max-inline-size where width relates to text measure,
  • use inset-* for positioning,
  • use text-align: start.

This course uses logical properties where they make the most difference, and physical ones where they’re clearer for learning.

Think in flow, not in screen directions: “space before the next paragraph” and “space at the start of the line” are what you actually mean — logical properties let you say it.

Exercise

A card that works in Arabic

This card puts an icon on the left with a gap and a coloured border on the left edge — written with physical properties. Rewrite it with logical properties, then add dir="rtl" to the second card (Arabic text) and check that the icon, gap and border all flip to the right without any extra CSS.

HTML
<div class="card"><span class="icon">★</span> Featured course</div>
<div class="card" dir="rtl"><span class="icon">★</span> دورة مميزة</div>

<style>
  .card {
    border-left: 4px solid #2d5bd7;
    padding: 12px 16px 12px 12px;
    margin-bottom: 1rem;
    width: 20rem;
    text-align: left;
  }
  .icon {
    margin-right: 8px;
    float: left;
  }
</style>

Summary

  • Physical properties (left, right, top, bottom) are fixed to the screen; logical properties follow the text direction.
  • The inline axis is the direction text runs; the block axis is the direction lines stack.
  • margin-inline, padding-block, inset-inline-start, inline-size and friends map to physical sides automatically.
  • Right-to-left languages and vertical writing modes then work without extra CSS.
  • Flexbox and grid are already logical: start and end follow the writing direction.