css html vector image

Logical Layouts: Stop Using Top, Right, Bottom, and Left in CSS

The Shift from Physical to Logical

For decades, web developers have relied on physical directions: top, right, bottom, and left. While these work for standard Western layouts, they create a maintenance nightmare the moment you need to support Right-to-Left (RTL) languages like Arabic or Hebrew, or vertical writing modes. CSS Logical Properties solve this by mapping physical directions to the flow of the content.

Understanding the Coordinate System

To master logical properties, you must replace the concept of 'horizontal' and 'vertical' with Inline and Block. The Block axis represents the direction in which blocks (like paragraphs) are stacked—typically top to bottom. The Inline axis represents the direction in which text flows within a line—typically left to right.

By using logical properties, your layout automatically adapts when the writing-mode or direction changes. If a user switches a site to Arabic, a logical margin on the 'start' of a button will correctly flip from the left side to the right side without you writing a single line of extra CSS.

Practical Examples

Consider a standard card component. Traditionally, you might write:

.card {
  margin-left: 20px;
  padding-top: 10px;
  border-right: 5px solid blue;
}

In a modern, logical approach, you would write this instead:

.card {
  margin-inline-start: 20px;
  padding-block-start: 10px;
  border-inline-end: 5px solid blue;
}

This code is now direction-agnostic. margin-inline-start refers to the beginning of the text line, regardless of whether that is the left or right side of the screen.

Simplified Centering and Sizing

Logical properties also simplify common layout tasks. Centering a block-level element used to require margin: 0 auto; or specific left/right values. Now, you can be explicit about which axis you are targeting:

.container {
  max-inline-size: 800px; /* Instead of max-width */
  margin-inline: auto;   /* Centers horizontally in LTR/RTL */
  padding-block: 2rem;   /* Sets top and bottom padding */
}

Why This Matters for SEO and Accessibility

Search engines and screen readers value content that respects internationalization standards. Using logical properties ensures that your UI remains functional and visually coherent across different locales. It prevents the "broken layout" look that often occurs when developers hard-code physical directions and then try to overlay RTL support using separate stylesheets or complex [dir="rtl"] overrides.

Browser Support and Adoption

As of 2024, CSS Logical Properties have excellent support across all evergreen browsers (Chrome, Firefox, Safari, and Edge). If you are building a new project or refactoring an existing design system, there is no reason to continue using physical properties. Switching to logical properties makes your CSS more robust, easier to read, and ready for a global audience.

Start by replacing your most common margin-left and margin-right instances with margin-inline-start and margin-inline-end. Once you get used to thinking in terms of flow rather than screen edges, you will never want to go back.