views:

2329

answers:

5

I am trying to make the ugly grey border that appears around anchor tags go away. The CSS property "outline:none;" works for Firefox, but how can I do it in IE. Preferably using CSS expressions or jquery. I'm not worried about accessibility BTW.

Based on your suggestions I found these to be the best solutions...

The jquery(for IE browsers):

$('a').focus(function(){
     $(this).blur();
});

Another jquery option(for IE browsers only):

$('a').focus(function(){
     $(this).attr("hideFocus", "hidefocus");
});

The CSS(For all other browsers that force an outline):

a{
     outline: none;
}

Note: Some browsers such as Google Chrome don't force an outline on focus.

A: 

does this not work:

a { border: 0; }

Matt
Nope. Did it work for you?
border is something different than outline, so that wouldn't work.
peirix
Some websites have suggested outline:0; as a solution
+2  A: 

It sounds like you're talking about the dotted border that appears when you tab through links. You have the correct solution for Firefox (outline: none in the CSS). The best solution I've used for IE is to add an onfocus listener that removes focus:

<a href="" onfocus="this.hideFocus=true;">link</a>

Take a look at this site for an example of how you might do it globally: http://codylindley.com/Javascript/223/hiding-the-browsers-focus-borders-should-i-shouldnt-i

Chris R
`$("a").focus(function(){ this.hideFocus=true; });` for the jquery way of doing what that link suggests
gnarf
+1  A: 

For IE, you can use Javascript like this:

<a href="..." onfocus="this.blur();">Click Here</a>

Read more: http://www.htmlgoodies.com/beyond/javascript/article.php/3471171

For Firefox and Safari, outline:none works.

Read more: http://css-tricks.com/removing-the-dotted-outline/

Crimson
Don't do this, it breaks keyboard navigation. Laptop users and various varieties of disabled people won't thank you. There are workarounds like temporarily setting `element.onfocus=element.blur` onmousedown (getting rid of it again after the click), but really hideFocus for IE (and outline for the others) is simpler.
bobince
+5  A: 

Unfortunately I think hideFocus is your best answer as blur isn't always appropriate:

<a href="..." hidefocus="hidefocus">...</a>

http://msdn.microsoft.com/en-us/library/ms533783(VS.85).aspx

McKAMEY
BTW, this is a non-standard attribute not a JavaScript solution. You can set this in JavaScript as well with the `hideFocus` property, but is easier to just statically set.
McKAMEY
Whats wrong with blur()?
There's nothing "wrong" per se but you lose the actively focused element which can have odd effects (e.g. tab order, etc.). Also there are other ways of activating the link besides clicking (e.g. keyboard). If all you want is to hide the appearance of being focused then there is no point in also changing the focus when you don't have to.
McKAMEY
Makes sense ill use hideFocus().
Actually `hideFocus` is a property not a method, so if you insist on using JavaScript for this, then you'd want to do `this.hideFocus = true;` and you only really need to do it once for that element.
McKAMEY
That's what I have been trying to do in jquery...can't get it to work!
Even something like this? `$("a").each(function(){this.hideFocus=true;});` Be aware jQuery is going to use a lot more processing than just putting the attribute on the tag.
McKAMEY
`$("a").attr('hideFocus', true);` maybe?
gnarf
I'm aware, what I have above only adds the attribute on focus so its not looping through each. I don't want to manually set the attributes for each link because I have a no HTML attribute policy, and on top of that it would be annoying to manage the hideFocus attribute on the server side or manually. I'm kind of breaking my own rule applying an attribute through js, but at least its not hard coded in there and with a little logic it could be IE only!
@gnarfJquery way: `$("a").attr('hideFocus', 'hidefocus');`HTML way: `<a href="" hideFocus="hidefocus"></a>`
A: 

Unless I'm missing which dotted border is being discussed, outline:none works in Internet Explorer 8 (at least, for me). Rather all of a sudden some hyperlinks were rendering with a dotted border (the only attribute I remember changing is display:inline on an h2 element that contained a link, afterwards the dotted border appeared). So I threw in a { outline:none; } in my global stylesheet and poof, no more border in IE8!