
Building the Wild Zoofari website we wanted to have some sort of frame around the images on the site. Normally that would mean going into photoshop and adding a frame around each image. This seemed very troublesome and inefficient to me, so I starting thinking about different methods that could be used to achieve the result. In the end I decided to add the image frame using CSS and you can see the result on the image to the left.
The first thing you’ll need are the border images. You’ll need two images one for the top and bottom border and one for the left and right border. Depending on the design your could also use four different images one for each border side, but for this example we’ll stick with two. Here are the two images used for the above picture. These images either need to be taller/wider then the largest image you’ll be displaying or be able to repeat and still fit together.
Now that we have the border images we need to setup the html code for the images.
<div class="img-wrap"> <div class="img-container"> <img src="http://static.punteney.com/imgs/Golden_Eagle2_.jpg" width="256" height="331" /><div class="img-top"></div><div class="img-bottom"></div> </div> </div>
The first part of the html is the div with the “img-wrap” class the css that is applied:
div.img-wrap { /* Needed to keep the image from being set to float left in ie 7, but doesn't work for ie 6*/ position: relative; /* This triggers the layout feature to contain the floated divs for the top and bottom borders */ overflow: hidden; }
The next part is the div with the class of “img-container” and it’s css:
div.img-container { /* Keep the floated divs/image contained within */ position: relative; float: left; overflow: hidden; /* this is the left side vertical border Set the color to transparent then include the image and have it repeat vertically */ background: transparent url(/images/pic_frame_vertical.jpg) repeat-y; }
Then comes the actual image:
div.img-container img { /* Setting the padding equal to the border width for all sides so the borders can be seen */ padding: 6px; /* this is the right side vertical border Set the color to transparent then include the image and have it repeat vertically and line up on the right side of the image */ background: transparent url(/images/pic_frame_vertical.jpg) repeat-y right; }
Finally the top and bottom border divs:
div.img-container div.img-top { /* Positioning the top border to start at the top left corner of the img-container div */ position: absolute; left: 0; top: 0; /* Set the height equal to the height of the border */ height: 6px; width: 100%; /* this is the top horizontal border Set the color to transparent then include the image and have it repeat horizontally */ background: transparent url(/images/pic_frame_horizontal.jpg) repeat-x; } div.img-container div.img-bottom { /* Positioning the bottom border to start at the bottom left corner of the img-container div */ position: absolute; left: 0; bottom: 0; /* Set the height equal to the height of the border */ height: 6px; width: 100%; /* this is the bottom horizontal border Set the color to transparent then include the image and have it repeat horizontally positioned at the bottom of the div (bottom position needed for ie 6) */ background: transparent url(/images/pic_frame_horizontal.jpg) bottom repeat-x; }
You can view the complete CSS file here. That’s all there is to it.
Just to prove it does work for any size of image here is a sample page with a couple images using the above code.
There are a few issues with using this method for doing image boarders though.
If you use this technique somewhere or if you encounter any bugs or anything leave a comment below.
This method has been tested in IE 6 and 7, Firefox 2, Opera 9 and Safari.
Here is a link to the Javascript version of the any size image frames.