RK* - rikkertkoppes.com

thoughts

[HTML] image descriptions: dl or label?

In addition to: [HTML] usefull element constructions:

Sometimes when you want to add a description to an image, you find out you do not have the proper elements to do this, for example (trivial atributes omitted):

<img>
  <caption>image description</caption>
</img>

is no valid HTML. And this:

<p>
  <img>
  image description
</p>

is semantically a mess (the above is not quite a paragraph do you think?). A way to solve this is to wrap it in a dl:

<dl>
  <dt><img></dl>
  <dd>image description</dd>
</dl>

This will bind the description more semantically to the image. You might want to specify a class for it to, because you use it for different purposes than you normally would. Furthermore, you could use this class to hook up a style.

It just occurred to me that there's another way to do this:

<label>
  <img>
  image description
</label>

Which is not preferred, but you can also make a reference like this:

<label for="imgID">image description</label>
<img id="imgID">

Frankly, I do like the last method. This is all ok by specification, a label element may contain inline content, which on its turn may contain special content. And img is special content.

The only thing I encountered is that label specification talks about it's use on (form) controls, while img is not a control.

Update 2005-05-21

It just occurred to me that the best way to do this is actually CSS. The title attribute of an <img> is the perfect place for a desciption, so say you have:

<img src="foo.gif" title="the logical structure of foo" alt="[something suitable here]">

And some CSS like:

img[title] {
	display: block;
	counter-increment: figure;
}
img[title]:after {
	content: 'figure ', counter(figure), ': ', attr(title);
	display: block;
}

This adds a numbering, and description to the element and it only affects images with the title attribute set, because I might like to add other images in a normal way as well.

Unfortunately, this does only work in Opera for now. Firefox does not support counters in generated content and moreover, it does not support generated content for the <img> element for some reason. I don't think I have to mention about Internet Explorer.

Further reading

Additional resources (top 15)

Below is a list of additional resources that might contain extra information about the subject at hand. These are all sites linking to this one (i.e. backtracking).

  1. MSN Search: Vyhľadať google [en] (21)
older articles

AdministrationAtom feed