demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

Use CSS to Auto-Generate Image Thumbnails

Auto-generated image thumbnailsTalking about the concepts of CSS filters with my class yesterday, I realized that one could use the clip or overflow properties to automatically generate thumbnails for a web image gallery. In most gallery designs it’s important for thumbnails to be exactly the same dimension or aspect ratio, and creating each thumbnail often takes a series of steps in an image editor.

Taking full-size gallery images and clipping them to size as navigational images would eliminate those steps, lowering page creation time. It could also decrease page download time: rather than loading both the thumbnails and the full-size images, you would only have to load a single set of large images once.

To achieve this, we would load the full-size images into our gallery navigation (I’m using minified (but still valid) in the code below to save on space).

  1. <ul id=gallery-nav>
  2. <li><a href=#><img src=sunset-muriwai-beach.jpg alt="Muriwai Beach"></a>
  3. <li><a href=#><img src=big-wheel.jpg alt="Sunset behind ferris wheel"></a>
  4. <li><a href=#><img src=cornwall-sunset.jpg alt="Sunset In Cornwall"></a>
  5. </ul>

Note that the images all have different sizes and aspect ratios, and that they lack width and height. I will add a little to the gallery navigation as a whole:

  1. ul#gallery-nav { list-style: none; margin-left: 0; }
  2. ul#gallery-nav li a { display: block; width: 100px; height: 100px;
  3. margin-bottom: 20px; }

I’ve set display:block on the a elements in order to provide them with a width and height (as an inline tag, links will not take dimensions by default). These match the dimensions we’re about to apply with clip.

  1. ul#gallery-nav li a img { border: none; position: absolute;
  2. clip: rect(0px 100px 100px 0px); height: 200px; }

Elements that we apply clip to must be positioned absolutely. Our clip measurements make the thumbnails square, although you could alter the aspect ratio to be whatever you wished. Note that we’re using the non-spec version of clip without commas between the values to make it work in IE. I’ve used a technique borrowed from responsive design to set the images to all the same height, without needing to specify the width.

An alternative approach, if you don’t want to use clip:

  1. ul#gallery-nav { list-style: none; margin-left: 0; }
  2. ul#gallery-nav li a { display: block; width: 100px; height: 100px;
  3. margin-bottom: 20px; overflow: hidden; }
  4. ul#gallery-nav li a img { border: none; }

While it’s very useful, there are a few drawbacks to this technique that should be noted:

  • This approach is only really practical for galleries with a dozen images or less. Anything more and the thumbnails will probably take too long to generate (as the page is loading in every full-size image before shrinking and clipping it).

  • Perhaps most importantly, there’s no aesthetic control over how individual thumbnails are cropped: the clip area always starts and stops at the same points. Essentially, this is a batch processing technique for cropping images. The only way around this limitation would be to write inline style changes to any image that didn’t look quite right.

You could combine this approach with any number of PHP directory read techniques that I’ve talked about on this blog to auto-generate the entire gallery content.

You must be signed up in order to leave comments.

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.