tags:

views:

2765

answers:

5

Hi all, I have these nested Divs and I need the main container to expand (in height) to accomodate the DIVs inside

    <!-- head -->
    ...
    <!-- /head -->

    <body class="main">
      <div id="container">
        <div id="header">
          <!--series of divs in here, graphic banner etc. -->
        </div>

    <div id="main_content"> <!-- this DIV _should_ stretch to accomodate inner divs -->
      <div id="items_list" class="items_list ui-sortable">
        <div id="item_35" class="item_details">
        </div>
        <div id="item_36" class="item_details">
        </div>        
        <div id="item_37" class="item_details">
        </div>
        <!-- this list of DIVs "item_xx" goes on for a while
             each one representing a photo with name, caption etcetc -->
      </div>
    </div>
    <br class="clear"/>

    <div id="footer">
    </div>
  </body>
</html>

CSS is this:

* {
    padding: 0;
    margin: 0;
}

.main {
    font: 100% Verdana, Arial, Helvetica, sans-serif;
    background: #4c5462;
    margin: 0; 
    padding: 0;
    text-align: center; 
    color: #000000;
}
.main #container {
    height: auto;
    width: 46em;
    background: #4c5462;
    margin: 0 auto; 
    border: 0px solid #000000;
    text-align: left;      
}

.main #main_content {
    padding: 5px;
    margin: 0px;
}
#items_list {
    width: 400px;
    float: left;
}

.items_list {
    width: 400px;
    float: left;
}
.item_details {
    margin-top: 3px;
    margin-bottom: 3px;
    padding: 3px;
    float: left;
    border-bottom: 0.5px solid blue;
}

The problem I have is that #main_content doesn't stretch to accomodate all the inner divs, with the result that they keep going against the background.

How can I solve this?

thanks, patrick

A: 

add a float property to the main_content div - it will then expand to contain its floated contents

Ray
Thanks Ray, this worked although it shrunk the div to the size of it's content (#items_list) which has width: 400px; Your solution would be really good if I could make #main_content stay to its full width - any suggestions?
patrick
+1  A: 

Typically I think this can be resolved by forcing a clear:both rule on the last child-element of the #items_list.

You can either use:

#items_list:last-child {clear: both;}

Or, if you're using a dynamic language, add an additional class to the last element generated in whatever loop creates the list itself, so you end up with something in your html like:

and css

.last_list_item {clear: both; }
David Thomas
A: 

Thw following should work:

.main #main_content {
    padding: 5px;
    margin: 0px;
    overflow: auto;
    width: 100%; //for some explorer browsers to trigger hasLayout
}
Steerpike
adding overflow: auto worked, although if I add also width: 100%, that causes the div (#main_content) to be slightly bigger than it's parent div. Didn't get why!
patrick
A: 

Don't use float:left

BenB
+3  A: 

You need to force a clear:both before the #main_content div is closed... I would probably move the <br class="clear" /> into the #main_content div and set the CSS to be:

.clear { clear: both; }
jennyfofenny