Margin Usage

Margin Usage

When using margins, try to only make an effect on the element that you put the margin to. It will be so much easier to maintain.

The Ways

Margin Usage 1

Practically, there are 3 ways that you can use to add some spacing to your elements.

Top and Bottom

It's not a good practice, because the margin will collapse.

Margin Usage 2

It's something that you don't want to have in your CSS. It will cause unexpected behavior.

Find more about margin collapse in Josh's Article (opens in a new tab)

Top or Bottom

Margin Usage 3

Nice, both work. But, why prefer one over the other?

Margin Bottom Side Effect

When using margin-bottom, this problem will appear

Margin Usage 4

When you remove an element, a side effect requires you to adjust the top element margins.

It is a nightmare to maintain because you need to find that other element, even though we change the other.

Eliminating Side Effects

Instead, just use margin-top

Margin Usage 5

It just works.

Last updated on November 14, 2023