Read more

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

Ulrich Berkmueller
February 23, 2012Software engineer

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.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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 Berkmueller
February 23, 2012Software engineer
Posted by Ulrich Berkmueller to makandra dev (2012-02-23 08:52)