Divs floats in a container with height auto

Asked

Viewed 1,502 times

7

I’m having a problem structuring a layout with some divs one side of the other and when no more box fit they pass down. And the container main need to increase according to how many box you have. More or less like this:

inserir a descrição da imagem aqui

But I do not know why it is not working, the main container does not increase in size. My code:

HTML

<div id="container">
    <div class="caixas"></div>
    <div class="caixas"></div>
    <div class="caixas"></div>
    <div class="caixas"></div>
    <div class="caixas"></div>
    <div class="caixas"></div>    
</div>

CSS

#container{
    width:610px;
    height:AUTO;
    border:1px #FF0000 solid;    
}
.caixas{
   width:200px;
   height:200px;
   border:1px #00FF00 solid;
   float:left;
}

Jsfiddle

4 answers

5


You can use a technique known as clearfix, which consists of the next element or pseudo-element having display:block; and clear:both;, ensuring that it increases no matter how much content is float:left;.

Take this example:

jsFiddle

This same form is adopted by CSS frameworks like Twitter Bootstrap for example.

  • I added an example to the answer to make it more understandable.

  • Thank you very much worked out well.

  • Imagine , we are here to help us ^^

1

I made some changes

CSS:

#container{
    width:620px; // aumentei para 620
    height:AUTO;
    border:1px #FF0000 solid;    
}
.caixas{
   width:200px;
   height:200px;
   border:1px #00FF00 solid;
   display:inline-block; // retirei o float e usei display:inline-block

}

Display: inline-block

To view the code in operation click here

0

I particularly solve this with a float:left in the parent element. But the correct thing is to use the clearfix that friend Diego Lopes Lima commented.

-3

The CSS property "overflow" specifies what happens if the content exceeds the box of an element, giving you even the ability to control the side and horizontal scrollbars.

See more about the property: CSS Overflow

Using it we can solve your problem with only 1 line of code, just add this property with the value "Hidden" in the styles of your container.

See how your code fits:

#container{
    width:610px;
    height:AUTO;
    border:1px #FF0000 solid;  
    overflow:hidden;
}
.caixas{
   width:200px;
   height:200px;
   border:1px #00FF00 solid;
   float:left; 
}

http://jsfiddle.net/j7Nk8/3/

  • Your answer is not correct for this situation.

  • Interesting your opinion. I might know why you feel that way?

  • Your text really explains the functionality of the property overflow CSS, but in this case the problem posed by the question is about the element that encapsulates other elements that use float:left;. This causes this element not to increase as the content increases unless a block-view element or pseudo-element is placed right away with clear:both;.

  • I believe it was just a confusion of link’s, you seem to have picked up the link I passed in the reply to see the error, and ended up getting solved, so must have misinterpreted...

  • Maybe I made some confusion in the links anyway, but I still find my answer more appropriate to question. Using the last item with "display:block;" actually works, but for me this is considered gambiarra. Link fixed thanks.

  • Well, considering a gambiarra is really something of one’s own opinion, but this technique is used as the best solusão for the largest CSS framworks currently existing. Including Twitter Bootstrap, which is the most used front end tool in the world. If you have a more efficient unknown clearfix technique we would all like to understand and Twitter creator Bootstrap Mark Otto would also be happy to update the repository. If you would like to learn more about this file written in LESS: https://github.com/twbs/bootstrap/blob/master/less/mixins/clearfix.less

Show 1 more comment

Browser other questions tagged

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