-1

I have this issue where a space is appearing between two divs and I don't know what is causing this. Basically it is just one div with three smaller divs of fixed height inside of it. The space is between the block-title and block-content divs. There is no space between the block-content and block-footer

Here is the HTML:

<div class="block buy">
  <div class="block-title">
    <span class="line-1">Title</span>
  </div>
  <div class="block-content">
    <h2></h2>
  </div>
  <div class="block-footer">
  </div>
</div>

And here is the CSS:

.block {
  width: 300px;
  margin-left: auto;
  margin-right: 15px;
  display: inline-block;
  vertical-align: top;
  margin-top: 35px;
  font-weight: 400;
  text-align: left;
}
.block-title {
  height: 70px;
  padding: 20px;
}
.block-content {
  width: 100%;
  height: 255px;
  text-align: center;
}

.block-footer {
  height: 75px;
}
.block-title,
.block-content,
.block-footer {
  color: #fff;
}
.line-1 {
  font-weight: 300;
  font-size: 20px;
  display: block;
}
.buy > * {
  background-color: #558F38;
}

A live JSfiddle is here: JSFiddle

Any ideas why this is happening?

Edit

So the reason why the space appears is because the <h2> elements margin goes outside of the containing div. Why is it not contained inside the div?

martin36
  • 2,253
  • 4
  • 18
  • 27

4 Answers4

3

Your h2 has margin that escapes its container and pushes the outer divs. Remove the h2 or remove its margin.

Royal Wares
  • 1,192
  • 10
  • 23
  • Thanks, that was the problem. But why is the margin of `

    ` going out of the containing `
    `?

    – martin36 Aug 03 '18 at 13:24
  • This is the only post that actually answers the question. – Rob Aug 03 '18 at 13:27
  • 2
    Margins sometimes behave counter-intuitively, you can find out more about these quirks from https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing In your example the margin needed to push off something, it doesn't touch the parent divs, but siblings/uncles etc are all valid to push from and cause the behaviour you saw. – Royal Wares Aug 03 '18 at 13:35
  • @martin36 Also Google for "collapsing margins". – Rob Aug 03 '18 at 13:40
  • Thanks, I will do that – martin36 Aug 03 '18 at 14:11
0

add this class or remove

from second div
h2 {
    margin: 0;
}

.block {
    width: 300px;
    margin-left: auto;
    margin-right: 15px;
    display: inline-block;
    vertical-align: top;
    margin-top: 35px;
    font-weight: 400;
    text-align: left;
}
.block-title {
    height: 70px;
    padding: 20px;
}
.block-content {
    width: 100%;
    height: 255px;
    text-align: center;
}
.block-footer {
    height: 75px;
}
.block-title, .block-content, .block-footer {
    color: #fff;
}
.line-1 {
    font-weight: 300;
    font-size: 20px;
    display: block;
}
.buy > * {
    background-color: #558F38;
}
h2 {
    margin: 0;
}
<div class="block buy">
  <div class="block-title">
    <span class="line-1">Title</span>
  </div>
  <div class="block-content">
    <h2></h2>
  </div>
  <div class="block-footer">
  </div>
</div>
Ankit Jaiswal
  • 274
  • 2
  • 8
-1

U can replace display: inline-block with display: inline-grid. Thath solves the problem.

.block {
    width: 300px;
    margin-left: auto;
    margin-right: 15px;
    display: inline-grid;
    vertical-align: top;
    margin-top: 35px;
    font-weight: 400;
    text-align: left;
}
Daniel Spiess
  • 222
  • 1
  • 13
-1

In you css you could add:

h2{
     margin-bottom:0px;
}
vitomadio
  • 1,140
  • 8
  • 14