The reason you're encountering this problem is due to how float
actually works. When you build a container element, then float the children inside the floated elements will not apply any height to the parent element, your container.
Due to the structure of your markup, you instantly float
those children elements, this doesn't add height so the background: yellow;
never actually displays. If you add height
or padding
the color will appear though.
You can read more on float
here.
This will instantly show your background:
header {
height: 4.3rem;
background: yellow;
}
Update: (Demonstrate Clearfix, proper approach)
You could solve your problem by applying a clearfix
to the solution. For instance you could structure the markup in the following fashion:
<header>
<div class="Clearfix">
<section id="Left-Head">
<h1>Sample Solution</h1>
</section>
<section id="Right-Head">
<h3>Content</h3>
</section>
</div>
</header>
Then you would use the existing style information, but add the following as a Clearfix.
.Clearfix:before, .Clearfix:after {
height: 0rem;
content: '';
display: block;
clear: both;
}
Then the background would properly append.
Full Solution:
header {
background: yellow;
}
.Clearfix:before, .Clearfix:after {
height: 0rem;
content: '';
display: block;
clear: both;
}
#Left-Head {
width: 50%;
background: blue;
float: left;
}
#Right-Head {
width: 50%;
float: right;
}
h5 {
clear: both;
}
<header>
<div class="Clearfix">
<section id="Left-Head">
<h1>Sample Solution</h1>
</section>
<section id="Right-Head">
<h3>Content</h3>
</section>
</div>
</header>
<h5>Lorem Ipsum...</h5>