SimpleQuiz › Part XI › Image Floating

First things first, many thanks to all of the readers of SimpleBits for making 2003 a great year. It’s been fun chatting about standards-compliant design, the subtlties of minute markup comparisons and… beer. I’m already looking forward to continue with bigger, better things in 2004.

Figure 1
Figure 1

Mike Papageorge had written in a while back with a SimpleQuiz suggestion concerning the floating of an image and caption within content.
I thought it may be an interesting bit of markup to discuss, so I’ve whipped up the following question.

But first, take a look at Figure 1. Ha! By floating Figure 1, I’m demonstrating precisely the situation that this question refers to: An image floated to one side, with a descriptive caption underneath. Content text will wrap around the entire package.

Now, on to the question. While I’ve illustrated only three methods, there are surely many more combinations that could achieve simliar results. Try to take into account how an unstyled view of the different methods would appear as well.

Q: Which method makes the most sense when floating an image and caption within a body of text?

  1. <p class="floatimg">
      <img src="image.jpg" alt="good alt text here" /><br />
      Description goes here
  2. <div class="floatimg">
      <p><img src="image.jpg" alt="good alt text here" /></p>
      <p>Description goes here</p>
  3. <dl class="floatimg">
      <dt><img src="image.jpg" alt="good alt text here" /></dt>
      <dd>Description goes here</dd>


  1. Scott Plumlee says:

    C is the only option that represents some sort of relationship between the image and the caption. So I would go with that. A caption can be a definition of an image, although it doesn’t have to be.
    A doesn’t seem to embody the type of relation ship that exists between the image and caption. It contains the two items without clarifying the relationship at all. At least the definition list has a set format and relationship.
    Maybe HTML needs some way to express the link between images and the text that often appears with them? Like an visible title attribute? Tables have a caption attribute, why not images?

  2. aliotsy says:

    I agree with Scott, C makes the most sense. I was wondering though, should the alt text match the dd text?

  3. says:

    I would have to go with B, but without the paragraph tags. They are not needed as the image can be styled with display: block;, thus wrapping the discription text to the next line.

  4. web says:

    B would make sence to me if there were not paragraph tag’s around the image. So D it is.
    I love Daisy’s, w00t

  5. Nick says:

    i agree with use b and get rid of the paragraph tags. something like….

    .floatimg {
    float: right;
    text-align: center;
    font-size: small;
    .floatimg img {
    display: block;

    of course, if we were allowed to use the <caption> tag outside of tables, this would be a good occassion.

  6. Jake says:

    The W3C likes P’s (whatever that’s worth).

  7. The same thing could be accomplished with A and no p tags. If I’ve got several paragraphs in a row I’d hate to throw a div in there. Seems like the image/caption is just a another paragraph anyway.
    BTW you float doesn’t in IE5/Mac

  8. Gerrit Kaiser says:

    Out of these three, I would go for “B”.
    But in my daily use, I’d rather use something like
    <div class=”floatimg”>
    <img src=”" />
    <span class=”caption”>Caption</span>
    I think neither an Image nor its caption should be regarded as a paragraph and thus shouldn’t be markup up as one. Besides, why put another element around the Image?

  9. stew says:

    aliosty – for good accessibility your caption shouldn’t be teh same as your alt text because people with screenreaders and non-visual browsers can be informed of the caption and the alt tag so it doesn’t make sense to have them the same and its also against some accesibility guidelines.
    anyway back to the topic at hand,
    i reckon A would be nicer in matinence but C makes sense semantically

  10. Tobias says:

    The alt text is not the same as the caption, as the alt text should be shown instead of the image, while the caption goes with the image.

  11. nick says:

    I never thought about the definition thing. I like that one best, demonstrating the relationship and what-not. But I’m open to just about anything that doesn’t use a <br />.

  12. Alex H. says:

    I’ve pretty much ruled out option A, as the <br /> tag is almost entirely unnecessary.
    Option B doesn’t make sense, semantically, because an image is not a paragraph, and the caption usually isn’t either.
    As Scott said, C is the only option that represents a relationship between the image and the caption. However, it’s not really a definition “list,” at least not in classical sense. Even so, this option makes the most sense to me, and is probably the one I’d use.

  13. simon says:

    I’d get rid of the p tag around the img but keep the p tag around the caption.

  14. jason says:

    The semantic relationship of C makes the most sense “when floating img and caption within a body of text”. Being inside a body of text, p tags are already being used. The floated image gives visual meaning to the text, so I think that a <dl> fits this example.

  15. Ilari Sani says:

    I’d love to give the caption as the image’s longdesc, then use :after to display it. Alas, this will not work in that one browser.

  16. Jim says:

    I agree with Ilari, except you are thinking of the title attribute, not longdesc (longdesc should be a URI pointing to a longer description).

    Also, even if you are using redundant content to cater to Internet Explorer, don’t misuse the <p> element type – an image and its caption are not two paragraphs.

  17. Corey says:

    I agree with Gerrit. B minus the <p> tags as illustrated in his “daily use” example is closer to what I would use. I would forgo any relationship between the image and the caption, but make the caption more descriptive so it could stand on it’s own…
    i.e. Figure 1 – Example of floating and image

  18. Anonymous says:

    (sub for ], sorry)
    The problem with using solution b and nixing the [p] tags around the [img] is that it convey’s no relationship between the two parts of the whole. Imagine that you are trying to glean the relationship between the two by just looking at the source code, or more practically, through a text based browser or screen reader.
    Now I don’t know how either of those two types of browsers handle [img/][p]…[/p] or [dl][dt][img/][/dt][dd][p]…[/p][/dd][/dl], but my guess is that if you chose C any browser would better be able to provide a relationship between the image and the caption. And isn’t that relationship all that we are trying to convey by floating the caption under the image?

  19. Kjell Olsen says:

    Did I forget my name?

  20. Lenny says:

    B is the closest to what I would do, but I would remove the <p> from the image and leave it for the caption.

  21. Paul says:

    I agree that B minus the <p>’s plus a little CSS to style makes the most sense, but what do non-CSS capable browsers see? You don’t want the text and the image on the same line.
    In that case I think it makes the mose sense to use B–but to only wrap the caption in a <p>, not the image.

  22. vlad says:

    hesitantly, i vote for c. as previously mentioned, it conveys the relationship between the image and the descriptive text better than the other 2 methods. furthermore, an image and its caption seem to fit the term-definition construct of <dl><dt /><dd /></dl> nicely.

  23. Brian says:

    Before I even saw the options I was thinking B, without the P tag around the image, but keep it for the caption(or use a span). Then I saw C, which makes total sense to keep the image and caption together. Since that image and caption is basically a thumbnail I think in most instances where I would use something to that effect the image would be linked. So this crossed my mind…

    .float {
    float: right;
    margin: 10px;
    .float span {
    display: block;
    margin-top: 10px;
    text-align: center;

    <a href="#" class="float"><img src="image.jpg" /><span>caption text</span></a>

    So if the thumbnail needed to be linked this would be slightly leaner than pulling in an additional div or definition list.
    So yeah, C. And my additional 2 cents.

  24. Shaun Inman says:

    Aliotsy: “should the alt text match the dd text?”
    In this instance I would say no. I think the alt should only attempt to convey something about any relevant content in the image that isn’t covered by the caption. Any instance of the alt being visible/read instead of the image would be accompanied by the caption making that redundancy…um, redundant. Right?

  25. Alex says:

    Why not D?
    <img src="image.jpg" alt="alt text" title="caption text" class="floating">
    along with the CSS rule:
    img.floating {
    content: attr(title);
    display: block;

    You can set margins and width somewhere else for display purposes. I like living on the bleeding edge.

  26. the attr(title) CSS styling isn’t always mutable enough.
    I always see tags being neglected and this isn’t an exception…
    Try small.
    <img src=”blah.jpg” />

  27. David House says:

    As I don’t care much for IE users, this is what I’d do:

    img.floating { float: right }
    /*display: block automatically assumed by parser*/
    img.floating:after {
    text-align: center;
    content: attr(title);}
    <img class="floating" title="Description" src="image.gif" />

  28. Vegard says:

    I don’t care much for the IE users either, but let’s face it: It’s still a hell of a lot of them out there. That makes the solution proposed by David and others a good one if you’re coding for your own personal I-reserve-the-right-to-choose-my-visitors-site, but a less good one if you’re doing commercial work.
    In short: The solution depends on your target audience.

  29. Ben Pirt says:

    Just thought I would cast my vote for option B, but without the <p> tag around the image. I know the w3c use this style, but it doesn’t really make that much sense to me to have a paragraph tag around an image.
    I’m not really convinced that the caption is the definition of an image as in C, but in the cases where it is then this too would make sense.
    Using :after would be a great solution (maybe in a few years time though)

  30. Political says:

    I think C actually. I don’t see why a caption couldn’t be considered the definition of an image.

  31. Solution C. Although comment 25 is quite witty, I’d like to use the title tag for something else (such as telling users they can click the picture to get it in larger format).

  32. Mike says:

    Wow, thanks for including the suggestion Dan! I used ‘c’ in a website and like it for both the semantic value and the extra stylehook that comes with the DL. What worried me was is it really semantically correct?
    I know the way of the web moves slowly in terms of browsers, which ultimately have to render the document, but what happens if the W3C decides to tighten what DL is used for?
    (in xhtml 3.0, for example ;-] )
    At any rate, I vote ‘c’.
    (image:after is a great theory, but I was looking for a real-world solution. Paying clients care about users.)

  33. Guy says:

    It’s not a paragraph or a list, so I’d go with B. As others have suggested, I’d also try to eliminate the paragraph tags around the image.

  34. Chris Neale says:

    regarding the demonstration not displaying as intended in MacIE …
    don’t float declarations require an explicit width ?

  35. Mike says:

    I was wrestling with this problem a while back (when I submitted ‘c’ to Dan), and after reviewing what exactly I was wrestling with, some comments:
    As has been mentioned by Scott and Nick, a caption tag would be a great solution.
    Guy mentions that it is not a list, which is right, and neither is the markup – the definition list is not a normal list; although parhaps a stretch, it is an attempt at finding some semantic value to this situation that just happens to come with a great extra style hook.
    The problem with ‘b’ and removing the paragraph tags and resorting to an empty length of text or span tags (or small) has been documented elsewhere.
    Now, I am not sure I’d rather just refer to the figure in the text of the page, covering IE users, and then use the content: attr(title); idea. Perhaps a javascript workaround similar to Simon Willison’s Blockquote citations could get IE on board with this solution…

  36. Anne says:

    Wow, the suggestion made in comment 15 is just great!

    Too bad it is only possible with a working draft of CSS3, which is never implanted in any browser. Besides; content:url(something.htm); /*or .txt*/ will not be implanted in Mozilla, a bug about that is marked as WONTFIX. Maybe suggestions like this will get it open again :-)

  37. David House says:

    longdesc is meant to be a URI, a link to a long description of the image, not the description itself.

  38. Gerrit Kaiser says:

    A lot of you guys are preferring the <dl>-Solution due to the semantic information it conveys. But an Image isn’t exactly a list, is it?
    So why not use the following:
    <div class=”floatimg”>
    <dfn><img /></dfn>
    <div class=”caption”>Caption</div>
    Though the <dfn>-Element is rarely used, I think it fits here quite well. Essentially the caption IS defining the Image, isn’t it?
    Also, this example degrades nicely without adding line breaks or unsuitable markup. But think about the ugly rendering of an unstyled Definition-List…

  39. Mike says:

    But an Image isn’t exactly a list, is it?
    No, but from what I gather that’s not the matter here (wrt ‘c’) – what is of question is (imho), ‘are an image and it’s caption suitable for use in a definition list?’.
    Or… ‘does the caption ‘define’ the image?

  40. Dan says:

    “I’m already looking forward to continue with bigger, better things in 2004.”
    Bigger, better… beer? Woo-hoo!
    As to the question…
    During the course of coding my site, I had it set up at one time like option A. It’s the easiest one of them all if you don’t want it to do anything fancy.
    Option B seams like it would give you more controll over display due to the additional P tags.
    I wouldn’t use option C though as it’s not a list we’re making here. Quite frankly, both A and B would seam to work just fine unless there is something I’m overlooking here. I’ve only been coding CSS a few months now and am still very new to all this.
    Here’s the way I have mine right now for my sub-galleries.
    <p><a href="s-dunes1.html" title="Dunes I">I</a></p>
    I have the image set as a background image in CSS for a rollover effect. Granted it’s not the most archaic-browser friendly but this is a fairly different use of the image float than the standard image-within-text use.

  41. Anonymous says:

    I like comment 38 with the dfn tag, but what about using dfn as the caption.
    <div id=”fig1″>
    <img src=”image.jpg” alt=”good alt text here” />
    <dfn>Description goes here</dfn>

  42. Anonymous says:

    It’s a shame that <caption> is ‘wasted’ on tables… surely a table caption could have been <tc>, leaving <caption> open as a nice meaningfull tag for something so popular in DTP.
    Anyway, I don’t think <p>‘s or <dl>‘s are appropriate, but concede that <p> may be the best of a bad bunch.
    Given that the display of the image can be set to block, why do we need to mark-up the caption at all?

    <div class='captionedImage'>
    <img ...>
    This is my meaningfull caption.

    Styled with:

    div.captionedImage img {
    display: block;

    There’s also the option of using the content of either the alt or longdesc attributes as the caption using either CSS or JS, although there’s a few browser not up to it — but I’d question that as an excuse.

  43. Anonymous says:

    I would go with C. I consider a caption a definition to an image, so this makes sense.
    Ya, not much else to say that already hasn’t been said.

  44. Lar says:

    Vote ‘C’: but with regard to the image and caption, what about….
    Description of image

  45. Of course, C is the best choice if you want your markup to be semantically correct.
    However, it looks whacky in browsers and devices that do not understand CSS. And since there still a lot of web-enabled devices (phones, PDAs, refrigeraters :)) that are used to browse the web but don’t support CSS, I’d rather go for A. This is also the most compact solution and it looks good even without CSS-styling.
    About B: again, I’m concerned with the way it looks unstyled. The B method would look odd; virtually every graphic/textbrowser adds a margin between paragraphs, so there will be a gap between the image and the description. With method A there’s a clear visual relationship between the image and its description.
    When the day comes that most phones, PDAs and other web-enabled devices support CSS, I’d go for C. For now, it’l be A.

  46. cafe fort says:

    IMHO, it’s C the good one, because semantically spoken, DT and DD are the semantic tags specifing a definition.
    Any way, B is not so semantic, of course, coz’ ain’t no paragpraphs. A answer is also no so semantic, an image is not a paragraph too.

  47. Shaun Inman says:

    Wouldn’t it be possible to hijack the recently uncovered :hover fix to make :after work in IE? A little innerHTML here, some walking the node tree there and viola! I’ll play around with it tonight and see what I can come up with.

  48. Shaun Inman says:

    Hehe, got a little excited there. In which browser exactly does the :after pseudo-element work? I was under the impression that it worked in anything other than IE but it doesn’t seem to work in Safari 1.0 or Firebird 0.7. My apologies for temporarily hijacking the thread Dan.

  49. michaud says:

    Why not place the image in a span and in this span a span with the description. The XHTML strict validation seems to like it:

    Lorem ipsum dolor sit amet, ...
    hendrerit in vulputate ...

  50. Anonymous says:

    I like C, but how can you wrap the caption text so that the caption is not wider than the image itself? The only thing I can think of is to use an inline style to set the width of the <dl> element to equal the width of the image, something like <dl class=”floatimg” style=”width:155px”>. But that’s just ugly, isn’t it?

  51. Tom says:

    I’d probably do:


    Although I probably wouldn’t be correct.

  52. Tom says:

    Correction (forgot the code tag):


  53. Mike says:

    For comment #50, I believe that you have to set a width as the dl is floated.
    What I found was I had made a dl class with a width for each of tall and skinny and short and wide, and set the class accordingly.

  54. Jim Dabell says:

    Generated content has been working in Gecko-based browsers for ages now. It works in Opera and KHTML too. For instance:
    img:after {
    content: attr(alt);

  55. "Generated content has been working in Gecko-based browsers for ages now."
    … as does styling of XML, in which you can define your very own tags, that suits whatever you consider good, semantic markup.
    … or you could simply create everything in XSL-FO, and suggest to your sites audience that they just run your content through FOP.
    My vote is on option B, modified to my personal likings and my customers’ needs. Currently all of my customers need to have their sites available to IE users, so I’m sticking with what works for them, for now.
    I’m not trying to step on peoples toes, but why not just stick with what works (reasonably well) now, is well-formed, and can be transformed easily in the future?

    <div class="floatright">
        <img src="someimg.jpg" />
        Image description

    can be transformed into this

    <object type="image/jpg" data="someimg.jpg">Image description

    Or whatever becomes relevant in the Future …
    just my 2px

  56. Knightking says:

    Personally, I would go with the following:

    <p class="floating">
    <div>alt text</div>

    In this instance, I see no need for alt text, assumming the description works without the presense of the picture. If not, you could place the additional text in the contained div.
    This has the following benefits:
    1) Mobile browsers, screen readers, etc. have, if it is well done, no idea that they are not seeing an image, and believe that it is merely another paragraph. Could require skillful writing, but definatly accessable.
    2) In this case, these browsers are recieving semantically accurate information, except for a random div in the middle of it.
    Of course, it is not perfect. Whenever using any form of FIR, you tend to suffer in the case of images off, CSS on. Of course, most pictures which would have captions tend to have opaque backgrounds, making this relativly easy to avoid.
    The biggest problem is that instead of a nice img tag, which contains a small amount of semantisism, you have a div sitting there, which means nothing. However, I don’t know of anything useful that triggers off of image tags besides things such as Google Image search. I would love to know if I was wrong, though.

  57. I realy would like the floating:after css without any further html tags. But what browser does really support this yet? I tried with Mozilla, IE6 and Opera, all of them failed. At least Opera displayed the caption but lost the floating.
    Any working example of this for some known browser?

  58. waylman says:

    Like many others here I don’t care to much for A, and here’s why: The br affects layout/design/style (whatever you want to call it). If I wanted to switch stylesheets in the future and, for some strange reason, actualy wanted the caption on the same line as the img or (more likely) above it, A would not easily allow me to do so. Therefore, A is out.
    For those who object to images being a list: we all (should) know that a list with one item is completely valid if it makes sence to do so. So a definition list with one item is not poor markup, assuming that it actualy contains a definition. I can see where the caption could be considered a definition of an image. But couldn’t an image be considered the definition in some cases? While this solution may work in some applications I don’t see it for all.
    I would be inclined to use some variation of the title attribute and a Pseodo class, but unfortunetly this is to poorly supported by todays browsers. Maybe in the future!
    Thus, I would go with B without the extra p around the img as there is not real need for it as others have pointed out quite well.

  59. Gambit says:

    I would have to go with C, B dosent make much sense as a few people have said, an image is not a paragraph. I also like the idea of just putting the caption in a paragraph, but then again its not a paragraph.
    C is my choice, it makes the most sense to me.

  60. Layla says:

    Yeah, Chris, float does require an explicit width, and if you don’t include this pretty much all of the suggestions above will break in IE for mac.

  61. James McComb says:

    I wouldn’t use option A because it contains <br /> and <br /> is evil.
    B is out because neither an image nor a caption is really a paragraph.
    I wouldn’t use C because an image is not a term, and a caption is not a definition of an image.
    So I guess I’d quit being a web designer.

  62. You may want to read my new article on auto captions for images at

  63. Amy says:

    I am going to say a for two reasons
    1) I just started working with css
    2) It just makes since to use the p tag since you are discussing something about the image.

  64. Amy says:

    try this:
    border: thin #000;


    This is some text.

  65. Amy says:

    border: dashed #000;


    This is some text.

  66. Amy says:

    border: dashed #000;


    This is some text.”

  67. Amy says:

    well ok let me put it this way
    head tag = border: solid;
    float: right
    in the division tag place
    div class=”idea” style=”position:absolute; left:48px; top:20px; width:78%”>


    This is some text.
    if this doesnt post right i wont try it again

  68. Doug Baker says:

    Myself, I like C, even though that markup choice has never occured to me. I understand the opposition to this choice because the image is not a term per se, but you can see Douglas Bowman’s comment in Part III: Heading and List for more expansive ways of utilizing <dl>. So I’d say C.

    (p.s. thanks for the great forum for debate, Dan!)

  69. Richard@Home says:

    How about this for semantically tying the image to it’s caption?

    <div class='thumb'>
    <img id='image_1'/><label for='image_1'>Image Caption</label>

    Suprisingly, this validates against the XHTML Strict DocType!

  70. David Walker says:

    The Michael Suodenjoki solution at comment 62 above really appeals: the image’s ALT text is semantically its caption.
    Otherwise I’m with PM in commment 42: stick the lot in a styled DIV and use contextual markup for the IMG as required (until the W3C comes up with something appropriate).

  71. Richard@Home says:

    going along with #42…

    Whats so bad about using a single cell table with a caption?

    Just a thought…

  72. Marko says:

    Previously at SimpleBits: Heading and List – “Clearly, there are uses for definition lists far beyond the traditional term and description.” (W3 specification).
    Add one more C…

  73. Joe Clark says:

    It will exclusively be revealed that not only do I use single-cell tables with the <caption> element for this purpose, I do it so often I have a macro set up in BBEdit.
    Of course, I also use the <dl> approach, but I am too much of a piker to know how to float things around a page.

  74. Juan Ulloa says:

    Isn’t the use of the caption element reserved for datatables? I would hesitate in using a table wiht the caption tag for these purposes.
    I personally would go with ‘C’. I guess you can say that an image and it’s caption are a definition list. But in practice I would go with B, minus the

    tags…depeding on how my style sheets are set.

  75. mark panay says:

    Having used C and B for different commercial projects. I’m absolutley amazed that I didn’t think of doing the method in post 69. I think thats the one to try for me next. Semantic, valid and simple. Thanks a lot for pointing out the obvious.

  76. Karl says:

    I’ve got a further wrench to throw into the works….
    How do you set up one of these elements so one of your users using Contribute can ad the photo with caption as he pleases to the pages?…

  77. Jano says:

    I dont think generating :after with any attribute of the image as content is a good choice. The caption is not a design thing, it is not something that can be left if CSS is not supported well. Think on text browsers! Caption is content and not a meta or alternative element.

  78. Tero says:

    Just came across a strong (IMO) argument against the DIV option (b) in a form of a client who insists on having Mac IE 5 among the supported browsers.
    In a floated layout floating DIVs with no width CSS attribute set seem to always have width of 100%. Haven’t been able to go round this.
    Almost wants me to go to a two-row TABLE with images/captions. Almost… With single-paragraph captions, option A seems to work with least hassle.

  79. sam says:

    Dispite the fact that the <label> [see comment #69] tag was created for texboxes (and other form elements, I guess) I think it’s the most semantically correct, but not perfect.
    <label> seems awful close to <caption> to me.

  80. FataL says:

    First of all, any of options (A, B or C) we can’t put inside paragraph, because it not validates with XHTML doctype. So, these options not universal, because some times authors need to put pictures inside paragraphs.
    We only can do this:
    <p>Paragraph 1</p>
    [option A, B or C]
    <p>Paragraph 2</p>

    not that:
    <p>Paragraph 1 [option A, B or C] Paragraph 1</p>
    <p>Paragraph 2</p>

    I found some universality and semantic in this markup:
    <p>Paragraph 1 <label for="pic1"><img /><span class="caption">Caption 1</span></label> Paragraph 1</p>

  81. Bill Rehm says:

    I’m struggling with this issue right now myself. My problem with C above is that a list of one is not a list. If the W3C spec provided for using <dt> and <dd> outside of <dl>, I’d embrace it.
    I tentatively vote for the <label> solution above, but think it unworkable because of the sizing issue for floated divs.
    This leads me to a general question: what is the semantic meaning conveyed by <div>?

  82. Anonymous says:

    #81 – I don’t think there is any semantic meaning for a Div – It’s an empty element.
    It does seem weird using a DL simply because there is only one thing in the list. However, there doesn’t seem to be a perfect alternative so this is what I go for.
    DT would contain the definition term (either the image or its ALT attribute) and the DD would be it’s definition in this case a description
    The Label option sounds good but I think it is really meant for form controls (