Skip to main content

Modern CSS Authoring

Logical Properties

Written by Updated Logical Properties exercises

Write layouts that adapt to right-to-left text and vertical writing without duplicating physical left and right rules. It replaces physical edges with inline and block intent, introduces logical sizes and insets, and explains direction and vertical writing modes.

margin-left assumes text runs left to right. Logical properties like margin-inline-start say "at the start of the line" instead, so the same CSS works for Arabic, Hebrew and vertical Japanese without changes. They are also simply shorter for common jobs.

In English, inline means left and right, and block means top and bottom. start is the left, end is the right. In a right-to-left language, start becomes the right side automatically. Alignment works the same way with text-align: start, see CSS Text Alignment, and the block/inline idea comes from Normal Flow.

Example

css

.callout {
  border-inline-start: 0.3rem solid #0f766e;
  padding-inline: 1rem;
  padding-block: 0.75rem;
  margin-block: 1.5rem;
}

.container {
  max-inline-size: 70rem;
  margin-inline: auto;
}

The callout gets its border on the side where lines start, horizontal padding with padding-inline and vertical spacing with margin-block. The container uses max-inline-size (the logical max-width) and margin-inline: auto to center itself, one line instead of two.

Vertical text

Example

css

.vertical-label {
  writing-mode: vertical-rl;
  text-orientation: mixed;
}

writing-mode: vertical-rl turns text so it runs top to bottom, the way it is written in some East Asian layouts. It is also handy for vertical labels on charts and tables. Logical properties follow along: block and inline swap, so the same spacing rules still make sense.

  • Use the HTML dir attribute for text direction; do not use CSS direction as a substitute for document semantics.
  • inset-inline and inset-block replace paired physical offsets.
  • Logical border radius and float values are available when a component must follow writing direction.

Logical Property Reference

Logical propertyPhysical relationshipExample
inline-sizeWidth in horizontal writing modes.inline-size: 20rem
block-sizeHeight in horizontal writing modes.block-size: auto
margin-inlineMargins on both inline edges.margin-inline: auto
inset-inline-startInset at the inline starting edge.inset-inline-start: 1rem

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Logical Properties example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.callout {
  border-inline-start: 0.3rem solid #0f766e;
  padding-inline: 1rem;
  padding-block: 0.75rem;
  margin-block: 1.5rem;
}

.container {
  max-inline-size: 70rem;
  margin-inline: auto;
}

.vertical-label {
  writing-mode: vertical-rl;
  text-orientation: mixed;
}
  </style>
</head>
<body>
  <main class="container"><aside class="callout">This border follows the inline start edge.</aside><p class="vertical-label">Vertical label</p></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
margin-inline87876614.1
writing-mode48124110.1

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Good to know

  • Set the text direction with the HTML dir attribute, like <html dir="rtl">, not with CSS. Logical CSS then follows it.
  • You do not have to convert everything at once. Start with new components, and convert padding, margin and borders together, so a component never mixes the two systems.
  • inset-inline-start is the logical left for positioned elements.

Logical Properties exercises

Space on the inline axis

Give both paragraphs a margin of 20px at the start and the end of the line, with margin-inline.

The HTML your code works on
<p class="ltr">Left to right</p>
<p class="rtl" dir="rtl">مرحبا</p>

Result

Check your answer to see it here.