Styled images with caption
Posted in Tech/Development on: Saturday, 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









48 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
Neat solution Paul :-)
Fantastic work Paul. Great example of a perfect use for DOM scripting.
Thanx for sharing that tip! I was looking for a way to add captions to photos, thanx! :D
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!
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.
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.
Well where there are weird blanks in my above post there are meant to P tags :)
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.
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.
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.
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?
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?
Hmm, am I misisng something? Step 4) example …
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!
a long time ive tried to found a plugin like that!, amazing thanks alma!! and thanks Paul for the script!!!
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.
Ok, here’s a solution: the .imgRight p, .imgLeft p should have a clear:both; attribute. It works well with my script. thanks Paul :)
Thanks for helpful information you catch up us with your instructional explenation.
What people do is more important that what they say…
Best regards
Thanx for sharing that tip! I was looking for a way to add captions to photos,
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?
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
yavşaklar eklemesin..
anlayan anladı..
http://www.cesurturk.org/index.php?ind=kureselisinma
Perfect post Paul, good sharing. Thanks brother…
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.
imgLeft p should have a clear:both; attribute. It works well with my script
Neat solution Paul, thanks…
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/
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
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
imgLeft p should have a clear:both; attribute. It works well with my script
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.
imgLeft p should have a clear:both; attribute. It works well with my script
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.
imgLeft p should have a clear:both; attribute. It works well with my script
Fantastic work Paul. Great example of a perfect use for DOM scripting.
Fantastic work Paul. Great example of a perfect use for DOM scripting.
Fantastic work Paul Great example of a perfect use for DOM scripting…
Perfect post Paul, good sharing. Thanks brother…
One thing I would like to see though is the ability to add a certain amount of pixels to the.
thanks for this script
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.
One thing I would like to see though is the ability to add a certain amount of pixels to the.
The width will expand to accommodate that. Hmm ok
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.
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.
good working Paul. thanks you for it all long time ive tried to found a plugin like that.
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.
Fantastic work Paul. Great example of a perfect use for DOM scripting.