I have a div with floated children.
I know I can stretch the height in the following 2 ways:
.container {
border: 2px solid #ccc;
margin-bottom: 250px;
}
.container-2::after {
content: '';
display: block;
height: 0;
font-size: 0;
clear: both;
}
.container-3 {
overflow: hidden;
}
.item {
float: left;
width: 200px;
height: 50px;
background: red;
margin: 10px;
}
<div class="container container-1">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="container container-2">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="container container-3">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
But I think they have the different principles: the clear:both
::after element stays away from the float brothers, and force the parent div to stretch the height; the overflow:hidden
style makes the div have the BFC, and according to standard, BFC will stretch its height to include its float children.
The advantages and disadvantages are not important,but the how they works.
Am I right, they are different but have the same result?