skip to content

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

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, Firefox, Opera, Mozilla, Netscape etc. but does require two different styles, one for IE and the other for non-IE browsers.

IE uses inline-blocks which is a valid style (although the w3.org validator does not yet recognise this) 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.




CSS play recommend

Flash LoadedCreative stock imagesPSD to HTML, PSD to XHTML Service by PSD2HTML.com. You Design - We XHTML / CSS.SitegrinderFree, practical CSS menus, layouts, and examplesSocial Media Applications On DemandBitrix Site Manager