Skip to content

A podcast for those who design, develop and run websites.

Boagworld is the personal website of Paul (the Wurzel) Boag who lives in the heart of rural Dorset. He produces a weekly podcast along with Marcus (pop star) Lillington on all things relating to building and running websites.

Latest shows

127. Context
In this week's show we discuss taking context into consideration when designing websites and we answer your questions about video for an elderly audience and the most influential books in the industry. 
126. Scaling
In this weeks show we learn lessons from the botched iPhone launch here in the UK. We chat to Jeff Veen about the designer / developer relationship and Marcus talks about adding jingles to your website.
125. Copy
In this weeks show we discuss how to give personality to your site copy and we talk with Elliot Jay Stocks about going freelance.
124. HTML 5
In this weeks show we explore how to create better online surveys and Lachlan Hunt joins us to discuss HTML5
123. Plight
In this weeks show we review Textmate and the Top 5 Tips for Web Designers and we discuss the plight of in-house designers.

or view all shows

Have your say

Leave a message for the show...

Buy my book: The website owners manual. A book for all those involved in designing, developing or running websites on a daily basis.

Styled images with caption

Published on: July 29, 2006 by Paul Boag

Here is an interesting problem that keeps cropping up. How do you balance the need for easy update by web editors with the desire to make a site as visually appealing as possible? Take for example the images that website owners inevitably want to add to their site via a content management system.

The problem is a simple one. A client wants to add an image to their site via the content management system. They want it to look attractive, not appear too boxy (let us say they want a nice rounded corner, as this is all the rage) and have a nice caption underneath it. However, they do not know how to use an image editor (beyond basic resizing) or how to edit HTML.

What would be great is if they could just add a normal everyday image using the img tag, add a title tag including the caption and then it magically styled itself. Well by combining CSS and DOM scripting, I have managed to get this working.

Of course, I am not the best scripter in the world so if you can improve on the code below then please let me know by posting a comment.

Step One: The HTML

The website owner adds the image resized to the appropriate dimensions. Notice they have added img tag contains a caption in the form of a title tag and a class name of "imgRight" (something easy to add with a WYSIWYG editor like Xstandard or contribute). They have also set the width and height of the image. This is important from a styling point of view later.

<img src="/images/foo.jpg" alt="Description of picture" width="200" height="147" class="imgRight" title="The caption would go here" />

Step Two: The DOM Script

The script I have created does the following:

  • Finds all images with the class "imgRight" or "imgLeft"
  • Loops through each one extracting the title tag and inserting it into a new p tag it has created
  • It then effectively wraps the img tag in a div and inserts the p caption after the image
  • It removes the class name from the img and adds it instead to the div.
  • It also uses the width of the image as the width of the div. This prevents the caption expanding beyond the width of the image.
  • Finally it adds an additional span tag that we are going to use later to create the rounded corner.

Just to keep the code a little more streamlined I use the getElementsByClassName function created by Robert Nyman so don't forget to include that in your javascript file.

function addCaption(xClass) {
var allImages = getElementsByClassName(document, "img", xClass);
for ( var i=0; i < allImages.length; i++) {
var imageCaption = document.createTextNode(allImages[i].title);
var imageContainer = document.createElement("div");
var imagePara = document.createElement("p");
var imageWidth = allImages[i].getAttribute("width");
var spareSpan = document.createElement("span");
imagePara.appendChild(imageCaption);
allImages[i].parentNode.insertBefore(imageContainer, allImages[i]);
imageContainer.appendChild(allImages[i]);
if ( allImages[i].title != "" ) {
imageContainer.appendChild(imagePara);
}
imageContainer.appendChild(spareSpan);
imageContainer.className = xClass
spareSpan.className = "spareSpan"
allImages[i].className = "img"
imageContainer.style.width = imageWidth + "px";

}
}

// Runs all the listed functions on the loading of the window

window.onload=function(){
addCaption("imgLeft");
addCaption("imgRight");
}

Step Three: Add the styling

Once the Javacript has run it should output the following HTML which we can now style:

<div style="width: 200px;" class="imgRight">
<img src="/images/foo.jpg" alt="Description of picture" width="200" height="147" class="img" title="The caption would go here" />
<p>The caption would go here</p>
<span class="spareSpan"></span>
</div>

Obviously, you can style this in whatever way you want but some basic styling might look like this:

.imgRight {
float:right;
margin:0.5em 0 1em 1em;
position:relative;
}

.imgLeft {
float:left;
margin:0.5em 1em 1em 0;
position:relative;
}

.imgRight p, .imgLeft p {
font-size:0.9em;
color:#FFFFFF;
margin:0;
background-color:#4D6D80;
padding:0.5em;
}

.spareSpan {
position:absolute;
top:0;
right:0;
display:block;
width:17px;
height:17px;
background:url(/images/curvedCorner.png);
}

This styling basically absolutely positions the sparespan in the top right corner and adds a nice curve to it while at the same time applying some styling to the caption.

So there you have it. Still very much a work in progress but I would very much like the feedback of the coders out there who are more knowledgeable about such things.

Click here for a very basic working example

Comments

Comments are for the discussion of this post. If you have other questions / comments then post them to the forum or send me an email

  • Post by Adam on July 29, 2006 2:01 PM

    Neat solution Paul :-)

  • Post by Chris on July 29, 2006 10:29 PM

    Fantastic work Paul. Great example of a perfect use for DOM scripting.

  • Post by Sugar on July 30, 2006 10:26 AM

    Thanx for sharing that tip! I was looking for a way to add captions to photos, thanx! :D

  • Post by Daniel on July 31, 2006 3:13 AM

    Great post Paul. I have looked half-heartedly for a solution like this in the past but never came up with anything satisfactory. I will give this a go.

    Digg It! on Digg!

  • Post by Jason Head on July 31, 2006 6:29 PM

    Excellent post Paul, this is a nice little script that's going to make my life a little easier now that I have to work with Contribute a lot more.

    Clients "always" want to add captions, so at least now I can give them an option to do so.

  • Post by Gary Hides on August 1, 2006 4:28 PM

    This looks like a really nice solution to an all too common problem - clients!

    One thing I would like to see though is the ability to add a certain amount of pixels to the width attribute used in the width in the javascript. Therefore you would be able to put on nice borders aroung the image, that are say, 5px. So you would add 10px to the width of .

    I've had a look at the script and when I try and add 10 to the width on your example all I end up with is a tag that is 1710px wide :(

    Obviously I'm no DOM scripter.

  • Post by Gary Hides on August 1, 2006 5:11 PM

    Well where there are weird blanks in my above post there are meant to P tags :)

  • Post by Paul Boag on August 1, 2006 6:23 PM

    You shouldnt need to add that kind of thing to the dom. Just add that to the css styling. The width will expand to accommodate that.

  • Post by Billy on August 1, 2006 8:51 PM

    It is cool, but I am using a Windows 98 computer (it's not mine, it's my friend's) with IE6, and the picture of "I hate the Internet" has a problem: On the curved edge on the upper-right corner, there is a little square. You can still see the curve, but there is a little color alteration thingy there in that square.

    It worked fine on my XP though.

  • Post by Gary Hides on August 1, 2006 9:57 PM

    So you can. I think was messing with the wrong CSS earlier. Very busy and being in a rush n all (that's my excuse anyway).

    Billy,

    You're seeing the lack of native transparent PNG support of IE6. I think Paul was just showing the capability of this DOM script.

  • Post by Daniel Chesterton on August 19, 2006 7:29 PM

    Why do you need the spare span? Wouldn't it be easier to add the background image onto the image through another class.

    e.g. img.rounded {
    background-image: url(image.jpg);
    background-repear: no-repeat;
    background-position: top right;
    }

    Or am I just missing something?

  • Post by Russell on September 1, 2006 3:11 AM

    Can you work this same solution into a mouseover situation, so the caption only comes up styled once rolled over using a different Z axis level?

  • Post by graham on March 18, 2007 11:27 AM

    Hmm, am I misisng something? Step 4) example ...

  • Post by Alma Fernandez on March 19, 2007 12:41 AM

    I've made a WordPress plugin with your script. I've modified a little the script so it doesn't add an extra span and edited the CSS.

    You can found the script in http://labs.webmasterlibre.com/wp/image-captions/, the page is only in spanish for the moment but your script speaks for itself.

    Thanks for your work!

  • Post by Sergio on March 19, 2007 8:30 PM

    a long time ive tried to found a plugin like that!, amazing thanks alma!! and thanks Paul for the script!!!

  • Post by Marcin Nowakowski on August 3, 2007 11:09 AM

    I have a problem with float argument, when client align image with FCKEditor left or right, the caption is pop out the box, where caption has to be. I don't know where's the problem, but I try to figure it out.

  • Post by Marcin Nowakowski on August 3, 2007 11:13 AM

    Ok, here's a solution: the .imgRight p, .imgLeft p should have a clear:both; attribute. It works well with my script. thanks Paul :)

  • Post by web tasarim on October 30, 2007 7:28 PM

    Thanks for helpful information you catch up us with your instructional explenation.

    What people do is more important that what they say…

    Best regards

  • Post by geciktirici on December 25, 2007 12:27 PM

    Thanx for sharing that tip! I was looking for a way to add captions to photos,

  • Post by film izle on December 26, 2007 12:21 AM

    Can you work this same solution into a mouseover situation, so the caption only comes up styled once rolled over using a different Z axis level?

  • Post by seo yarışması on December 26, 2007 12:31 AM


    I am Very thank full the owner of this blog. Becouse of this blog is very imformative for me.. And I ask u some thiing You make more this type blog where we can get more knowledge.
    http://www.cesurturk.org

  • Post by seo on December 26, 2007 12:35 AM

    yavşaklar eklemesin..

    anlayan anladı..

    http://www.cesurturk.org/index.php?ind=kureselisinma

  • Post by Oyun, indir on December 26, 2007 9:34 AM

    Perfect post Paul, good sharing. Thanks brother...

  • Post by www.r10.net küresel ısınmaya hayır seo yarışması on December 26, 2007 9:35 AM

    Becouse of this blog is very imformative for me.. And I ask u some thiing You make more this type blog where we can get more knowledge.

  • Post by resimler on December 26, 2007 9:37 AM

    imgLeft p should have a clear:both; attribute. It works well with my script

  • Post by Divx,indir,movie on December 26, 2007 9:40 AM

    Neat solution Paul, thanks...

  • Post by www.r10.net küresel ısınmaya hayır seo yarışması on December 26, 2007 10:05 AM


    I am Very thank full the owner of this blog. Becouse of this blog is very imformative for me.. And I ask u some thiing You make more this type blog where we can get more knowledge.
    http://www.saboces.gen.tr/

  • Post by www.r10.net küresel ısınmaya hayır seo yarışması on December 26, 2007 10:07 AM


    I am Very thank full the owner of this blog. Becouse of this blog is very imformative for me.. And I ask u some thiing You make more this type blog where we can get more knowledge.
    http://r10kureselisinmayahayirseo-yarismasi.blogspot.com

  • Post by www.r10.net küresel ısınmaya hayır seo yarışması on December 27, 2007 6:40 PM

    Can you work this same solution into a mouseover situation, so the caption only comes up styled once rolled over using a different Z axis level?
    http://www.myrize.org

  • Post by hikaye on December 28, 2007 12:06 PM

    imgLeft p should have a clear:both; attribute. It works well with my script

  • Post by oyun on December 30, 2007 5:13 PM

    soho, I’ve used Photoshop before, too, but I find iWeb easier for brainstorming. I find Photoshop’s power distracting. In iWeb, I don’t worry about getting things just right, so I can play more with ideas and fine tune later.

  • Post by çocuk oyunları on December 30, 2007 5:15 PM

    imgLeft p should have a clear:both; attribute. It works well with my script

  • Post by forum on December 31, 2007 9:24 AM

    This looks like a really nice solution to an all too common problem - clients!

    One thing I would like to see though is the ability to add a certain amount of pixels to the width attribute used in the width in the javascript. Therefore you would be able to put on nice borders aroung the image, that are say, 5px. So you would add 10px to the width of .

    I've had a look at the script and when I try and add 10 to the width on your example all I end up with is a tag that is 1710px wide :(

    Obviously I'm no DOM scripter.

  • Post by msn nickleri on December 31, 2007 10:14 AM

    imgLeft p should have a clear:both; attribute. It works well with my script

  • Post by www.r10.net küresel ısınmaya hayır seo yarışması on December 31, 2007 6:19 PM

    Fantastic work Paul. Great example of a perfect use for DOM scripting.

  • Post by kozmetik on January 4, 2008 7:10 PM

    Fantastic work Paul. Great example of a perfect use for DOM scripting.

  • Post by Web Site Çeviri on January 14, 2008 12:04 PM

    Fantastic work Paul Great example of a perfect use for DOM scripting...

  • Post by varmısın yokmusun on February 4, 2008 2:38 AM

    Perfect post Paul, good sharing. Thanks brother...

  • Post by Kolay Gelsin on February 6, 2008 6:23 PM

    One thing I would like to see though is the ability to add a certain amount of pixels to the.

  • Post by yarisma on February 7, 2008 3:27 PM

    thanks for this script

  • Post by çocuk oyunları on February 8, 2008 10:47 AM

    You shouldnt need to add that kind of thing to the dom. Just add that to the css styling. The width will expand to accommodate that.

  • Post by Aşk şiirleri on February 15, 2008 3:54 PM

    One thing I would like to see though is the ability to add a certain amount of pixels to the.

  • Post by iyinet webmaster forumu 2008 seo yarışması on February 22, 2008 10:57 PM

    The width will expand to accommodate that. Hmm ok

  • Post by birthday gifts on March 16, 2008 7:09 PM

    This looks absolutely fantastic. As a non-programmer guy, this sure sounds promising. Even WYSIWG editors like Wordpress never seem to come out as wonderful as promised. Sometimes it even changes the resolution of the pictures for some random reason, and I can't figure out where things have gone wrong. So, this idea of yours sounds excellent. Basic templates often don't help, because the picture might need to be a different size etc. so this might be just what we need.

  • Post by ttnet on March 17, 2008 4:21 PM

    Everyone who’s looking to bring traffic to thier website is pretty focused on social bookmarking right now. However one of the things a lot people miss is making it easy for their readers to do this.

  • Post by Prefabrik Kabin Konteyner on March 21, 2008 4:06 PM

    good working Paul. thanks you for it all long time ive tried to found a plugin like that.

  • Post by travesti on March 26, 2008 2:43 AM

    knows what Gordon Bell and Jim Gemmell have been up to and if they are going to talk again anytime soon in the Bay Area.

  • Post by 註冊營養師 on April 9, 2008 8:33 AM

    Fantastic work Paul. Great example of a perfect use for DOM scripting.

Leave a comment

Additional Information

Supporting boagworld

Boagworld only exists thanks to the kind support of the following people. Check them out.

Speaking and writing

From FeedInformer