CSS Logical Properties Margin/Border Direction Fix
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-blockandinset-inlinefor position - Use
inline-sizeandblock-sizefor dimensions - Test layouts in LTR (left-to-right) and RTL (right-to-left) modes
- Use
writing-modesupport for vertical text layouts
Common Mistakes with logical properties
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro