Logical Properties

What You'll Learn

  • Understanding inline and block axes in different writing modes
  • Using logical properties for internationalization-friendly CSS
  • Replacing physical properties (top, right, bottom, left) with logical equivalents
  • How logical properties adapt automatically to LTR, RTL, and vertical writing modes
  • Using logical values like start and end for text alignment
  • Building truly international websites without separate stylesheets

Introduction to Logical Properties

Logical properties provide a way to write CSS that automatically adapts to different writing directions (left-to-right, right-to-left) and writing modes (horizontal, vertical). Instead of referring to physical directions like left or right, logical properties refer to the start and end of the inline and block axes.

This is crucial for internationalization. A website in English (LTR) and Arabic (RTL) can use the same CSS with logical properties, and the layout will automatically flip appropriately. No need for separate RTL stylesheets or complex overrides.

Understanding Inline and Block Axes

Every writing system has two axes: the inline axis (direction text flows within a line) and the block axis (direction lines stack). In English, inline is horizontal (left-to-right) and block is vertical (top-to-bottom). In traditional Japanese, inline is vertical and block is horizontal.

Horizontal Writing (English, Spanish, French)

  • Inline axis: Horizontal (left → right in LTR, right → left in RTL)
  • Block axis: Vertical (top → bottom)

Vertical Writing (Traditional Japanese, Chinese)

  • Inline axis: Vertical (top → bottom)
  • Block axis: Horizontal (typically right → left)

Visual Demonstration:

Margin: Physical vs Logical

Physical margin properties (margin-left, margin-right) always refer to the same physical direction, regardless of writing mode. Logical margin properties adapt to the writing direction.

Syntax

/* ❌ Physical properties - don't adapt to direction */ .box { margin-left: 2rem; margin-right: 0.5rem; } /* ✅ Logical properties - adapt automatically */ .box { margin-inline-start: 2rem; /* Start of inline axis */ margin-inline-end: 0.5rem; /* End of inline axis */ }

Example

<div class="box"> This box has logical margins that adapt to writing direction. </div>

Rendered Result (LTR vs RTL):

All Logical Margin Properties

/* Individual properties */ margin-block-start /* Physical: margin-top (in horizontal writing) */ margin-block-end /* Physical: margin-bottom (in horizontal writing) */ margin-inline-start /* Physical: margin-left (LTR) or margin-right (RTL) */ margin-inline-end /* Physical: margin-right (LTR) or margin-left (RTL) */ /* Shorthand */ margin-block: 1rem 2rem; /* block-start block-end */ margin-inline: 1rem 2rem; /* inline-start inline-end */

Padding: Logical Properties

Padding follows the same pattern as margin. Use padding-block and padding-inline to create padding that adapts to writing mode.

Syntax

.card { padding-block: 1.5rem; /* top and bottom in horizontal writing */ padding-inline: 2rem; /* left and right in LTR, reversed in RTL */ } /* Or individually */ .card { padding-block-start: 1rem; padding-block-end: 2rem; padding-inline-start: 1.5rem; padding-inline-end: 1.5rem; }

Rendered Result:

Border: Logical Properties

Borders can also use logical properties, including border radius corners which use compound logical directions (e.g., border-start-start-radius for the corner at the start of both axes).

Syntax

.sidebar { border-inline-end: 3px solid gray; /* Right border in LTR, left in RTL */ } /* Border radius with logical properties */ .card { border-start-start-radius: 8px; /* Top-left in LTR horizontal */ border-start-end-radius: 8px; /* Top-right in LTR horizontal */ border-end-start-radius: 8px; /* Bottom-left in LTR horizontal */ border-end-end-radius: 8px; /* Bottom-right in LTR horizontal */ }

Available Border Properties

border-block-start, border-block-end, border-block border-inline-start, border-inline-end, border-inline /* Individual properties */ border-inline-end-color: gray; border-inline-end-style: solid; border-inline-end-width: 3px;

Rendered Result:

Inset Properties (Positioning)

When using absolute or fixed positioning, logical inset properties replace top, right, bottom, and left. The inset shorthand is particularly useful.

Syntax

.positioned { position: absolute; /* ❌ Physical */ top: 1rem; right: 1rem; /* ✅ Logical */ inset-block-start: 1rem; /* top in horizontal writing */ inset-inline-end: 1rem; /* right in LTR, left in RTL */ } /* Shorthand */ .positioned { inset: 10px 20px; /* block inline */ inset-block: 10px 20px; /* start end */ inset-inline: 10px 20px; /* start end */ }

Rendered Result:

Size Properties

Width and height have logical equivalents: inline-size and block-size. In horizontal writing, inline-size is width and block-size is height. In vertical writing, they swap.

Syntax

.container { inline-size: 100%; /* width in horizontal writing */ block-size: 300px; /* height in horizontal writing */ max-inline-size: 1200px; /* max-width */ min-block-size: 200px; /* min-height */ }

Complete Size Property Mapping

Physical Logical Horizontal Writing
width inline-size Horizontal dimension
height block-size Vertical dimension
max-width max-inline-size Maximum horizontal
min-width min-inline-size Minimum horizontal

Text Alignment: start and end

Instead of text-align: left or right, use start and end which adapt to the writing direction. In LTR, start is left and end is right. In RTL, they flip.

Syntax

/* ❌ Physical - always left/right */ text-align: left; text-align: right; /* ✅ Logical - adapts to writing direction */ text-align: start; /* LTR=left, RTL=right */ text-align: end; /* LTR=right, RTL=left */

Rendered Result (LTR vs RTL):

Practical Example: LTR vs RTL Layouts

Here's a practical demonstration showing how the same CSS with logical properties creates proper layouts in both left-to-right (English) and right-to-left (Arabic) languages.

CSS (Works for Both Directions)

.container { padding-block: 2rem; padding-inline: 1.5rem; } .sidebar { inline-size: 250px; border-inline-end: 2px solid gray; padding-inline-end: 1rem; margin-inline-end: 1rem; } .button { padding-block: 0.75rem; padding-inline: 1.5rem; margin-inline-end: 1rem; }

Rendered Result:

Writing Modes

The writing-mode property changes how text flows. Logical properties automatically adapt to the writing mode, making them essential for supporting vertical text in languages like traditional Japanese and Chinese.

Writing Mode Values

/* Horizontal, top to bottom (default, English) */ writing-mode: horizontal-tb; /* Vertical, right to left (traditional Japanese, Chinese) */ writing-mode: vertical-rl; /* Vertical, left to right */ writing-mode: vertical-lr;

Demonstration:

Complete Property Reference

Margin and Padding

margin-block-start, margin-block-end, margin-block margin-inline-start, margin-inline-end, margin-inline padding-block-start, padding-block-end, padding-block padding-inline-start, padding-inline-end, padding-inline

Border

border-block-start, border-block-end, border-block border-inline-start, border-inline-end, border-inline /* Border radius (compound logical directions) */ border-start-start-radius, border-start-end-radius border-end-start-radius, border-end-end-radius

Positioning

inset-block-start, inset-block-end, inset-block inset-inline-start, inset-inline-end, inset-inline inset /* Shorthand for all four */

Size

inline-size, block-size min-inline-size, min-block-size max-inline-size, max-block-size

Other Properties

/* Text alignment */ text-align: start | end /* Float */ float: inline-start | inline-end /* Clear */ clear: inline-start | inline-end

Migration Strategy

Migrating existing CSS to logical properties is straightforward but requires understanding your content's writing direction needs.

Step-by-Step Migration

  1. Identify directional properties: Find all uses of left, right, top, bottom, width, height
  2. Determine intent: Is this truly directional or just a measurement?
  3. Replace directionally-meaningful properties: Margins, padding, borders that relate to reading direction
  4. Keep physical properties when appropriate: Some layouts genuinely need physical direction
  5. Test with dir="rtl": Verify layout works in both directions

When NOT to Use Logical Properties

Physical properties are still appropriate for:

  • Absolute positioning that must stay in a specific corner regardless of direction
  • Decorative elements that are truly physical (e.g., a design that must be on the physical left)
  • Integration with third-party components that use physical properties

Key Takeaways