Languages
[Edit]
EN

CSS - margin collapsing

3 points
Created by:
elmer
646

In this article, we would like to show you what is margin collapsing in CSS.

Margin collapsing is a situation where browsers overlap margins of two neighbouring elements. The distance between two elements is then margin with larger value.

CSS - margin collapsing example
CSS - margin collapsing example

Practical example

Edit

In this example, we we create three paragraphs with different margins.

When you use Chrome Developer Tools to inspect the paragraphs, you can see their margins overlap leaving the space between paragraphs equal to the largest margin (not the sum of two margins as could be expected).

Note: the final space between each paragraph is calculated as max(20px, 10px) so it is 20px

Animated example

Edit

In this example, you can see animated margins preview created from the above example source code. Elements are selected with Chrome Developer Tools showing areas covered by margins. We can see the margins for paragraps are overlapped.

Animation that shows margins overlapping using Chrome Developer Tools.
Animation that shows margins overlapping using Chrome Developer Tools.

 

References

Edit
  1. margin - CSS: Cascading Style Sheets | MDN

Alternative titles

  1. CSS - margin auto-collapse
  2. CSS - margins overlapping
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join