How can we avoid the shake when we hover over an element and set its border? Here is a sample of the code I wrote:
http://jsfiddle.net/s3N2h/
Is there a technique to avoid the shaking? Suppose I hover on File, the borders appears, but that line of text moves a little to the right due to the borders getting rendered. If we hover away it again shakes.
Is there any CSS way of avoiding such shakes?
add margin:-1px; which reduces 1px to each side. or if you need only for side you can do margin-left:-1px etc. That was the best solution for me because, in my case, I set a 1px border to the orignal element and want to get, on hover, a thicker border (3px). Using margin: -2px; indeed works.
Answer: Use the negative CSS margin If you apply the border around an element on mouse hover it will move the surrounding elements from its original position, this is the default behavior.
Approach: This task can be accomplished by adding one element inside the other element & accordingly declaring the required CSS properties for the parent-child elements, so that whenever hovering outside the element then automatically the property of the inner element will change.
The usual solution to this problem is to start off with a transparent border, then give the border a colour on hover.
I've updated your fiddle with this technique:
http://jsfiddle.net/s3N2h/1/
CSS and JavaScript:
#my_menu li {
border: 1px solid transparent;
}
li.hover(function() {
$(this).css('border-color', 'white #808080 #808080 white');
}, function() {
$(this).css('border-color', 'transparent');
});
Alternatively, when setting the border add a negative margin of the same size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With