If the Content partition is a static block (a regular, non-positioned block element like a div), and it has floated children inside it (it does now, you’ve floated them), then Content can’t see that it has children anymore. It thinks it’s empty, and so does not enclose it’s children anymore.
This is normal, and what happens with floated children inside static boxes.
There are lots and lots of solutions to this. I’ll give you the two I think would be safest for you, then maybe I’ll list some more so you know all later possibilities.
The Quick Dirty Easy way is to set the Content partition to “overflow: hidden”. By default, a static parent box is set to overflow: visible, and so long as its children aren’t floated or anything weird, the rules say that parent must “see” its children and enclose them.
When you tell the box “overflow: something-other-than-visible” (there is also “scroll” and “auto” values, but these will probably look ugly to you), now the parent box MUST consider if anyone is “overflowing” outside of it. Since we set it to hidden (or whatever), it must now be able to “see” those floated children; otherwise, how could it deal with overflow?
Because the parent can now “see” those floated children, it will enclose them like it should. Throw an ugly background colour or outline around Content/Parent and you’ll see it.
Overflow: hidden however does… hide overflow. If you have set dimensions on the Content/Parent like height or width, and any child inside needs to be bigger, then you’ll see them get cut off. Usually setting a width is ok because you’ve also set widths on your floated children and you calculated it all out and everyone inside carefully fits inside the parent, so no overflow. But if you have set a height on the parent, remove it. Let the children inside determine the height automatically. So now no cutoff.
Option 2: What if you really did want something to stick out of Content/Parent? Maybe an image that’s absolutely positioned, or some abso-po’d popup box shows up when users click on something and it might need to stick out from the Content/Parent box. Then overflow: hidden will bite you and you’ll need another solution.
A popular one is a modified version of Tony Aslett’s “clearfix”. Unfortunately lots of nasty hacky codes are out there under this name (the original was long because it dealt with IE5 for Mac and whatnot, but it was also brilliant). I’ll give you a clean one.
You take the Content/Parent, and give it a “fake” child who’s not floated. Since it’s not a float, it counts as a child who must be “seen” by the parent, and so the parent will enclose this one. But the fake child must be “under” the floats, so it has to clear them (you know float clearing? if not, read up on it, you’ll need to understand it well to use floats). When we create this fake child, it’s an inline element by default and inlines can’t clear stuff, so we’ll have to make it a block element. We’re using the :after pseudo element.
Let’s call Content #parent.
Your original parent code:
#parent {
all your styles;
}
#parent:after {
content: " "; /* an empty space. You have to have something as content. Some browsers are okay with a totally empty string but not all, so a space will do it. You can also use nonbreaking (content: "\\0a";) if you want to see it easier*/
display: block; /*only block elements can clear*/
clear: both; /*you have floats in both directions, but also this is often safer than clearing one direction anyway*/
height: 0; /*there are many variations on this, but this one has worked for me cross-browser: don't let this little bit of content actually show up or take any room on the page*/
}
This will work on most browsers, excluding the ones who don’t know these pseudo elements. Which are IE6 and IE7.
Are you supporting IE 6 or 7?
If you are supporting those, then you’ll also trigger HasLayout property on the Parent (Haslayout is not on non-compat-mode IE8).
If you had set a width on #parent already then your job is done!
But if you didn’t, setting a dimension like width will do the trick. If your parent was naturally 100% width of the page and you didn’t add any stuff to the sides like side padding, side margins or side borders, then you could safely get away with adding
width: 100%;
just for IE6 and 7, but without hacks and out in the open since it can be safe (almost redundant) for all other browsers.
If setting a width isn’t an option, you could use one of the old IE6 hacks for feeding info just to that. However you’re likely not supporting that one. If you are supporting IE7 though and not 6, it’s easier: there are more ways to trigger Haslayout.
Setting a min-height or min-width (I like min-height) will trigger haslayout in IE7. It just so happens that setting overflow to something other than “visible” does it too, which is why overflow: hidden works in IE7 (not because parents must see overflowing children, but because HasLayout demands it. IE is silly).