Centering an image of unknown size in an outer container of known size

15th October 2006

The image below should be centered within the outer gray box without specifying the dimensions of the image

Homework

copyright © stu nicholls - CSS play

Information

I get lots of emails asking how to vertically and horizontally align images within outer containers when the image size is unknown.

Well this is one answer that works in IE5.x, IE6, IE7, IE8, Firefox, Opera, Mozilla, Safari(PC) and Google Chrome, but does require two different styles, one for IE 5, 6 and 7 and the other for non-IE browsers and IE8.

IE 5, 6 and 7 uses inline-blocks and non-IE browsers use table-cell and vertical-align to emulate the table method.


Copyright

You may use this method on your personal 'non-profit' web site without seeking my permission. A link back to CSSplay is always appreciated.

Commercial usage is also permitted without seeking approval, but I would ask that a donation is considered to support my work on CSSPlay.

If you are having problems integrating any of my demonstrations into your website then I now offer a service to fault find and correct any errors that you may have introduced. Please email me for more information.



Please Support CSS play

Please note that if your donation is for a demo/menu then you will need to email me with the url of the demo/menu after making the donation

For more CSS demonstrations I recommend that you visit the Dynamic Drive CSS Library.


Free, practical CSS menus, layouts, and examples

Follow CSS play


Facebook   Twitter   Facebook Fan Page