3

There are multiple ways posted here to uncollapse a vertical PARENT margin, but nothing about uncollapsing vertical margins of adjacent elements. The only solution I found was in this answer (back in 2009):

<div style="overflow: hidden; height: 0px; width: 0px;">&nbsp;</div>

Almost 7 years passed since there. Is some better way to do this (possibly using some CSS3)?

Basically, suppose you have: http://jsfiddle.net/ok2u3o3c/

<div class="one"></div>
<div class="two"></div>

div {
  width: 300px;
  height: 200px;
}

.one {
  margin-bottom: 10px;
  background-color: blue;
}

.two {
  margin-top: 20px;
  background-color: red;
}

What would be the most elegant way to make the distance between these 2 boxes 30px instead of 20px (where the first margin contributes 10px and doesn't collapses)?

aynber
  • 22,380
  • 8
  • 50
  • 63
daremkd
  • 8,244
  • 6
  • 40
  • 66
  • 1
    Can you please elaborate about your situation (code) and what you trying to achieve? Asking your readers to dig into the other question and the resources it links to is not very helpful. – GolezTrol Dec 15 '15 at 14:47
  • I'm trying to achieve adjacent vertical margin collapse in a more elegant way than the (best) way I've found so far. – daremkd Dec 15 '15 at 14:50
  • I have added the code. – daremkd Dec 15 '15 at 15:22

1 Answers1

2

Let's start with the relevant documentation explaining the behavior of collapsing margins:

8 Box model - 8.3.1 Collapsing margins

In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.

The following rules apply, which means that there are a things that you can do to prevent the margins from collapsing for sibling elements:

Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children)

Therefore if you float the elements with collapsing margins, they will no longer collapse:

.collapsing-margins {
  margin: 100px 0;
  background: #f00;
  float: left;
  width: 100%;
}
<div class="parent">
  <div class="collapsing-margins">Element</div>
  <div class="collapsing-margins">Element</div>
</div>

Margins of inline-block boxes do not collapse (not even with their in-flow children).

Therefore you could also add change the display of the elements to inline-block:

.collapsing-margins {
  margin: 100px 0;
  background: #f00;
  display: inline-block;
  width: 100%;
}
<div class="parent">
  <div class="collapsing-margins">Element</div>
  <div class="collapsing-margins">Element</div>
</div>
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
  • And I guess the third way is what I specified, so those are the only options? – daremkd Dec 15 '15 at 16:47
  • @daremkd See http://www.w3.org/TR/CSS2/box.html#collapsing-margins under *"Note the above rules imply that:"*. There were a couple more, I just didn't list them all. – Josh Crozier Dec 26 '15 at 00:04