views:

744

answers:

6

Hello, I have this simple xhtml:

<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;">
<head>
</head>
<body style="height: 100%;">
  <div style="height: 100%; overflow: auto; background-color: #00f;">
    <div style="height: 400px; margin-bottom: 2000px; background-color: #f00;"></div>
  </div>
</body>
</html>

It shows two rectangles - but the lower (blue) one doesn't show the 2000px margin. It is only as tall as the window. Why? Or more importantly, how do I make it to show? I can add some dummy text like 'aaa' between those two </div> </div> but I don't feel that's the right way of doing it.

Thanks a lot for help!

+1  A: 

If you want 2000px of blue beneath the 400px div, make this change:

Change the height of the containing div to be the height of it's contents + the 2000px space.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;">

<head> </head>

<body style="height: 100%;">

  <div style="height: 2400px; overflow: auto; background-color: #00f;">

    <div style="height: 400px; background-color: #f00;">
    </div>

  </div>

</body>

</html>
dylanfm
A: 

Thanks for the reply but I need the margin to show up because the height of red rectangle changes. I just want constant margin beneath... and it get's collapsed but I don't know why. I tried padding:1px, didn't help....

+1  A: 

Remove your height: 100% from <body>:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;">
<head>
</head>
<body>
  <div style="height: 100%; overflow: auto; background-color: #00f;">
    <div style="height: 400px; margin-bottom: 2000px; background-color: #f00;"></div>
  </div>
</body>
</html>
strager
A: 

Get rid of the height declaration in the blue background div.

<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;">
<head>
</head>
<body style="height: 100%;">
  <div style="background-color: #00f;overflow:auto">
    <div style="height: 400px; margin-bottom: 2000px; background-color: #f00;"></div>
      </div>
</body>
</html>
A: 

It's because you have a height set on the containing div. Margins aren't content, so they don't trigger overflow. You could add another div around the innermost div, and give it a 1px padding, or, as others have suggested, you could adjust your existing containing divs.

Christopher Swasey
A: