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

Margin: Outer Spacing

Create space between elements using margin and understand its effects on layout.

Margin: Outer Spacing

You'll learn how margin creates space outside elements, pushing them away from their neighbors.

What Is Margin?

While padding adds space inside an element (between content and border), margin creates space outside the element—beyond its border. Think of margin as your element's personal bubble of space that keeps other elements at a distance.

Imagine books on a shelf. Padding is like the white space inside the book cover around the text. Margin is like the actual gap between books on the shelf—the air separating one book from the next.

Basic Margin Syntax

You set margin values just like padding:

.box {
  margin: 20px; /* 20px space on all sides */
}

Margin pushes elements away from:

  • Other sibling elements
  • The edges of their parent container
  • Anything around them in the layout

How Margin Affects Layout

When you add margin to an element, browsers push neighboring elements farther away. A margin-bottom: 30px creates a 30-pixel gap below that element before the next one appears. A margin-left: 15px moves the element 15 pixels away from whatever's on its left.

.card {
  margin: 25px;
  /* Creates 25px breathing room around the entire card */
}

Unlike padding (which expands the element's box), margin lives outside the border entirely—it's truly external spacing.

When to Use Margin

Use margin when you need to:

  • Separate paragraphs or sections
  • Create gaps between cards or boxes
  • Center elements (you'll learn this trick soon!)
  • Control spacing between navigation items

Key Takeaway: Margin creates outer spacing around elements, pushing them away from neighbors and controlling the gaps between elements in your layout.