...

View Full Version : Get div to fit content



Keleth
08-04-2011, 11:39 PM
I have a div in which I have a number of floated divs, creating a number of rows within. The way its setup (using a few clears to arrange the layout the way I want), with height of the parent is up to the top of the last row, and expands the entire width of the page (as expected).

I'm trying to figure out if there is a way to get the div to fit exactly the content that's within it. Because of the way its setup, I can calculate the height/width (the number of rows and their width are determined by the user) and give the parent div a hard set height/width, but that's adding a step I'd rather avoid.

Anyone know of a way to get the div, given it has floading divs within it, to fit the content? Or am I going to have to hard code it?

Small note, I am aware of using a clear: both at the bottom to have the vertical content fit (if just slightly over), but I'm not aware of that fixing the horizontal fit, and I'm more curious if there is a method of getting an exact/nearly exact fit, rather then a close fit.

Lerura
08-05-2011, 04:05 AM
You can just float the container div too, to make it fit the width of the content
setting no height, will make the div fit the height.

alykins
08-05-2011, 04:17 AM
if u remove all height attributes it will by default expand to fit around it's children. until there is content the divs will be 0px high though... you may want to give everything a "hard-coded" height of something using the min-height property to assist you visually in coding

Keleth
08-05-2011, 04:44 AM
You can just float the container div too, to make it fit the width of the content
setting no height, will make the div fit the height.

I.... I can't believe I never tried a float within a float... that's crazy!

Thanks man, works perfectly, or so it seems :) I take all browsers will take it that way? I guess, to the testing!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum