Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand container div with content width

I have the following structure in my application:

<div id="container">   <div id="child_container">     <div class="child"></div>     <div class="child"></div>     ...     <div class="child"></div>   </div> </div> 

Each child div has a known fixed width, but the application allows more of them to be inserted in the child_container div.

What I'm trying to do is to have the container div expand horizontally when needed, given the total width of the child container.

This is what happens currently:

+------ container -------+ +--- child_container ----+ | child1 child2 child3   | | child4                 | +------------------------+ 

If I set the child_container div width to a fixed value, I can get it to expand horizontally past the container div, which works despite being a bit ugly:

+------ container -------+ +------ child_container -+----+ | child1 child2 child3 child4 | +------------------------+----+ 

However, that requires recalculating it whenever a new child is added.

Is there a way to do this without using fixed widths for child container, in a way such that the end result is

+--------- container ---------+ +------ child_container ------+ | child1 child2 child3 child4 | +-----------------------------+ 

Thanks.

like image 257
Andre Avatar asked May 24 '11 18:05

Andre


People also ask

How do you increase div width automatically?

width: length|percentage|auto|initial|inherit; Property Values: width: auto; It is used to set width property to its default value. If the width property set to auto then the browser calculates the width of element.

How do I stretch a div to fit a container?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.


1 Answers

Something like this should work:

#container, #child_container, .child {      position: relative;      float: left; } 
like image 158
wajiw Avatar answered Oct 04 '22 10:10

wajiw