PDA

View Full Version : Centering absolute div horizontally?



pxlcreations
Jul 10th, 2010, 05:28 PM
Hey guys, I have a question about centering an absolute div. I have multiple divs set up like this:



<div id="div1" class="draggable">

<!--Content Start -->
<span id="clock">*</span>
<!--Content End -->

</div>


and my CSS goes like this:


.draggable{
z-index: 1000;
display:none;
margin:0 auto;
}

#div1 {
height:219px;
width:252px;
color:black;
}


And the DIVs get dragged around the page via JavaScript but it needs it to be absolute positioning. So I don't know how to keep those divs all centered in the screen using the code above.

Thank you!

abduraooft
Jul 10th, 2010, 05:40 PM
I have multiple divs set up like this: Is it a fixed/variable count?
And the DIVs get dragged around the page via JavaScript but it needs it to be absolute positioning.Are they all to be aligned vertically or horizontally?

pxlcreations
Jul 10th, 2010, 06:05 PM
Is it a fixed/variable count?

The number of divs is growing so there is not fixed number.


Are they all to be aligned vertically or horizontally?

They are all to be aligned horizontally.

abduraooft
Jul 10th, 2010, 08:05 PM
Then you might need to dynamically calculate the positions depending upon the width/count of elements and maximum available space.

pxlcreations
Jul 10th, 2010, 08:15 PM
So there isn't anything that can be done like width:0 auto;?

abduraooft
Jul 11th, 2010, 01:50 PM
So there isn't anything that can be done like width:0 auto;?
Hope you mean margin:0 auto;. No, that won't play well with absolutely positioned elements. Besides that, you've an indefinite number of such elements.