0

I currently have an hover effect written in CSS:

h2:hover{
     background-color: #FFE4B5;
     border-bottom: 1px solid #888;
     border-top: 1px solid #888;
     cursor: pointer;
}

But i noticed that when i hover each of the menu options, the text would move slightly up and then down. I didn't add anything that would cause this? I don't think.. How can i fix this and make them stay in place when hovering?

JSfiddle here

PhoonOne
  • 2,678
  • 12
  • 48
  • 76
  • As well as a dupe of http://stackoverflow.com/questions/9612758/add-a-css-border-on-hover-without-moving-the-element and a bunch of others. – j08691 Jun 17 '15 at 21:17

2 Answers2

3

It's the border that's being added and removed that's causing this issue.

Two options: add box-sizing: border-box to the item or add transparent borders to the non-hovered elements.

Example box-sizing:

h2 {
  box-sizing: border-box;
}

Example border:

h2 {
  border-top: transparent 1px solid;
  border-bottom: transparent 1px solid;
}
Josh Burgess
  • 9,327
  • 33
  • 46
2

The movement comes from the border being added and removed on hover. An easy fix is to give the h2 element a transparent border of the same width when it's not being hovered:

h2{
     border-bottom: 1px solid transparent;
     border-top: 1px solid transparent;
}
j08691
  • 204,283
  • 31
  • 260
  • 272