I'm trying try to float an element to the right in an inline-element block but I'm noticing some strange behavior.
In the codepen link above, the first example shows that the .box div is wrapping around the floated element. I thought that you need to use a clearfix hack to make sure that a floated element's parent would properly contain it but that's not the case here. Also, for some reason the floated element is creating some extra vertical space beneath its parent div (see the blue gap in the codepen example). I also noticed that if you put some inline text next to the floated element, then the gap disappears (second example in codepen link).
Is there a reason why the inline-block parent element doesn't need to be cleared? Also, why is there extra space that appears below the parent div and how do you remove it?
.container {
background-color: blue;
text-align: center;
}
.box {
background-color: red;
display: inline-block;
width: 300px;
}
.float {
float: right;
line-height: 40px;
}
<div class='container'>
<div class='box'>
<div class='float'>Float Element</div>
</div>
</div>
</br>
<div class='container'>
<div class='box'>
Some text
<div class='float'>Float Element</div>
</div>
</div>