3
Everyone who deals with web programming has been through this:
.children {
float: left;
}
<div class="parent">
<div class="children">
Teste
</div>
</div>
If you use the Inspect Element above you may notice that in the element .parent
the height
is 0px
.
I have never read why this happens. This is part of the question, if someone knows and can explain, it would be good.
But what do I do to solve this ? Something I learned a while ago, not to have to use the clear: both
:
.parent {
display: table;
}
.children {
float: left;
}
<div class="parent">
<div class="children">
Teste
</div>
</div>
You can now notice that the element .parent
is the same height as the .children
, even with float
. Although I’ve been doing this for a while and it helps me a lot, I’d like to know why it happens.
What is the effect of
table
infloat
?And when you don’t do that because the parent element gets
height: 0px
?
display: table
replaces theclear: both
in case of setting a grids system as in bootstrap, with floating columns?– Woss
@Andersoncarloswoss no, the
clear
is for when you know where the elements will be. Whereas thedisplay: table
makes a table and this table has content the table is with the height of the children– MoshMage
@Andersoncarloswoss the bootstrap uses
margin
to 'float' the columns– Lennon S. Bueno