Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make floated div container fit to the div height?

Tags:

html

css

I have a div container #parent and a div child #child who's child of the parent div .

The #child div contain text and he is floated left, the problem is that I want the #parent div to fit the height of the #child` div what ever the height is with keeping the float property also .

    #parent{
       background: red;
       height: auto;
    }

    #child{
       float:left;
    }
    <div id='parent'>
        <div id='child'>
           <p>Some text Some text Some text</p>
        </div>  
    
    </div>

Here is a Jsfiddle

like image 966
Abddoo Avatar asked Jan 08 '23 09:01

Abddoo


2 Answers

Add overflow:auto to the parent:

#parent {
    background: red;
    height: auto;
    overflow:auto;
}
#child {
    float:left;
}

jsFiddle example

When you float the child, the parent collapses because it's acting as if the child occupies no space. Adding the overflow rule restores the behavior that you're after.

like image 154
j08691 Avatar answered Jan 18 '23 11:01

j08691


Method :1

 #parent{
       background: red;
       height: auto;
       overflow:auto
    }

    #child{
       float:left;
      }

http://jsbin.com/yufezamofo/3/

Method:2

 #parent{
       background: red;
       height: auto;
display:table
    }

    #child{
       float:left;
      display:table-cell;
      }

http://jsbin.com/yufezamofo/2/

like image 43
Prime Avatar answered Jan 18 '23 13:01

Prime