Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we avoid the shake when we hover over an element and set its border?

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?

like image 989
deostroll Avatar asked Sep 24 '10 16:09

deostroll


People also ask

How do you add a border on hover?

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.

How do I apply a border to the element on a mouse hover without affecting the layout in CSS?

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.

How do you make a hover element affect another?

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.


2 Answers

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');
});​
like image 131
Andy E Avatar answered Oct 20 '22 01:10

Andy E


Alternatively, when setting the border add a negative margin of the same size.

like image 36
Radomir Dopieralski Avatar answered Oct 19 '22 23:10

Radomir Dopieralski