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.
If you float everything left including the containing divs, it will work.
The modern solution today would be
Because
flex-wrap
is by default set toThis simple solution works like a charm. And by now also in all relevant browsers.
just set the width of the parent to 120% and done =)
The parent container won't grow when a child element is added.
but we can avoid the rendering of new one on the next line by using css3 flex box. The sample is placed the below mentioned path
The result will look like this:
Even easier:
Something like this should work: