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 |
Want an easy way to add a caption to an image like this?

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;
}
Similar Posts
Want more web design and development tips?
Subscribe to The Web Squeeze by Email, or our RSS feed, and you'll have all the latest web design tips coming right to your inbox! Don't be a stranger, join our forum!




July 17th, 2008 at 4:43 am
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.
July 17th, 2008 at 8:24 am
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.
January 1st, 2009 at 10:50 pm
<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” />
January 2nd, 2009 at 10:03 am
Well it seems this post got a little messed up. Thanks for seeing the errors! It’s fixed now!