What is the combination of table and float?

Asked

Viewed 95 times

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.

  1. What is the effect of table in float ?

  2. And when you don’t do that because the parent element gets height: 0px ?

2 answers

2


1) The effect of table in float it’s not any, only that table has another display (table) unlike display: block of the div. How table has always been used to have elements inside the display of this has the height of content

This makes the flow of the elements a little different, which in turn affects the child elements in a different way.

2) A div whose children are only elements with float loses height because the alternative is too ugly: thinks, if the element only contains floats and the parent element had height would happen a large line (of a color qq) with a huge height with elements at each end of the site (Esq. and law)

So, without height, no one realizes that there is an element (this, if you use a div).

  • display: table replaces the clear: both in case of setting a grids system as in bootstrap, with floating columns?

  • @Andersoncarloswoss no, the clear is for when you know where the elements will be. Whereas the display: table makes a table and this table has content the table is with the height of the children

  • 1

    @Andersoncarloswoss the bootstrap uses margin to 'float' the columns

0

  • You mean the default height is 0 ?

  • @Magichat da div yes

  • 1

    I was thinking that height default is auto...

  • @Magichat I know.. maybe I expressed myself incorrectly, I meant that if you do not set anything your height is zero!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.