What collapsing margins are, how they work and when margins do not collapse

Updated . Posted . Visible to the public.

Imagine you have 2 HTML boxes. The first one has a margin-bottom of let's say 30px and the second one a margin-top of 20px. After rules of collapsing margins Show archive.org snapshot have been applied we have a margin of 30px (not 50px) between these two boxes . This is because no addition of both margins takes place but the maximum of both is applied. This behavior is called collapsing margins.

Oftentimes it is a good behavior but collapsing margins can be annoying, too. For example child elements with margins also collapse with margins of the previous sibling of the parent box.

Nevertheless there are some exceptions where the behavior of vertical collapsing margins is not used.

Exceptions when margins do not collapse

Be aware that in some situations, margins do not collapse and we talk about Uncollapsing Margins Show archive.org snapshot . The list below summarizes the conditions when margins do not collapse. It is an excerpt from Sitepoint - Collapsing Margins Show archive.org snapshot .

  • flexbox children
  • absolutely positioned elements
  • inline-block elements
  • elements with overflow set to anything other than visible (They do not collapse margins with their children.)
  • floated elements
    • cleared elements (They do not collapse their top margins with their parent block’s bottom margin.)
  • the root element

Workaround: Utility classes to remove stacked margins

It is very hard to write your CSS so that all components can be combined in any layout and still retain collapsing margins.

For these situations, have some utility classes that remove the top or bottom margins, like .no-top-margin. When two margins don't collapse for some reason, you can apply the utility class to the component with the smaller margin.

In Bootstrap Show archive.org snapshot these classes are .mt-0 (remove top margin) or .mb-0 (remove bottom margin).

Collapse margins in tile layouts

When your margins don't collapse due to one of the rules above, you can sometimes use this hack to fix it:

Say you have a tile grid with many 150*150 pixel tiles. Each tile has 10px margin on all sides. When you implement this by letting tiles float left, this 10px margins won't collapse with elements above or below the grid:

.tiles
  +clear_after
  .tile
    float: left
    height: 150px
    width: 150px
    margin: 10px // uncollapsable margin because this floated element will be cleared by its container

What you can do now is to wrap all tiles in another container (.tiles_inner) which clears the floats and neutralizes the outer tiles' margins with a negative margins. Now we reapply the outer tiles' margin to the outer container (.tiles). Since no floating is involved, this margin can now collapse properly:

.tiles
  margin: 10px // this margin can be collapsed
  .tiles_inner
    +clear_after
    margin: -10px // neutralize the margin that cannot be collapsed
  .tile
    float: left
    height: 150px
    width: 150px
    margin: 10px // uncollapsable margin because this floated element will be cleared by its container

You can only use this hack if the container knows the margin of its children.

Further reading

Please follow the links if you want to delve deeper into the subject.

Ulrich Berkmüller
Last edit
Michael Leimstädtner
Keywords
overlap
License
Source code in this card is licensed under the MIT License.
Posted by Ulrich Berkmüller to makandra dev (2012-02-23 07:52)