Forum Moderators: not2easy
I have a container DIV outside of two smaller DIVs, like this:
<div id="content">
<div id="mainContent">
here's where the main content goes...
</div>
<div id="sideBar">
sidebar content
</div>
</div> And the CSS formatting for this goes as follows (the colors having been changed for this question):
#content {
padding: 10px;
margin: 0;
background: #000000;
}
#mainContent {
float: left;
background: #FF00FF;
width: 500px;
padding: 10px;
}
#sideBar {
float: right;
background: #00FF00;
padding: 10px;
width: 180px;
} The outside container DIV does not surround the two inner DIVs with its background completely. Why is this, and how can I fix this?
I appreciate any advice you could give.
Why is this
You may notice that in IE everything looks just as you expected. This is because IE has an auto-enclosing behavior. It will always allow a floated element to effect the height of it's container, even if you don't want it to.
how can I fix this?
The first, and easiest, IMO, is to float the container, itself. by applying a float property to the container, you force it to expand and contain it's children.
If, for some reason, you cannot float the container without making the rest of the layout go south, you can add a clearing element to the bottom of the container, AFTER the two floats but before the container's closing tag. Most people use a <br /> or an <hr /> styled to 0 height. Either way,, give it a clear:both and it will help prop open the container so the background shows through.
cEM
PS: Welcome to WebmasterWorld!
Next, try specifying a width for the container, which it should have anyway if it's being floated. You want to make sure it is wide enough to hold all of the elements, plus their padding, margins and borders. Calculated on the code you posted above, I would try 720px. If that doesn't work, try 725px. Note that this assumes a valid doctype, which causes IE6/Win to function in standards mode and use the same box model as FF. For lower versions of IE, you will have to hack or workaround the box model to get this layout displaying the same.
cEM