Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Floating divs have 0 height

Tags:

html

css

I'm trying to place 2 divs side by side inside of another div, so that I can have 2 columns of text and the outer div drawing a border around both of them:

HTML

<div id="outer">     <div id="left">          ...     <div id="right"> </div> 

CSS

#outer{     background-color:rgba(255,255,255,.5);     width:800px; }  #left{     float:left; }  #right{     width:500px;     float:right; } 

However, the outer div registers a height of 0px and so the border doesn't go around the other divs. How do I make the outer div recognize the heights of the things inside it?

like image 666
ario Avatar asked Jan 26 '12 20:01

ario


People also ask

Why does my div have zero height?

The reason why the height or the containers is 0 is because you are floating all the content inside them and floated elements don't expand the height (or width) of their parents.

Why is HTML height zero?

Height = 0 because the parent of the elements have 0 height, so make sure your parent, which is body here have 100% height or else.

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.

What does height 100% do CSS?

height: 100% gives the element 100% height of its parent container. height: auto means the element height will depend upon the height of its children.


1 Answers

It's not because the floating divs doesn't have a height, it's because the floating divs don't affect the size of the parent element.

You can use the overflow style to make the parent element take the floating elements in consideration:

#outer { overflow: auto; } 
like image 71
Guffa Avatar answered Sep 23 '22 15:09

Guffa