0

Can anybody explain why no background color is displayed in the outermost div in the space of the inner div's margin?

<div style="background-color:yellow;">
    <div style="margin-top:10px;background-color:black;color:white;">
        Why isn't the background color yellow inside my top margin?
    </div>
</div>
Jason Butera
  • 2,376
  • 3
  • 29
  • 46

5 Answers5

4

Divs are block elements, but they take up no space on their own (other than creating a line break) so your inner div is filling all available space within the outer div, masking the yellow background. Add some padding to the outermost div and you will see the yellow.

Matt Sisto
  • 116
  • 5
2

This is known as "margin collapse".

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.

As found in other answers, adding padding or border to the parent will prevent the margins collapsing.

I also had success applying the following CSS to the container, based on tarkabak's method described here. (Please note limited browser compatibility of :before and :after.)

.prevent-margin-collapse:before,
.prevent-margin-collapse:after
{
    content: "\00a0"; /* No-break space character */
    display: block;
    overflow: hidden;
    height: 0;
}
<div style="background-color:yellow;" class="prevent-margin-collapse">
    <div style="margin-top:10px;background-color:black;color:white;">
        Why isn't the background color yellow inside my top margin?
    </div>
</div>

http://jsfiddle.net/yCHkW/

Community
  • 1
  • 1
showdev
  • 28,454
  • 37
  • 55
  • 73
1

In addition to the other answers: This is a matter of collapsing margins. The section "Collapsing Margins Between Parent and Child Elements" should apply in this specific case.

Update: Here's a statement regarding this topic taken directly from the box model specification of CSS3 (you can find almost the same sentence within the CSS2 specification as well):

Certain adjoining margins combine to form a single margin. Those margins are said to “collapse.” Margins are adjoining if there are no nonempty content, padding or border areas or clearance to separate them.

Onkel Toob
  • 2,152
  • 1
  • 17
  • 25
0

To achieve what you want to see change your html as followed:

<div style="background-color:yellow; padding-top:10px;">
    <div style="background-color:black;color:white;">
        Why isn't the background color yellow inside my top margin?
    </div>
</div>

The reason is that the outer div has no width set and just takes the size of its content.

Sven Bieder
  • 5,515
  • 2
  • 21
  • 27
0

I would imagine it has something to do with not inheriting any properties from elsewhere.

<div style="background-color:yellow; position: fixed;">
   <div style="margin-top:10px;background-color:black;color:white;">
        Why isn't the background color yellow inside my top margin?
    </div>
</div>

http://jsfiddle.net/rJ3HG/

Ryan
  • 494
  • 1
  • 5
  • 10