How to Add a Web Caption That Stays With Its Image

What to Know

  • In the HTML, place a div tag around the image and add a div style attribute.
  • Set the div width to the image width, add a text-align property, add space between the image and caption, then add the text caption.

This article explains how to add a caption to your web image and make sure the caption stays with your image wherever you move it on the web page.

Steps to an HTML Image Caption

Image captions are important because they add extra information to your visual web page elements.

  1. Add an image to your webpage.

  2. In the HTML for your webpage, place a div tag around the image:

    <div><img src="URL" alt="alternate text" width="width" height="height" /><
  3. Add a style attribute to the div tag:

    <div style=""><img src="URL" alt="alternate text" width="width" height="height"
  4. Set the width of the div to the same width as the image, with the width style property:

    <div style="width:image width px;"><img src="URL" alt="alternate text" width="width" height="height"
  5. For captions that are slightly smaller than the surrounding text, add a font-size property to the div style:

    <div style="width:image width px; font-size:80%;"><img src="URL" alt="alternate text" width="width" height="height"
  6. Captions look best if they are centered below the image, so add a text-align property to the style attribute:

    <div style="width:image width px; font-size:80%; text-align:center;"><img src="URL" alt="alternate text" width="width" height="height"
  7. Finally, add a little extra space between the image and the caption, by adding a style attribute to the image with a padding-bottom style property:

    <div style="width:image width px; font-size:80%; text-align:center;"><img src="URL" alt="alternate text" width="width" height="height" style="padding-bottom:0.5em;"
  8. Then add the text caption directly below the image:

    <div style="width:image width px; font-size:80%; text-align:center;"><img src="URL" alt="alternate text" width="width" height="height" style="padding-bottom:0.5em;" />This is my caption

Upload the webpage to your server and test it in a browser.

Captioning Tips

CSS dimensions can be in many different measurements, so you usually must include the measurement type. For example:

However, HTML image dimensions are always in pixels, so you leave the measurement off.

If you make the width of the div wider than the image width, the caption may appear beside the image. If this is what you want, then add a
tag directly before the caption and after the image tag.

Was this page helpful?