Course contentsShow
← Web Development
Lesson 145 of 2,9823. CSS FundamentalsFree lesson

Margin Shorthand Syntax

Use margin shorthand to set spacing on all sides with compact syntax.

What You'll Learn

You'll learn how to set all four sides of an element's margin in a single, compact CSS declaration instead of writing four separate lines.

The Core Idea

You've already learned about margin properties for individual sides (margin-top, margin-right, margin-bottom, margin-left). The margin shorthand lets you set all of these in one line, making your CSS more efficient and easier to read.

The shorthand follows a clockwise pattern: top, right, bottom, left (think of a clock starting at 12 and moving clockwise).

The Four Shorthand Patterns

Four Values: All Sides Different

div {
  margin: 10px 20px 30px 40px;
  /* top right bottom left */
}

Three Values: Vertical and Horizontal

div {
  margin: 10px 20px 30px;
  /* top | right & left | bottom */
}

Two Values: Vertical and Horizontal

div {
  margin: 10px 20px;
  /* top & bottom | right & left */
}

One Value: All Sides Equal

div {
  margin: 10px;
  /* all four sides */
}

Memory Trick

Think "TRouBLe" for the four-value order: Top, Right, Bottom, Left. Or imagine a clock: start at noon (top) and move clockwise.

When to Use It

Use shorthand when setting multiple sides at once. If you only need to adjust one side, use the specific property (margin-left: 10px;) for clarity.

Key Takeaway: Margin shorthand follows the clockwise pattern (top, right, bottom, left) and lets you set 1-4 values to control spacing around elements efficiently.