Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DIVs, height and float

Tags:

html

css

I have float red divs on blue div like on picture enter image description here

<div class="blue">
  <div style="height: 40px; float: left"></div>
  <div style="height: 40px; float: left"></div>
  <div style="height: 40px; float: left"></div>

  <div style="height: 40px; float: left"></div>
  <div style="height: 40px; float: left"></div>
  <div style="height: 40px; float: left"></div>
</div>

I want to do, so blue div have height on red DIVs. When I remove float it's OK.

like image 930
Kuba Żukowski Avatar asked Apr 23 '13 19:04

Kuba Żukowski


People also ask

How do you make two DIVs float the same height?

Answer: Use the CSS3 flexbox With CSS3 flex layout model you can very easily create the equal height columns or <div> elements that are aligned side by side. Just apply the display property with the value flex on the container element and the flex property with the value 1 on child elements.

How do you float a div?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

Can Div have height?

This seems like the ideal candidate for transition from a table-based layout to a CSS layout. It makes sense: both DIVs and tables can be nested, have HEIGHT and WIDTH attributes set, contain borders, etc.


1 Answers

You need to add display:table-cell; or overflow:hidden; to your blue div. This will give the parent the height of it's children.

Demo

like this:

.blue{
   overflow:hidden;
   //or
   //display:table-cell;
}

a sidenote - your divs need a width when they are floating.

You also have the option to make your div with class blue float. But this might cause some unwanted behavior in your document (if the div is not supposed to float).

like image 120
Peter Rasmussen Avatar answered Sep 19 '22 21:09

Peter Rasmussen