tags:

views:

169

answers:

4
+5  A: 

You can change the behaviour of how parent blocks deal with floated content by changing the overflow property. This should do it:

#main_contentbox { overflow: hidden; }
cletus
:o how can i have missed that .. +10 (if i could) i always used the clear:both technique ...
Gaby
Thanks bud, it worked.
Dan
This particular feature of `overflow` isn't *that* well-known.
cletus
A: 

Try something with JavaScript maybe:

window.onload = FixMyDiv();

function FixMyDiv()
{
  var divh = document.getElementById('main_contentbox').style.offsetHeight;
  var imgh = document.getElementById('sample').style.offsetHeight;

  if(divh < imgh + 50) document.getElementById('main_contentbox').style.height = imgh + 50;


}
Joel Etherton
Not to knock javascript, it definitely has its place, but I was looking for a purely CSS solution because this is problem is strictly design and layout based.
Dan
+1  A: 

Here's an interesting link with some common CSS Gotchas. Number 2 offers an alternative solution to the one given here (ie the clearfix mentioned by Gaby) and the rest are also great to know about and keep in mind.

Veli
thank you very much, these are helpful
Dan
+1  A: 

Using an :after pseudoclass, you can have the named div automatically append a clear fix. Add this to your css file:

#main_contentbox:after {
   content: "Foo";
   visibility: hidden;
   display: block;
   height: 0px;
   clear: both;
}

With that in place, you don't have to do anything to force the main_contentbox to grow to contain its floats, no matter what page you're on.

monksp
I will have to look into this further, it seems that with CSS there are many many hacks or quick fix ways of doing things as well, but I am concerned with learning the right ways.
Dan
Not sure what qualifies for a "right" way of doing things, but I use float a -lot- at work, and having a couple of these :after pseudoclasses, I never have to think about it when designing. Add elements with floated classes and the container classes always know to do the right thing. Clears seem to be a fairly common way to achieve this (I know at least Wikipedia's default skin does this with a dedicated clear class). My solution is just an extrapolation of that.
monksp