Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For what reason margin collapse rules were introduced in CSS?

Tags:

Just can't figure out the situation when this clever set of rules can be helpful. They break the simplicity of the box model and provide infinite source of troubles when you combine different pieces of layout together. So what is the reason?

Rules for the reference.

Update: Rules are quite logical for sibling elements, but why margins should propagate to parent elements up to the tree? What kind of problems that solves?

For example:

<div style="margin: 20px; background-color: red;">     <div style="margin: 20px;">         <p style="margin: 100px;">red</p>     </div> </div> <div style="margin: 20px; background-color: blue;">blue</div> 

Top level divs are spaced from each other by 100px.

like image 326
actual Avatar asked Sep 17 '09 10:09

actual


People also ask

Why do margins collapse in CSS?

Margin collapse occurs when vertically adjacent margins of block-level elements collide to share a general margin space. The size of this shared space is dictated by the larger number margin. You can visualize this as an arm wrestling match, where the larger margin will take over and win.

Is CSS margin collapsing only happens with?

Margin collapsing only happens in the block-direction. This is true even if you change the writing-mode or use logical properties. The largest margin “wins”

What is the purpose of margin in CSS?

The CSS margin properties are used to create space around elements, outside of any defined borders. With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

What is collapsing in CSS?

Collapsing margins happen when two vertical margins come in contact with one another. If one margin is greater than the other, then that margin overrides the other, leaving one margin.


2 Answers

This is one of the situations where it doesn't really make sense until you realise that the alternatives make less sense.

As you probably know, margins specify the distance between elements, it's not an "outer padding" that surrounds each element. If two elements with the margin 20px are next to each other, the distance between them is 20px, not 40px.

As the margin is a distance to another element, it makes sense that the distance is from the element to the surrounding elements, not to the boundary of the parent element.

If the margin would be counted to the boundary of the parent element, putting elements in a div element would introduce extra spacing between the elements eventhough the div itself has no margin or padding. The margins around an element should remain the same if you add an unstyled div around it.

like image 113
Guffa Avatar answered Sep 21 '22 13:09

Guffa


When it could be helpful?

The simplest example: a list of paragraphs and headings, each with a margin-top and margin-bottom. You want a margin on the top and bottom of the article, and between different elements.

With margin collapsing, you can do without setting special margins on the first or last item (NOT a part of the original CSS spec!) or padding the container.

But I agree, on a whole, it's a pointless feature.

like image 22
Eli Krupitsky Avatar answered Sep 24 '22 13:09

Eli Krupitsky