tags:

views:

279

answers:

3

Hi! I need to read CSS settings for the div's width and height etc in jQuery to arrange the divs (hundreds of them) in proper position in the page.

I know it is possible to read an existing div's actual size/position. But my requirement is to get the info before the div even exists. Anybody? Thanks!

Sorry, I didn't make it clear enough. Here is my situation:

I have a big parent div, which is going to hold multiple rows and columns of small divs.

The parent div is like 1000 x 1000 px. The small divs like 15 x 10 but it is configed in CSS, and its real value I don't know.

I need to calculate how many row and columns to array the matrix of divs. So that I can loop through to arrange them in the parent div.

Hope this is a better description.

+1  A: 

Like this?

var myDiv = $("<div></div>").css({properties});

$("body").append(myDiv);
Jonathan Sampson
+1, you did answer this before me
karim79
+1  A: 

If the div does not exist, how do you intend to get some of its properties? If you're talking about setting attributes such as height and width on the fly whilst creating your divs that would be a different story:

var div = $('<div>Blahblahh</div>').css("height","100px");
karim79
You can create a new element and not apply it to the page yet. Just assign it to a variable, and then wrap it with jQuery. I believe he means "doesn't exist" as simply not on the page.
Nerdling
Nerding: Thank you, I think your comment solved my problem.
+2  A: 

Have a look at the compatibility page for DOM CSS. You should be able to access the rules of a stylesheet before any divs are created on the page, and without creating "dummy" divs. For example,

var height = document.styleSheets[0].cssRules[0].style.height;

That would show you the height value of the first rule in the first stylesheet.

Matt Bridges