bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/CSS/CSS Foundations
CSS•CSS Foundations

CSS Margin Collapse

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind CSS Margin Collapse?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___-bottom: 50px;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Margin collapse is when two margins collapse into a single margin.
All CSS Margin Properties
CSS Margin Collapse

Margin collapse is when two margins collapse into a single margin.

Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.

Note

Margin collapse only happens with top and bottom margins! Not left and right margins!

In the following example, the <h1> element has a bottom margin of 50px and the <h2> element has a top margin of 20px. So, the vertical margin between the <h1> and the <h2> would be a total of 70px (50px + 20px). But due to margin collapse, the actual margin ends up being 50px:

Example

Formatted code
h1 {
  margin-bottom: 50px;
}
h2 {
  margin-top: 20px;
}

Live preview

In the following example, each <p> element has a top margin of 30px and a bottom margin of 30px. So, the vertical margin between the <p> elements should have been 60px (30px + 30px). However, due to margin collapse, the actual margin ends up being 30px:

Example

Formatted code
p {
  margin-top: 30px;
  margin-bottom: 30px;
}

Live preview

All CSS Margin Properties

PropertyDescription
marginA shorthand property for setting all the margin properties in one declaration
margin-bottomSets the bottom margin of an element
margin-leftSets the left margin of an element
margin-rightSets the right margin of an element
margin-topSets the top margin of an element

Previous

CSS Margins

Next

CSS Padding