KeyLimeTie Blog

Use CSS to AutoSize a DIV

By Brian Pautsch – 8/11/2008. Posted to Code Snippets.

There are often cases where you want to display content in a div, but a fixed height won't work. It the height is too little, content is cut off or you need a vertical scrollbar (by setting the overflow value). If the height is too much, you'll have a lot of whitespace. The example below shows you how to easily create a div that autosizes based on the content.
1//HTML:
2<div id="outerDiv" class="autosize">
3    <div id="innerDiv" runat="server" style="display: block">
4        //Content...
5    </div>
6</div>
7
8//CSS:
9#outerDiv
10{
11    min-height: 200px; 
12}
13
14div.autosize { display: table; width: 1px; }
15div.autosize > div { display: table-cell; }

Photos on Flickr

More Photos »

Search Blog


Archives