Adding a Caption to an Image with CSS

Adding a Caption to an Image with CSS

CSS tip to add a caption or title to an image on your website. Easy to follow example. Test it out and play with the settings to create your own look.

By Linda Chadbourne on March 17th, 2008 in Tips & Tricks | Print This Article

Want an easy way to add a caption to an image like this?

banana Adding a Caption to an Image with CSS

HTML

<div class="imagecaption">
<img src="your-image-here.jpg"/>
<alt ="banana" width="150px" height"100px" /><p>Banana With a Peel</p> </div>

CSS

.imagecaption {
      width: 200px;
      border: 1px solid #999999;
      padding: .5em;
      }
p {
      font: small Verdana, Arial, Helvetica, sans-serif;
      text-align: center;
      }

Did you like it?

Help us spread the word!
  • Float it!
  • Digg it!
  • Stumble it!
  • Bookmark it!

About The Author

Linda Chadbourne
Linda Chadbourne
Linda Chadbourne has been a web designer since 1998. A large portion of her day also involves graphic and logo design for Maine-ly Web Design which she owns and operates. Linda is also one of the co-founders of The Web Squeeze which is a Web Design and Development Help Forum. In her free time she is an avid family person, horseback rider and reader.

Comments

  1. Gravatar Icon
    ThomasH said :

    This isn’t really valid code, is it?

    That should be a single line:

    Oh, and I’ve noticed that leaving out the px in height and width sometimes screwes up things.

  2. Gravatar Icon
    Karinne Legault said :

    Hmmm… why is it not valid code? And what should be in one line?

    Units are very important, you should never leave them out … never. Unless of course you have a value of 0. Then 0px or 0em or 0% it’s all the same. It’s still 0.

  3. Gravatar Icon
    GTL said :

    <img src=”your-image-here.jpg”>
    <alt=”Stacked” width=”150″ height”100″ />

    should read:

    <img src=”your-image-here.jpg” alt=”Stacked” width=”150px” height=”100px” />

  4. Gravatar Icon
    Linda Chadbourne said :

    Well it seems this post got a little messed up. Thanks for seeing the errors! It’s fixed now!

Toggle Trackbacks

Trackbacks

Do you have something to say?

Live Comment Preview

Anti-Spam Quiz: