[CSS] You Don't Know Margins
Tags: html, css, frontend
Published: 2021-03-21 18:10:18

## Introduction

Some of the challenges in developing a website are its layout, legibility, spacing. In those cases, one solution could possibly be a smart use of margin. But, do you really know how the property <code> margin </code> works?


**This article is organized as follows:**

1. Introduction
2. Definition
3. Why?
4. When?
5. When not?
6. Conclusion



## Definition
[DuckDuckGoing](https://duckduckgo.com/) around about Margin Collapse, I have found some definitions about it, here I bring a summary of them.

Accordingly to W3.org, margin collapse is:

> "The adjoining margins of two or more boxes can combine to form a single margin.[...] the resulting is called collapsing margin."


A more comprehensive definition is given by CSS-Tricks:
> "Collapsing margins happens when two vertical margins come in contact with one another."

The final definition that I bring is given by MDN:
> "The top and bottom margins of blocks are sometimes combined (collapsed) into a single margin whose size is the largest of the individual margins."

 
While those definitions do a good job in describing what margin collapse is, bellow I show some cases where this collapse happens.

## Why?

Before moving on to the examples, I need to explain why it happens.

One of the reasons that CSS uses margin collapse I found on [this article](https://medium.com/@YeshaS93/collapsing-margins-a9d7d3f7be6e).

>The concept of margin collapsing comes from graphic design. There, you have margins to title and subtitles, but when a subtitle comes just after the title you should not double the margins. This is why they developed the concept of collapsed margins and this is why it happens just on vertical margins.

Using the power of margin collapsing, it is easily possible to define 
margins to sets of vertical content.

I explain, imagine we had a set of 3 paragraphs with this property.

```html
p {
 margin-top: 10px;
}

<main>
//10px margin
  <p> My First Paragraph </p>
//10px margin
  <p> My Second Paragraph </p>
//10px margin
  <p> My Third Paragraph </p>
//0 margin
</main>

```
The spacing wouldn't be equally spaced. An easy solution to this lies on the margin collapse.

```html
p {
 margin-top: 10px;
 margin-bottom: 10px;
}

<main>
//10px margin
  <p> My First Paragraph </p>
//10px margin (collapsed)
  <p> My Second Paragraph </p>
//10px margin (collapsed)
  <p> My Third Paragraph </p>
//10px margin
</main>

```


## When?

If there is one thing to remember when you leave this page is that **collapsing only happens on vertical margins**.

The main scenarios are:


### Parent and Descendants
If no border, padding, inline part or clearance separate the margin-top of a parent to his first child, then it is going to collapse.



![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/3u6e6fgj1w796auccp5r.png)

The same happens with the last child of the parent.


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4or2viiyxoy4562gduvv.png)


### Adjacent Siblings

When there are adjacent siblings, the margin-bottom of the top element collapses with the margin-top of the bottom element. The resulting size is the biggest between the two.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/fyhhl598dgjbp1h6q1hd.png)

### Empty Blocks

The margin-top and margin-bottom of an empty block collapse if there are no padding, border, inline content avoiding it. Also, it does not collapse if the block does not have a height or min-height attribute.


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/sa87hszmhfou48svch8z.png)


### Negative Margins

When it comes to negative margins, the resulting size depends on the signals involved.

####  Negative + Positive

The resulting size of a negative margin and a positive margin is their algebric sum.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/jm0texf4hh51tb5peywf.png)

#### Negative + Negative

When both margins are negative, the resulting size is similar when there are positive + positive. In this, the most negative one wins.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/gfrou7kk1ciqdgxbbhwm.png)

## When not?

There are many exceptions to the collapsing rule that it would be hard to know it by heart. 
Here I show some examples when the margins do not collapse.

### Non-Block-Level Elements

<ul>
<li> <code> Flex Items</code></li>
<li> <code>Grid Items </code></li>
<li> <code> Absolutely positioned items</code></li>
<li> <code>Other non-block-elements </code></li>
</ul>

### Border or Padding Property on the Parent Element
 
If there is a border-top or padding-top between the parent and the first-child.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8g2bj1sco8xs42ra4dwg.png)


The same happens if there is a border-bottom or padding-bottom between the parent and its last-child.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/755l2emdr2psof592bdb.png)

### Horizontal Margins

The horizontal margins never collapse.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/urj7mr9n82mreso954dq.png)


## Conclusion

<img src="https://thepracticaldev.s3.amazonaws.com/i/7osydb3fqh1047zs3mmx.jpg" width="400px" />

Even though it came with the idea of easing our lives, It can be really tricky to deal with margin collapse. This is mainly because of the many rules and exceptions has grown since the invention of it. One possible solution that it is easy to remember and understand is only applying margin-top to the elements. To read more about the possible solutions, [Harry Robert](https://csswizardry.com/2012/06/single-direction-margin-declarations/) discuss more on his article.


Follow me on [Twitter](https://twitter.com/camfilho) and comment down below your thoughts on this.