
Originally Posted by
RyanReese
Margin:auto does not work in absolute elements.
Margin :auto doesn't work on absolute elements in iE6 and 7 
It works in good browsers and is a little known fact but you need to also set the left and right positions at the same time and for the element to have a width.
e.g.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
div{
position:absolute;
left:0;
right:0;
width:100px;
height:100px;
background:red;
margin:auto
}
* html div{left:50%;margin-left:-50px}
*+html div{left:50%;margin-left:-50px}
</style>
</head>
<body>
<div>test</div>
</body>
</html>
The trick is the left:0 and right:0 which would normally mean the element is 100% wide but because it has a width already defined then the auto margins can centre it within that original left:0 and right:0 space.
Bookmarks