Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable margin collapse between sibling elements

Tags:

css

margin

Probably this is very stupid and well-known trick, but I haven't found any fix yet. I've tried "overflow", "content: ' '; display: table;", padding and 1px border. No success. So I've made small example to this problem.

There are 2 block elements: header with bottom margin and footer with top margin. The task is to make margins add together: 50 + 49 = 99 px!

.main-header {
  margin-bottom: 50px;
}
.main-footer {
  margin-top: 49px;
}
<h1>if distance btw H.&amp;F. is 99 px then margins don't collapse! Unfortunatelly, is is</h1>

<header class="main-header">
  HEADER Lorem ipsum dolor.
</header>

<footer class="main-footer">
  FOOTER <span>&copy;2015 Lorem ipsum dolor.</span>
</footer>
like image 965
ieXcept Avatar asked Feb 07 '16 18:02

ieXcept


People also ask

How to disable margin collapse in IE 7?

More specifically, in IE 7 margins do not collapse when some kind of layout is specified for the parent element, such as width. Show activity on this post. One neat trick to disable margin collapsing that has no visual impact, as far as I know, is setting the padding of the parent to 0.05px:

How to prevent margin from collapsing between siblings?

To prevent margin collapsing between siblings, add display: inline-block; to one of the siblings (one is enough though you can add it to both). Thanks for contributing an answer to Stack Overflow!

How to stop margin collapsing when padding is 0?

The padding is no longer 0 so collapsing won't occur anymore but at the same time the padding is small enough that visually it will round down to 0. If some other padding is desired, then apply padding only to the "direction" in which margin collapsing is not desired, for example padding-top: 0.05px;.

Can margins collapse?

Margins can collapse even when they aren’t from sibling elements. Margins in the same direction from different elements can also collapse. Margins from any number of elements can collapse. Negative margins also collapse, but it’s the larger-negative number that wins.


3 Answers

You could use Flexbox because it doesn't have collapsing margins.

.content {
  display: flex;
  flex-direction: column;
}

.main-header {
  margin-bottom: 50px;
}
.main-footer {
  margin-top: 49px;
}
<div class="content">
  <header class="main-header">
    HEADER Lorem ipsum dolor.
  </header>

  <footer class="main-footer">
    FOOTER <span>&copy;2015 Lorem ipsum dolor.</span>
  </footer>
</div>
like image 154
Nenad Vracar Avatar answered Sep 28 '22 13:09

Nenad Vracar


You can float them to disable the collapsing margins, with width:100% to make them to occupy the entire width, rather than determined by the content.

.main-header,
.main-footer {
  float: left;
  width: 100%;
}
.main-header {
  margin-bottom: 50px;
}
.main-footer {
  margin-top: 49px;
}
<h1>if distance btw H.&amp;F. is 99 px then margins don't collapse! Unfortunatelly, is is</h1>

<header class="main-header">
  HEADER Lorem ipsum dolor.
</header>

<footer class="main-footer">
  FOOTER <span>&copy;2015 Lorem ipsum dolor.</span>
</footer>
like image 39
Stickers Avatar answered Sep 28 '22 12:09

Stickers


To start, the flexbox solution that @Nenad Vracar posted is the most valid one.


A couple of alternatives
Assuming the problem is that you do not know if there is something between the two tags you could use an extra selector for this case (although it would fail if only text is between the two tags as it would still apply the 99 margin)

.main-header + .main-footer{margin-top:99px;}
<h1>if distance btw H.&amp;F. is 99 px then margins don't collapse! Unfortunatelly, is is</h1>

<header class="main-header">
    HEADER Lorem ipsum dolor.
</header>

<footer class="main-footer">
    FOOTER <span>&copy;2015 Lorem ipsum dolor.</span>
</footer>

Now, depending on your situation you could use the tricks you mention and pass the margin to the pseudo-elements.

.main-header:after {
    content: '';
    margin-bottom: 50px;
    display: table;
}

.main-footer:before {
    content: '';
    display: table;
    margin-top: 49px;
}

.main-header,
.main-footer {
    overflow: auto;
}
<h1>if distance btw H.&amp;F. is 99 px then margins don't collapse! Unfortunatelly, is is</h1>

<header class="main-header">
    HEADER Lorem ipsum dolor.
</header>

<footer class="main-footer">
    FOOTER <span>&copy;2015 Lorem ipsum dolor.</span>
</footer>
like image 20
Gabriele Petrioli Avatar answered Sep 28 '22 13:09

Gabriele Petrioli