Domain Empire

How can I center a HTML div using px?

Spaceship Spaceship
Watch
How can I center a HTML div using px?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Why would you want to use pixels?
 
0
•••
0
•••
0
•••
was posted in "Development wanted" dont play the monkey :P

Now that it was moved I can answer.

one of the ways to center a div using pixels, it must be absolute positioned INSIDE a relative positioned element with a fixed width.

<style>
.a{position:relative;width:800px;margin:0 auto;background:#dddddd}
.b{position:absolute;top:300px;left:300px; width:200px ;background:#fefefe}
</style>

<div class=a>
<div class=b>
This is centered
</div>
</div>

-----
you can center (or position anywhere you want it) editing the .b class "top" and "left" attributes
 
Last edited:
0
•••
was posted in "Development wanted" dont play the monkey :P

Now that it was moved I can answer.

one of the ways to center a div using pixels, it must be absolute positioned INSIDE a relative positioned element with a fixed width.

<style>
.a{position:relative;width:800px;margin:0 auto;background:#dddddd}
.b{position:absolute;top:300px;left:300px;width:200px;background:#fefefe}
</style>

<div class=a>
<div class=b>
This is centered
</div>
</div>

-----
you can center (or position anywhere you want it) editing the .b class "top" and "left" attributes

It was in the right place when I looked :)

If the height is not fixed and dynamic you'll have to use JS. An example of sorts (specifically a picture in a div) is here.. to give you an idea..

http://jsfiddle.net/z334b/2/
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back