tags:

views:

227

answers:

2

I have the following code that mimics my problem. I want to style the elements within the "Container" div using percentages. Therefore I have to set the "Container" div to height 100% (or other value) to make this work. The problem arises when I want the "Container" div to change height to reflect contents. In the case below I'd want the height to be 200% (in this case that would work as I know the contents of "Container" but I have a datagrid in the real world case with varying numbers of rows). If I set height to auto then I can't change the child elements using percentages. Is there any way around this or am I set with fixed height containers?

Thanks,

Rich.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html style="height:100%">
<body style="height:100%">
<form style="height:100%">
<div id="Container" style="border:solid 5px;min-height:100%;height:100%">
<div style="height:100%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
<div style="height:10%; color:red"> TEST </div>
</div>
</form>
</body>
</html>
+1  A: 

From your description, this sounds like tabular data. Use a table, which will adjust to fit its contents without any effort on your part.

Ray
I should have said that I'm not using tables - this is a pure CSS solution. Thanks for the answer though.
This is one of those places where I don't get the insistence on CSS-based layouts. A table will do what you want, free, with no fuss. To do it with CSS, you have to battle dynamic content sizing and browser differences, search the web for ideas, post on SO and hope for answers. And then if/when you get it working, it cost a lot more, and it looks just like a table.
Ray
A: 

If you put a "spacer" div at the bottom of the Container div, then the container div should expand and contract as the contents do.

@Ray - if web design were meant to be table-based, all the other stuff wouldn't have been created. There are quite a few things you don't get with table-based design that are important. Two things that come to mind are optimal SEO and Section 508 compliance.

tahdhaze09
Could you explain in more depth with regards to the spacer div? As I have it at the moment only the first "TEST" div fits into the container (as it is 100% high) and the rest leak out underneath. In other words it obeys the height and not min-height property of the container. If I take out the height property of the container the "TEST" divs don't have the correct height spacing as they cannot calculate their height against the parent (container) div because I've removed it!Thanks again,Rich.
That should read cannot calculate their height against the parent (container) div's height because I've removed it!