CSS play - F.a.S.T - Fit and Shrink Technique

11th April 2010

In collaboration with Román Cortés

The Problem

How to fit images of unknown sizes into fixed width and height boxes and maintain their aspect ratio using just CSS.
The four sample images shown below are actual size.

The Answer

The boxes below are 180 pixels square and all use the same code and styling to show how the above images can be styled to fit, and shrink to fit.

Tested and working in IE7, IE7 emulation, IE8, Firefox, Opera, Safari(PC), Chrome, Flock, SeaMonkey, Avant and Maxthon.
IE6 shows all the images correctly sized but does not center the image when it is smaller than the containing box.

copyright © stu nicholls - CSS play


This demonstration is produced in collaboration with Román Cortés who came to me with the idea and a working demonstration that had a few flaws.

I have taken the code and restyled it to produce a version that now works in all modern browsers including IE6 and IE7.

The idea is based on the problem of how to fit images of unknown size into a fixed size containing box, such as used when uploading images for display without having to use javascript to find and manipulate the image size.

With this method an image that is smaller than the box size will be centerd horizontally, one that is the same size as the box will fit exactly, an image that is taller (portrait) than the box will shrink to fit the box height and will be centered horizontally and finally an image that is wider (lanscape) than the box will shrink to fit the box width.

14th April 2010

CSS simplified to remove need for separate IE stylesheet


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

Soft Geometry Creative Industries

Soft Geometry Creative Industries
is a music-based design company located in Bristol UK.
I create artwork for albums, singles, posters and t-shirts.
My work is influenced by punk and post-punk poster and flyer design,
lo-fi photocopy aesthetics,
Francis Bacon and Mark Rothko.

I can provide a start to finish service with my own art and photography, put together work from images provided by yourself, and collaborate on an idea.

Follow CSS play

Facebook   Twitter   Facebook Fan Page