# Logical Properties & i18n

> Stop using margin-left and margin-right. Use logical properties to build layouts that adapt to any language automatically.

Subject: CSS | Lesson 41 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/logical-properties-and-i18n
Previous: [Anchor Positioning & color-mix()](https://codewithanimation.in/css/anchor-positioning.md) | Next: [Dark Mode & color-scheme](https://codewithanimation.in/css/dark-mode-and-color-scheme.md)

## Physical vs Logical

For decades, we styled elements using physical coordinates: `margin-left`, `padding-top`, `border-bottom`, and `width` / `height`.

The problem is that physical properties are hardcoded to the screen. If you add `margin-left: 20px` to an icon so it sits nicely next to text, it looks great in English (Left-to-Right).

But if you translate that page to Arabic (Right-to-Left), the icon still has margin on the left, but the text is now on the right. The layout breaks.

CSS Logical Properties replace the physical compass (top, right, bottom, left) with a flow-relative compass (block-start, inline-end, block-end, inline-start).

## Automatic Internationalization (i18n)

Logical properties adapt to the `dir` (direction) and `writing-mode` of the document automatically.

Instead of `margin-left`, you use `margin-inline-start`. In English, `inline-start` resolves to the left. In Arabic, the browser automatically resolves `inline-start` to the right. In vertical Japanese, `inline-start` resolves to the top.

This means you can write one stylesheet for your app, and it will support global languages with zero extra effort. No more complex RTL CSS overrides.

## The new vocabulary

`block`: The axis that blocks stack on. In English, blocks stack from top to bottom. Therefore, `block-start` is Top, and `block-end` is Bottom.

`inline`: The axis that text flows along. In English, text flows from left to right. Therefore, `inline-start` is Left, and `inline-end` is Right.

So, `padding-top` becomes `padding-block-start`. `border-right` becomes `border-inline-end`. `width` becomes `inline-size`. `height` becomes `block-size`.

## Common mistake: Muscle memory

The primary pain point is muscle memory. We are deeply conditioned to type `margin-top`. Typing `margin-block-start` feels verbose.

However, modern CSS linting tools and Tailwind (`ms-4` for margin-inline-start) abstract away the verbosity, making it the standard best practice for all modern codebases.

## Tip: Shorthands save time

Just like physical properties have shorthands (`margin: 10px 20px`), logical properties have them too. `padding-inline: 20px` sets both the start and end padding along the inline axis, replacing `padding-left` and `padding-right`.

## Try it: Try it

Toggle the language modes below. Watch how the layout built with physical properties breaks when translated, while the logical layout adapts flawlessly.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/logical-properties-and-i18n

## Check yourself

### Question 1

Which logical property replaces `margin-left` in a standard English (LTR) document?

- A. margin-inline-start
- B. margin-block-start
- C. margin-inline-end
- D. margin-start

Correct answer: A

In Left-To-Right languages, the inline axis starts on the left. Thus, `inline-start` replaces `left`.

- Why not B: `block-start` corresponds to the top edge in horizontal writing modes.
- Why not C: `inline-end` corresponds to the right edge in LTR languages.
- Why not D: While `margin-start` was an early draft, the standard is `margin-inline-start`.

### Question 2

If you apply `margin-inline-start: 1rem;` to an element, and the document is set to Arabic (`dir="rtl"`), where will the margin appear?

- A. On the right side.
- B. On the left side.
- C. On both sides.
- D. On the top.

Correct answer: A

Because Arabic is read Right-To-Left, the start of the inline axis is on the right. The browser automatically places the margin on the right.

- Why not B: It would appear on the left in English, but in RTL languages, the inline axis starts on the right.
- Why not C: That would be `margin-inline`.
- Why not D: It would only appear on the top if the `writing-mode` was vertical-rl.

### Question 3

What is the logical equivalent of `width` in a standard horizontal document?

- A. inline-size
- B. block-size
- C. inline-width
- D. cross-size

Correct answer: A

Since text flows along the inline axis, the 'width' of a block of text is its `inline-size`. If the language becomes vertical, `inline-size` seamlessly controls the vertical dimension.

- Why not B: `block-size` replaces `height` in horizontal documents.
- Why not C: The standard name drops 'width' and 'height' entirely to avoid confusion, using 'size'.
- Why not D: Cross-size is Flexbox terminology, not logical property terminology.

## Remember this

- Logical properties adapt automatically to the document's direction and writing-mode.
- `inline` replaces Left/Right. `inline-start` is where you start reading a line.
- `block` replaces Top/Bottom. `block-start` is where the first block of text appears.
- Use `inline-size` and `block-size` instead of `width` and `height`.
