33

I'd like to create a bottom outline border when the cursor is over an image and I don't know how to do this. I'd like to use this kind of inner border because I don't want to have layout problems with a traditional border-bottom.

Here's my current code, with outline margins everywhere:

.img-lightbox-small {
  width: 110px;
  height: 110px;
  margin: 1px;
}

a img.img-lightbox-small:hover {
  opacity: 1;
  outline: 3px solid #4bb6f5;
  outline-offset: -3px;
}
Jeanjean
  • 723
  • 2
  • 12
  • 22

2 Answers2

52

To get around the problem you can use border-bottom, with it set margin-bottom: -1px (the size of the border). This will stop it from moving the content below.

HTML:

<div></div>
test

CSS:

div {
    width: 100px;
    height: 100px;
    background: #eee;
}
div:hover {
    width: 100px;
    height: 100px;
    background: #eee;
    border-bottom: 1px solid;
    margin-bottom: -1px;
}

DEMO HERE

Ruddy
  • 9,795
  • 5
  • 45
  • 66
3

outline is not like the border property. you have all sides, or none. you'd better use the border property mixed with box-sizing:border-box which overwrites the box-model, so that your layout doesnt "move".

http://jsfiddle.net/8XjM5/1/

div {
    width: 100px;
    height: 100px;
    background: #eee;
    box-sizing:border-box;
    -webkit-box-sizing:border-box;
}
div:hover {
    width: 100px;
    height: 100px;
    background: #eee;
    border-bottom: 1px solid;

}
valerio0999
  • 11,458
  • 7
  • 28
  • 56
  • Ah I see, I didn't go for this as it is using `CSS3`. – Ruddy May 13 '14 at 08:24
  • I tried this too, but when I apply it my image is moving (size is reducing when a:hover) which is not the case with the other code... See http://jsfiddle.net/8XjM5/4/ (Sorry I didn't know how to use this web site I think I deleted your code *Newbie*) – Jeanjean May 13 '14 at 08:29
  • img is an inline element , that property only applies to block elements. anyway, even if it is a css3 feature it's widely supported: http://caniuse.com/#search=box-sizing – valerio0999 May 13 '14 at 08:36