KOMPX.COM or COMPMISCELLANEA.COM   

CSS centering absolutely positioned elements

CSS horizontal centering of an absolutely positioned element. Example:

HTML code:


<div class="example">
	<img src="image.jpg" alt="" />
</div>

CSS code:


.example img {position: absolute; left: 0; right: 0; margin: auto;}

Note: the method also works if the width of an absolutely positioned element is indicated explicitly: in pixels, percent or other units.

Browser support
Windows
Internet Explorer 8.0+
Firefox 1.0+
Google Chrome
Opera 5.0+
Safari 3.1+
SeaMonkey 1.0+
Mozilla 1.5+
Netscape 8.01+
Linux
Firefox 1.0+
Google Chrome / Chromium
Opera 5.0+
SeaMonkey 1.0+
NetSurf 2.6+
Mozilla 1.5+
Netscape 8.01+
More