views:

66

answers:

1

In IE7 I'd like to add some margin to a element that has 100% width of it's parent. But the element overflows it's parent. Somewhat logical, but what's a solution to this problem?

I added a simplified example, where the yellow div overflows it's green parent.

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>Test</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head>
<body>

<div style="width: 500px; height: 200px; background-color: green;">
 <div style="width: 100%; height: 100%; margin: 10px ; background-color: yellow;">
  Test
 </div>
</div>

</body>
</html>

Thanks.

+2  A: 

If you just remove the width: 100%; declaration you're all set:

<div style="width: 500px; height: 200px; background-color: green;">
 <div style="height: 100%; margin: 10px ; background-color: yellow;">
  Test
 </div>
</div>

The default behavior is to stretch to the full width of the container.

Nick Craver
Allright, almost there! Actually height 100% also causes to overflow the green div. Without the 100% height, it's height is reduced to the height of the text line. In my case, that's not a solution. So: what is the best solution to get the same results for the height? Thanks!
Monty
@Monty - To do that, you're going to need to give the inner div `height: 180px;`, since you know the height of the outer div, this is the safest route.
Nick Craver
@Nick, can't seem to find any other decent way indeed. Best solution, thanks.
Monty