Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parent <div> not wrapping child element with float attribute

Tags:

Please look at this example and explain to me why the green <div> does not wrap itself around the orange <div>.

<div id='green'>   <div id="orange"></div> </div>  #green{     border: 20px solid #3D3; }  #orange{     width :100px;     height: 100px;     border: 10px solid orange;     float: left; } 
like image 432
Web_Designer Avatar asked Mar 31 '11 04:03

Web_Designer


People also ask

How do you keep a floated element from wrapping?

Use "overflow: hidden" to avoid floating elements from wrapping a container's text. Unfortunately, any content of the content 's text will wrap underneath it: ![

How do I make DIVS always fit in my parent div?

For width it's easy, simply remove the width: 100% rule. By default, the div will stretch to fit the parent container. Height is not quite so simple. You could do something like the equal height column trick.


2 Answers

Try adding float:left and display:block to the outer div

see updated example - http://jsfiddle.net/jordanlewis/gDtSZ/1/

like image 107
Jordan Lewis Avatar answered Sep 27 '22 21:09

Jordan Lewis


Elements do not expand to contain floated children by default.

There are a number of workarounds, such as floating the parent, using a clearfix or adding overflow: hidden to the parent.

Personally, I try and use the latter.

Here is your jsFiddle working.

like image 41
alex Avatar answered Sep 27 '22 20:09

alex