Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

Margin Collapse

Margin Collapse

Top and bottom margins of elements may occasionally collapse into a singular margin, which equals the larger of the two margins. This behavior does not occur with left and right margins.

Consider the following example:

Example

Illustration of margin collapsing:

h1 {
  margin: 0 0 50px 0;
}

h2 {
  margin: 20px 0 0 0;
}

In the given example, the <h1> element has a bottom margin of 50px, while the <h2> element has a top margin set to 20px.

]Intuitively, one might expect the vertical margin between the <h1> and the <h2> to sum up to 70px (50px + 20px). However, due to margin collapse, the actual margin ends up being 50px.

All CSS Margin Properties

Property

Description

margin

A single declaration shorthand for setting all margin properties.

margin-bottom

Specifies the margin at the bottom of an element.

margin-left

Specifies the margin at the left of an element 

margin-right

Specifies the margin at the right of an element.

margin-top

Specifies the margin at the top of an element