views:

149

answers:

4

I have an Li Style as follows:

li{
    display:inline-block;
    padding:5px;
    border:1px solid none;
}
li:hover{
    border:1px solid #FC0;
}

I need that when I hover over the Li the border appears without making the li's shift around. I sit possible to have a 'border' that's not visible?

+3  A: 

You can use "transparent" as a colour. In some versions of IE, that comes up as black, but I've not tested it out since the IE6 days.

http://www.researchkitchen.de/blog/archives/css-bordercolor-transparent.php

Douglas
Well it worked on ie8, Mozilla, Opera and Chrome, good enough for me, I didn't try it out on Safari but I don't mind safari much. thanks a lot!
William Calleja
Yeah, it's specifically IE6 that this doesn't work in. IE7 is OK.
bobince
+2  A: 

You could remove the border and increase the padding:

li{
    display:inline-block;
    padding:6px;
    border-width:0px;
}
li:hover{
    border:1px solid #FC0;
    padding:5px;
}
Matt Ellen
Well this worked like a charm, I just wondered if there was a cleaner way how to do it? if it was at all possible to have an invisible border? thanks again for the suggestion.
William Calleja
This sounds like a more compatible solution to me
SLC
Just realised the code works opposite to how you need! Fixed. Also, I'd go with the transparent colour. I just didn't know about it :D
Matt Ellen
A: 

Yep, you can use border: 1px solid transparent

Another solution is to use outline on hover (and set the border to 0) which doesn't affect the document flow:

li{
    display:inline-block;
    padding:5px;
    border:0;
}
li:hover{
    outline:1px solid #FC0;
}

NB. You can only set the outline as a sharthand property, not for individual sides. It's only meant to be used for debugging but it works nicely.

DisgruntledGoat
Thanks a lot! the more options I have the better
William Calleja
A: 

Since you said in a comment that the more options you have, the better, here's another one.

In CSS3, there are two different so-called "box models". One adds the border and padding to the width of a block element, while the other does not. You can use the latter by specifying

box-sizing: border-box;
ms-box-sizing: border-box;
webkit-box-sizing: border-box;
moz-box-sizing: border-box;

Then, in modern browsers, the element will always have the same width. I.e., if you apply a border to it on hover, the width of the border will not add to the overall width of the element; the border will be added "inside" the element, so to speak. However, if I remember correctly, you must specify the width explicitly for this to work. Which is probably not an option for you in this particular case, but you can keep it in mind for future situations.

RegDwight