Skip to content

CSS Logical Properties Margin/Border Direction Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about CSS Logical Properties Margin/Border Direction Fix. We cover key concepts, practical examples, and best practices.

The Problem

CSS logical properties (margin-block-start, margin-inline-end, etc.) replace physical directions (margin-top, margin-right) for layouts that adapt to writing modes. When logical properties are not used, layouts break in right-to-left (RTL) or vertical writing contexts.

Quick Fix

Step 1: Replace physical margin with logical

/* Wrong — margin-top adds space above regardless of writing mode */
.card {
    margin-top: 1rem;
    margin-left: 1rem;
}

/* Right — logical properties adapt to writing mode */
.card {
    margin-block-start: 1rem;
    margin-inline-start: 1rem;
}

/* In RTL: inline-start becomes right instead of left */

Step 2: Use logical border properties

/* Wrong — physical border on left side */
.card {
    border-left: 3px solid #6366f1;
    padding-left: 1rem;
}

/* Right — logical border, works in LTR and RTL */
.card {
    border-inline-start: 3px solid #6366f1;
    padding-inline-start: 1rem;
}

/* In RTL: border appears on the right side */

Step 3: Set logical padding

/* Wrong — asymmetric physical padding */
.callout {
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
    padding-left: 1rem;
    padding-right: 0.5rem;
}

/* Right — logical padding */
.callout {
    padding-block: 0.5rem; /* block-start + block-end */
    padding-inline: 1rem 0.5rem; /* inline-start, inline-end */
}

Step 4: Use logical width and height

/* Wrong — physical dimensions */
.sidebar {
    width: 250px;
    height: 100%;
}

/* Right — use logical sizing */
.sidebar {
    inline-size: 250px;
    block-size: 100%;
}

/* In vertical writing mode: inline-size is height, block-size is width */

Step 5: Handle positioning with logical insets

/* Wrong — physical positioning */
.tooltip {
    position: absolute;
    top: 100%;
    left: 0;
}

/* Right — logical positioning */
.tooltip {
    position: absolute;
    inset-block-start: 100%;
    inset-inline-start: 0;
}

/* Shortcut: use inset-inline and inset-block */
.tooltip {
    position: absolute;
    inset-block: auto 0;
    inset-inline: 0 auto;
}

Step 6: Combine with writing-mode and direction

/* This card works correctly in any writing mode */
.card {
    border-inline-start: 3px solid #6366f1;
    padding-inline-start: 1rem;
    margin-block-end: 1rem;
}

/* RTL support — no additional CSS needed */
[dir="rtl"] .card {
    /* border-inline-start automatically flips to right side */
    /* No override needed! */
}

Prevention

  • Use logical properties for margins, padding, and borders in all new code
  • Use inset-block and inset-inline for position
  • Use inline-size and block-size for dimensions
  • Test layouts in LTR (left-to-right) and RTL (right-to-left) modes
  • Use writing-mode support for vertical text layouts

Common Mistakes with logical properties

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between block and inline in logical properties?

Block axis runs perpendicular to the flow of text (top to bottom in horizontal writing). Inline axis runs parallel to the text flow (left to right in LTR, right to left in RTL). margin-block-start equals margin-top in horizontal LTR.

Do I need to rewrite all existing CSS?

No. Physical and logical properties both work. Use logical properties for new components and when adding RTL support. Physical properties are fine for projects that never need LTR-to-RTL translation.

What are the shorthand logical properties?

margin-block: 10px 20px (start and end), margin-inline: 10px 20px, padding-block, padding-inline, border-block, border-inline, inset-block, inset-inline. These combine two physical directions into one logical declaration.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro