π What Are margin-inline & margin-block?
These properties allow you to set margins based on the **writing mode** rather than physical directions (left, right, top, bottom). They provide **logical margins** that adapt automatically to:
- π Different languages (LTR, RTL)
- β Vertical writing modes
- π± Responsive and internationalized UI layouts
Note
β Writing-mode aware
β Essential for multilingual & vertical writing support
π¦ Understanding Logical Directions
Instead of physical directions, logical properties use:
| Logical Side | Depends On |
|---|---|
| inline-start | Where text begins (left in LTR, right in RTL) |
| inline-end | Where text ends |
| block-start | Top in horizontal writing |
| block-end | Bottom in horizontal writing |
π¦ Syntax
margin-inline & margin-block syntax
margin-inline: <start> <end>;
margin-block: <start> <end>;
margin-inline-start: <length | auto>;
margin-inline-end: <length | auto>;
margin-block-start: <length | auto>;
margin-block-end: <length | auto>;π 1. Using margin-inline
margin-inline example
.box {
margin-inline: 20px; /* Applies to inline-start & inline-end */
}Equivalent to:
margin-left: 20px; margin-right: 20px; in LTR
margin-right: 20px; margin-left: 20px; in RTL
π 2. Different Inline Start and End Values
Start & end margins
.box {
margin-inline: 10px 40px; /* start end */
}π 3. Using margin-block
margin-block example
.card {
margin-block: 30px; /* top & bottom in horizontal writing */
}Equivalent to:
margin-top: 30px; margin-bottom: 30px;
π 4. Different Block Start/End Values
Start & end block margins
.card {
margin-block: 15px 50px; /* block-start block-end */
}π 5. Example Using All Logical Margin Properties
Full logical layout example
.article {
margin-inline-start: 20px;
margin-inline-end: 40px;
margin-block-start: 10px;
margin-block-end: 30px;
}π 6. International-Friendly Layout
LTR + RTL support
.content {
writing-mode: horizontal-tb; /* default */
direction: rtl; /* Hebrew, Arabic */
margin-inline: 2rem; /* auto flips! */
}Using logical properties removes the need for separate RTL CSS files.
π 7. Vertical Writing Mode Example
Vertical layout
.poem {
writing-mode: vertical-rl;
margin-inline: 20px; /* now top/bottom in vertical flow */
margin-block: 10px; /* now left/right depending on mode */
}Inline and block directions adapt automatically.
π§ͺ Real-World Use Cases
1οΈβ£ Universal Card Component
Card spacing
.card {
margin-block: 1.5rem;
margin-inline: 1rem;
}2οΈβ£ Blog Layout Supporting RTL
Blog example
.post {
margin-inline-start: 2rem;
margin-inline-end: 2rem;
}3οΈβ£ Navigation Bar in RTL Languages
Navbar
.nav-item {
margin-inline: 0.5rem;
}β οΈ Common Mistakes
- β Mixing physical (left/right) & logical (inline/block) margins incorrectly
- β Expecting margin-inline to affect vertical spacing
- β Forgetting direction/writing-mode change layout behavior
Note
π₯ Summary
margin-inline andmargin-block help you write layout code that adapts to languages, directions, and writing-modes automatically. They make your CSS cleaner, more scalable, and global-ready.
Learn more βMDN: margin-inline
MDN: margin-block π