How do you caption images in the Ingeniux HTML editor? Table? Custom coded <div> tag? It’s not easy. And it generally requires you or your users to write code.
Well, we’ve come up with a pretty cool solution to use the standard Ingeniux style selector in the content editor and the alternate text of a specified image to create cleanly captioned images.
~~
How To
Step 1: Create CSS Classes in the Editor:
To enable inline image captioning, you first need to add two styles to the “localstyles.css” file. The file can be found in your main XML directory under Custom > Editor. Add the following CSS references:
.caption_left {}
.caption_right {}
These are just placeholder classes, so there are no CSS attributes.
Step 2: Insert an Image in Ingeniux:
To insert a captioned image in the HTML editor, select the image as you always do using the built-in image selection tool. Make sure you give your image alternate text — this will become the caption. For the purposes of formatting text and images within the editor, go ahead and select an alignment for your image — either left or right — and, to make it easier to view in the editor, put in a border of 1px and vertical and horizontal padding of 5px. Click the insert button to add your image to the HTML editor.
Now if you want to add a fancy caption to this image, click on the image in the HTML editor and make sure the select handles are visible in the four corners of the image. With the image selected, choose from your styles dropdown list in the editor toolbar “caption_left” for images that should be floated left or “caption_right” for those that should be floated right.
IMPORTANT: Nothing will happen in the editor! Your image should look exactly like it did when you first inserted it. The magic doesn’t happen until you view your content in a browser.
Step 3: The Magic
In your default XSL stylesheet, add the following <script> tags:
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script>
<script type=”text/javascript”>
$(document).ready(function() {
$(“img.caption_left”).each(function() {
$(this).wrap(‘<div style=”width:’+($(this).width()+10)+’px”></div>’)
.after(‘<p>’+$(this).attr(“alt”)+’</p>’)
.removeAttr(‘alt’);
});
});
$(document).ready(function() {
$(“img.caption_right”).each(function() {
$(this).wrap(‘<div style=”width:’+($(this).width()+10)+’px”></div>’)
.after(‘<p>’+$(this).attr(“alt”)+’</p>’)
.removeAttr(‘alt’);
});
});
</script>
The first <script> block allows for the use of Google API’s JQuery library. The second block taps into that library to quickly scan the page on load and find any images that have been given the class “caption_left” or “caption_right.” For each such image, we’ll add some code before, after and around the image to build a nice little <div> that looks like this:
<div class=”figureRight” style=”width: 190px;”>
<img src=”images/news/graduate.jpg” style=”float: left; border: 1px solid black; margin: 5px;” width=”180″ height=”250″ />
<p class=”capText”>Cool Image Captioning</p>
</div>
The <div> is given a set width, which comes from the specified width of the image plus 10. The paragraph text (the caption) is pulled from the alternate text attribute of the image.
Step 4: Make the Magic Pretty
Earlier you created empty classes for use within the editor. Now you need to create the real CSS styles for the captioned images. The following CSS references need to go in the cascading stylesheet linked to your default XSL stylesheet:
.figureLeft {
position:relative;
float:left;
margin:0px 10px 10px 0px;
padding:5px;
background-color:#f1ece1;
border:1px solid #e3dac7;
visibility:visible;
text-align:center;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
.figureRight {
position:relative;
float:right;
margin:0px 0px 10px 10px;
padding:5px;
background-color:#f1ece1;
border:1px solid #e3dac7;
visibility:visible;
text-align:center;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
.figureLeft img, .figureRight img {
padding:0px;
}
.figureLeft p.capText, .figureRight p.capText {
clear:both;
display:block;
margin:0 auto;
padding:5px 5px 0px 5px;
color:#524833;
font-size:9px;
font-style:italic;
font-weight:normal;
text-align:center;
}
These will float the container <div> left or right, as appropriate, provide the soft background color and padding, and the font styling for the caption text. These styles will also round the corners of your container <div> in most non-IE browsers. These can be changed to better suit your site’s look and feel.
That’s it. You can create as many captioned images on a single page as your good taste will allow. I hope someone else finds this trick useful.