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

CSS and Images: Simple Roll-over Image Gallery

If you have been reading these articles in order, this lesson gets a little ahead of the concepts introduced so far, and uses properties (primarily ) that have not yet been thoroughly explained, I find it a useful example to wrap up a course / semester.

Simple CSS Gallery ScreenshotThere are many, many different possible methods to create image galleries. Many people use complex , when all is required, in most cases, is some simple combined with semantic markup.

What I’m about to show you is a synthesis of several techniques combined with a few of my personal additions. To complete this exercise, you’ll need three thumbnail photographic images, ideally all the same size, and three matching large versions of those same images. For clarity, save the large versions of the images as subject.jpg and the small versions as thumb_subject.jpg.

Once we have our content, the question is what markup makes the most semantic sense for what we are trying to express? What we desire to create is a set of thumbnail images that, when interacted with, reveal larger versions of themselves with an explanation of their context and meaning. Once again, the incredibly useful (and relatively unknown / unappreciated) definition list comes to our aid: we are, at heart, talking about a series of terms (which could be words, images, links, or anything else) that are expanded upon with a definition declaration.

So our markup is fairly straightforward:

  1. <dl id=gallery>
  2. <dt><img src=thumb_maple.jpg alt="Maple tree">
  3. <dd><img src=maple.jpg alt="Maple tree">
  4. <dt><img src=thumb_oak.jpg alt="An oak tree">
  5. <dd><img src=oak.jpg alt="Oak">
  6. <dt><img src=thumb_sequoia.jpg alt="Fallen sequoia">
  7. <dd><img src=sequoia.jpg alt="Fallen sequoia">
  8. </dl>

(Note that for the purpose of this exercise I am assuming that every thumbnail is the same size, so I could put their width and height as part of a dl#gallery dt img { } declaration in a style sheet if I wanted to. If the thumbnails were all different sizes, you would put the width and height as separate inline styles for each image, as you normally would for the large images. I am also assuming that all images are placed in an images folder next to this HTML page.)

On viewing the web page in our browser we’re immediately confronted with several problems. The first is that our large images are visible, and we only want them to appear when the user hovers over a thumbnail image.

There are many CSS properties that can be used to make elements on a web page “disappear”: display, opacity, pushing things off the screen with margin or position, and more. We will use visibility:

  1. dl#gallery dd { visibility: hidden; }

That hides our large images, but preserves the space they take on-screen. We’ll get around this by positioning the dd elements absolutely:

  1. dl#gallery dd { visibility: hidden; position: absolute; }

Using absolute positioning on elements takes them out of the “flow” of the document: the web page acts like the definition declarations are no longer there, and collapses the remaining structure of our document. (The <dd> elements and the images they contain are still present, of course, simply hidden by our visibility: hidden rule. You could see where the images are by removing or commenting out that part of the style declaration.)

position: absolute is very useful, but very dangerous – if not used carefully, it leads to positioning every element on the page absolutely, due to designer’s misplaced desire for “pixel-perfect layout” and the aforementioned collapse due to the removal of absolutely positioned elements from the flow of the document. position: absolute should, as a general rule, be avoided unless it is absolutely required – and when used, you should know why you are using it, rather than “it just works this way”.

In this case, position: absolute is used for an eminently logical reason: we want all of our large images to appear in the same location. Right now, they are not (again, turn off visibility: hidden temporarily to see where they are). In addition, we want the position of the large images to be measured in respect to their relation to the definition list as a whole. This is a two-step process. First, some CSS for the definition list itself:

  1. dl#gallery { position: relative; }

The reason for this line will be discussed in future entries – for now, the rule is:

Absolutely positioned elements position themselves against the origin of the body (i.e. the top left corner of the web page), unless they are within another absolutely positioned or relatively positioned element.

In essence, we’ve told the absolutely positioned <dd> elements to measure their position against the definition list, rather than the body. We can see this by once more temporarily commenting out the visibility of the <dd> elements and adding more CSS to the style declaration:

  1. dl#gallery dd { /* visibility: hidden; */ position: absolute; top: 20px;
  2. left: 200px; }

Naturally the numbers for left and top are guesses; you should feel free to adjust them to suit your particular design before turning visibility: hidden back on.

The penultimate step is to make the <dd> elements appear only when we are hovering over the thumbnail images. Looking at our markup, we can see that each <dd> element is immediately preceded by a <dt>. We also recall that :hover can be applied to any element, not . Ergo, the solution is a combination:

  1. dl#gallery dt:hover + dd { visibility: visible; }

This CSS solution is a good one, with just one remaining problem: you can make the large images visible by moving your mouse to the right of the thumbnail images, where the <dt> is still active. This is due to the fact that <dt> is a block tag, and therefore stretches all the way across the page by default. But if it’s a block tag, we can apply width to it. Assuming that your thumbnail images are all 75 pixels wide:

  1. dl#gallery dt { width: 75px; }

The complete CSS code:

  1. dl#gallery { position: relative; }
  2. dl#gallery dt { width: 75px; }
  3. dl#gallery dd { visibility: hidden; position: absolute; top: 20px; left: 200px; }
  4. dl#gallery dt:hover + dd { visibility: visible; }

You must be signed up in order to leave comments.

How would you have a place holder image so there isn't a big blank space next to the thumbs? Thanks

posted by SimCat

Dudley StoreyIf you mean placing a large image by default in the page, SimCat, that's a little more complicated, at least only using CSS. (I've discussed how to do so with PHP elsewhere in this blog). Let me try to pull together an example for you - you can expect to see it in the next few days.

posted by Dudley Storey

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.