design: creating an inline css image border
Our sister article, entitled creating proper mouse-over borders explains how to add a clean border around an image link on hover, without the all-too-common and undesirable “jump” effect. In this article, we will explain in detail how to apply a border INSIDE an image itself, for those situations where an image may be displayed up against another element, or where a hidden margin would not be appropriate. Before we get started we should warn you that this technique is not (at this time) IE 5.5 friendly, but does work in Internet Explorer 6, 7, Firefox and Safari just fine. On to the code!
First, we will create the div to encase our image link. In contrast to our original mouse-over borders article, this one will not include the image within the div (we’ll explain this in just a moment). We will continue using the image of our furry friend for this article, and here’s the code we’ll start with: <div class="catwidth"><a href="#"></a></div>
Now, instead of adding the image directly into our div, we will need to define this image in the background of the div that we are using. If you are only planning on using this code in one place, you can simply use this css to do it:.catwidth a{width:130px;height:130px;border:0; background-image:url(images/cat.jpg);display:block;}What we have done here is told the browser that we have a 130 pixel image link, but on mouseover we would like it to remove three pixels on each side (to 124 pixels), replacing it with a three pixel border. Now, the way we get around the image jumping and becoming displaced is by adding background-position:center, which orders the browser to maintain the same position for the image, even if it has been clipped on all edges by three pixels. Make sense?
.catwidth a:hover {border:3px solid #5f9215;
width:124px;height:124px;background-position:center;}
Here is our end result:
This is great for one image, but let’s say that you have three different places on a page that you would like to apply the same effect to. You could create three distinct classes, but a much easier method (if all three images are the same size) would be to simply change your div code to look like this:<div class="catwidth" style="background-image:url(images/second-image.gif);" ><a href="#"></a></div>
This will override the image defined within your stylesheet, and render second-image.gif in its place. As we mentioned, this will only work if the images are all the same size. This is because, while we can define width and height just fine within the style variable of our div, we can’t define what will happen on mouseover. So, we could add an image at twice that size and declare as such in our div, but as soon as the image is moused over it would go back to what is defined within the stylesheet.
Compatibility:
This method has been tested in and is compatible with Internet Explorer 6, 7, Firefox (PC and Mac), Netscape and Safari (PC and Mac). Note that at the present time, IE 5.5 is not supported.
That’s all folks!
If you have an idea or article that you would like to contribute, send it on! We’re always looking for good, quality articles. Note that we will not republish an article that has been published elsewhere, so keep it original!







