Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding a parent <div> to the height of its children

Tags:

html

css

I have a page structure similar to this:

<body>   <div id="parent">     <div id="childRightCol">       /*Content*/     </div>     <div id="childLeftCol">       /*Content*/     </div>   </div> </body> 

I would like for the parent div to expand in height when the inner div's height increases.

Edit:
One problem is that if the width of the child content expands past the width of the browser window, my current CSS puts a horizontal scrollbar on the parent div. I would like the scrollbar to be at the page level. Currently my parent div is set to overflow: auto;

Can you please help me with the CSS for this?

like image 893
Steve Horn Avatar asked Dec 21 '08 05:12

Steve Horn


People also ask

How do you make a child div expand with their parents?

You have margin-bottom set on both elements. With the child having a bottom margin, it will always be smaller by that amount. Removing the margin-bottom style might bring it a little closer.

How do I make my div take 100% height of parent div?

container div has two parent elements: the <body> and the <html> element. And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.


2 Answers

Try this for the parent, it worked for me.

overflow:auto;  

UPDATE:

One more solution that worked:

Parent:

display: table; 

Child:

display: table-row; 
like image 68
Dr Casper Black Avatar answered Sep 18 '22 06:09

Dr Casper Black


Try to give max-contentfor parent's height.

.parent{    height: max-content; } 

https://jsfiddle.net/FreeS/so4L83wu/5/

like image 21
Skotee Avatar answered Sep 20 '22 06:09

Skotee