Logical Margin and Padding Properties
You'll learn how to use flow-relative spacing properties that adapt automatically to different writing modes and directions.
The Problem with Physical Directions
Traditional CSS uses physical directions—margin-top, padding-right, margin-bottom, padding-left—which assume horizontal, left-to-right text. But as you learned with the block and inline axes, content doesn't always flow that way. In vertical writing modes or right-to-left languages, "top" and "left" lose their logical meaning.
Flow-Relative Spacing
Logical properties replace physical directions with axis-relative ones:
Block Axis (vertical flow in English):
margin-block-startandmargin-block-endinstead ofmargin-topandmargin-bottompadding-block-startandpadding-block-endinstead ofpadding-topandpadding-bottom- Shorthand:
margin-blockandpadding-block
Inline Axis (horizontal flow in English):
margin-inline-startandmargin-inline-endinstead ofmargin-leftandmargin-rightpadding-inline-startandpadding-inline-endinstead ofpadding-leftandpadding-right- Shorthand:
margin-inlineandpadding-inline
Why This Matters
Think of it like giving directions: "turn left" only works if everyone faces the same way. But "turn toward the river" works regardless of which direction you're facing. Logical properties are like that—they work with your content's flow, not against it.
.card {
margin-block: 2rem; /* top & bottom in LTR/English */
margin-inline: 1rem; /* left & right in LTR/English */
padding-block-start: 1rem; /* top in LTR/English */
}
If the writing mode changes, these properties automatically adjust without rewriting CSS.
Key Takeaway: Use margin-block, margin-inline, padding-block, and padding-inline to create spacing that respects content flow direction, making your designs adaptable across languages and writing modes.