Course contentsShow
← Web Development
Lesson 459 of 2,9828. Advanced CSS Selectors and FeaturesFree lesson

Logical Margin and Padding Properties

Use margin-block, margin-inline, padding-block, and padding-inline instead of top/right/bottom/left for flow-relative spacing.

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-start and margin-block-end instead of margin-top and margin-bottom
  • padding-block-start and padding-block-end instead of padding-top and padding-bottom
  • Shorthand: margin-block and padding-block

Inline Axis (horizontal flow in English):

  • margin-inline-start and margin-inline-end instead of margin-left and margin-right
  • padding-inline-start and padding-inline-end instead of padding-left and padding-right
  • Shorthand: margin-inline and padding-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.