Adding a Border to an Image on Hover

This next issue I want to cover is fairly simple, but I ran into a situation last week where I needed to find a solution to this problem, and I wasn’t sure of the best answer. I wanted to add a border on the hover state of an image. The problem is that when you add a border it adds to the width. The size of the box according to the box model is made up of width+padding+border+margin. So if you only have the border when hovering, the width of the object changes. When you have images in a grid, the problem is very obvious and actually almost nauseating to look at!

To see the problem, it will be clearest if you go to Demo page to see it in action.

A simple solution is to add a border on your image, div, etc. (wherever it is you want the border) that is the same color of your background. Obviously, this will only work if you have a solid color background. In my demo, I do, so I will add a white border initially, and change it to black on hover. That way the size never changes, but things look different on hover.

img { 
  border: 3px solid white; 
img:hover { 
  border: 3px solid black; 

To solve the problem mentioned above, you can instead use outline property. This will work when you have an image or gradient in your background. It will also work when your initial image has a border, but you want to make it larger on hover. The outline property uses the same syntax as the border shorthand.

/* Solve problem on a non-solid background */
img:hover { 
  outline: 3px solid black; 

/* Solve problem where border size changes on hover */
img { 
  border: 1px solid black; 
img:hover { 
  outline: 2px solid black; 


The reason this works is that outlines do not add width to your objects. They are not really in the flow of your document, so things don’t change when you hover over something. Outlines are uniform on each side. You cannot set top, right, bottom, left to be different, as you can with borders.

Notice in the second solution, where the border-size is changing, I made the outline 2px. This is so the final size of border is 3px (1+2).

Browser Support

When I first came upon this solution, I read that it did not work in IE. However, I tested in IE8 and it worked fine. It does not work in IE7. So, if hovering in IE7, the border will remain the same. It depends on the importance of the that visual effect to your design if you want to find another solution to work in IE7 or not. Another solution would be to have a 2nd image for the hover state, but that comes at a price because you are downloading more images. In most cases, I’m fine with leaving IE7 hover-borderless, but it would depend on the site and audience of course.

7 thoughts on “Adding a Border to an Image on Hover

    1. You can do it with : border-radius: 5px 5px 5px 5px; /* 1st value is up left corner, 2nd for up right, 3rd for bot right, and 4th for bot left.

  1. so I thought of a simple solution for rounded borders, and no jumps:

    -starting with a border before hover (same color as your background color) will avoid the jump or movement when adding a border on hover

    you may have to add 1 px padding as well to make the rounded corners show on hover

    ^I used this for mouse-over images that were also links, and they changed in opacity as well on hover (the border was also affected by the opacity, but it did not concern me for my needs)

    shadows could be used as well

  2. Thank you, but I hve a Great problem, I Have two images in the web page. I Want only one image has border when I roll over the mouse, but not the other wwhen I roll over the mouse. With your explanation when I put the code in this web page both images show me the black border.
    Can you help me please.
    Thank you.

    1. Hi Fernando,

      If you add a class name to the image that you want the border to show up with, you can target that image only.

      For example, add class="highlight" to the img tag. (Use whatever class name makes sense on your page, of course.)

      Then your css could target img.highlight:hover specifically, and it won’t affect other images on the page.

      I hope that helps. Let me know if you need more explanation.

      ~ Lisa

  3. Can anyone help me, please?. I want to add a border style in the link:hover, to a component of primefaces.
    I add like this:
    .ui-contextmenu .ui-menuitem-link:hover{
    font-weight: bold;
    border-style: solid;
    border-width: thin;
    In a file calling pfcrud.css.
    The problem is that, the border style is not working/showing.

Leave a Reply to bruge Cancel reply

Your email address will not be published. Required fields are marked *