I have an unordered list full or anchors. I have a CSS :Hover event that adds borders to it but all the anchors to the left slightly adjust when i hover because it is adding 1px to the width and auto adjusting. how do i make sure the positioning is absolute?
I made a JS Fiddle here.
You can also use
outline
, which won't affect the width i.e. so no "jump" effect. However, unfortunately, you cannot make an outline rounded.You could use a box shadow, rather than a border for this sort of functionality.
This works because your shadow doesn't 'take size in the DOM', and so won't affect the positioning, unlike that of a border.
Try using a declaration like
instead of your
on your hover state.
Below is a quick demo of this in action:
DEMO
Add a margin of 1px and remove that margin on hover, so it is replaced by the border.
http://jsfiddle.net/TEUhM/4/
No one has mentioned it here, but the best and simplest solution to this in my opinion is to use "box shadow" instead of borders. The magic is on the "inset" value which allows it be like a boarder.
You can offset the X or Y to change top/bottom and use -negative value for opposite sides.
The easiest method I found was using 'outline' instead of 'border'.
instead of
Works the best!
https://www.kirupa.com/html5/display_an_outline_instead_of_a_border_hover.htm
The CSS "box-sizing" attribute fixed this problem for me. If you give your element
Then the width of the border is added to the inside of the box when the browser calculates its width. This way when you turn the border style on and off, the size of the element doesn't change (which is what causes the jittering you observed).
This is a new technology, but the support for border-box is pretty consistent. Here is a demo!