It's Movember. Movember is a yearly, world-wide fundraiser for research of men's health problems, such as prostate cancer and depression. Your donation will count, even if it's just $1. Click here to donate.
Use Bodhi Linux—It's awesome!

hasLayout.net

Share |

Removing Dotted Border on Clicked Links

Description

Dotted border appears on clicked links - what is it about and how to deal with it

Date of the Tutorial

Sun Aug 16 22:00:27 2009

Overview

Yes, yes, I know, there are a ton of tutorials online that show a simple "solution" for this "dotted border around clicked links". However, I am yet to see a tutorial that does not treat this accessibility feature as a "bug" or at least describes why that border appears when one clicks a link.

In this tutorial I will try not to take any sides; removing the border decreases your site's accessibility and not removing it makes things look "ugly". I will explain why that border appears, what it is for and how to remove it - it will be up to you to decide on how to deal with the problem.

Why Do We Have That Dotted Border?

You create a stunning graphical navigation and you style up your links nicely for it. You click the link and during that moment from the click until the next page loads you see a funny looking dotted border around the links. Moreover, if you're using text-indent to hide the actual text from the links you may see that that border extends through the entire page (just add a { overflow: hidden; } to fix that). You decide that the border is "ugly" and you go off trying to figure out how to get rid of it, but have you ever asked yourself why that border is there? Let's take a look at what I am talking about first:

The demo is shown on a separate page

HTML Code:
<ul>
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
</ul>

Nothing special here, just regular markup. Go ahead and load up that demo and play around with it - click the links... see the border? Note for IE6 and IE7: if the dotted border does not show up when you click the links, simply hit TAB upon page load and now the borders will show up on clickety. Note for Opera: it has more advanced keyboard shortcuts and I haven't had time to figure out how to enable the keyboard browsing mode... so there.

That border is an accessibility and usability feature. The reason you may not think that it is is because you're not actually using it. What happens is that browsers have default styles set on :focus and the dotted border you see is created by outline property. When you click the link, it becomes "in focus" and you see the border. The proper way to utilize this accessibility/usability feature is to use keyboard for navigating the links. Try it! Load the demo again and press your TAB key... see the border? Press TAB again, now that border is on the second link. Pressing SHIFT+TAB moves the opposite way. Now you can see that it's not about clicking. People who can't use the mouse, some public computers that do not have mice or "power users" who prefer to stay off the rodent and use the keyboard as much as possible - all utilize this marvelous feature! And what do you want to do? Remove it? Shame on you!

Keeping Up With Usability - Style

Before you butcher your page and completely remove that outline, even with the knowledge that it's a usability feature, let's look at the options. That outline is stylable in every modern browser! Unfortunately, IE6 and IE7 are not modern browsers, so let's skip them in this section. Let's take a look at the demo:

The demo is shown on a separate page

HTML Code:
<ul>
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
</ul>
CSS Code:
a:focus {
    outline: 4px solid lime;
}

We have used the outline property that functions much like border except it doesn't affect surrounding elements. We have applied it to :focus pseudo-class and now upon clicking the link or TABbing through them instead of a wimpy dotted border we get a thick, lime-colored outline. Depending on your setup, styling the "dotted border" instead of getting rid of it completely may even improve the look of your page. The border does not have to be dotted or a solid line, take a look at outline-style property... don't use the none style just yet, keep on reading

Keeping [Almost] Everyone Happy - JavaScript To The Rescue

There are not that many people left who have JavaScript disabled, and those who do, would sure not care about that silly "dotted border". Since the usability part of this dotted outline on links (and buttons) involves the keyboard and it "looks ugly" only when you click the link, why don't we put the two together and think up a solution that would get rid of the border during clickety but keep it for all the l33t dudes who perfected their keyboard browsing skills? Let's have a look:

The demo is shown on a separate page

HTML Code:
<ul id="nav">
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
</ul>
CSS Code:
a:focus {
    outline: 4px solid lime;
}
JavaScript Code:
var links = document.getElementById('nav').getElementsByTagName('a');
for ( var i = 0; i < links.length; i++ ) {
    links[i].onmousedown = function () {
        this.blur();
        return false;
    }
    links[i].onclick = function() {
        this.blur();
    }
}

First of all, I'd like to note that in IEs (even IE8) this method does not work all that perfectly as the "dotted border" (and styled outline in IE8) show up during the actual click, read the next section on how to fix that.

I've added an id="" attribute to the container of our links for me to be able to localize the effect of the "dotted border" removal with JavaScript - seriously, just let it be in other places. The CSS code stayed the same, we just added a thick lime border instead of the dotted one; you don't have to actually do that, but I think it would look more pretty if you style that outline somehow for browsers that support that.

The JavaScript code is pretty straight-forward. Since the "dotted border" is styled on :focus we are simply removing that focus with this.blur(). Sane browsers are pretty happy with just the .onmousedown. I've added the .onclick to ditch the "dotted border" in IE; however, as I already mentioned, it still shows up during the actual click.

The beauty of this method is that all the keyboard-only users can keep on TABbing away. Load up the demo in Firefox and hit TAB key. See? The lime outline clearly shows on which link we currently are, yet clicking the link with the mouse produces no borders.

Screw IE! I Want The Border Gone!

Alright, the "dotted border" is still a bother in IE browsers. What to do? Why don't we go ahead and butcher the usability for only IE browser and keep our super-duper usable "dotted border" removal solution for sane browsers? Let's have a look at how we're going to do that:

The demo is shown on a separate page

HTML Code:
<ul id="nav">
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
    <li><a href="#">Click me to see that border</a></li>
</ul>
CSS Code:
a:focus {
    outline: 4px solid lime;
}
JavaScript Code:
var links = document.getElementById('nav').getElementsByTagName('a');
for ( var i = 0; i < links.length; i++ ) {
    links[i].onmousedown = function () {
        this.blur();
        return false;
    }
    links[i].onclick = function() {
        this.blur();
    }
    if ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) ) {
        links[i].onfocus = function() {
            this.blur();
        }
    }
}

Everything stays the same as in previous demo with the exception of an extra piece of JavaScript code. What we are going here, is detecting if the browser is an IE browser and if that's the case, we are removing focus from focused links (using .onfocus event handler). This will break the keyboard usability, but it will only happen in IE browsers - can't keep everyone happy, now can we?

I Am The Victim - The Other Side of The Chessboard

There are a lot of sites that boldly use a {outline: 0;} to get rid of the "dotted border", so what to do if you are the person who needs it?

The answer is user sheets. Most sane browsers allow the user to specify their own style sheet that will be included along with the page's styles. You'll have to figure out how to set that sheet on your own. All I can say that in Firefox on Linux you'd want to edit ~/.mozilla/firefox/your_profile/chrome/userContent.css (copy the sample from userContent-example.css that is located in the same directory). The code that you would want to put in there is as follows:

CSS Code:
:focus {
    outline: 5px solid blue!important;
}

Note that I used :focus and not a:focus; this is for affecting not just the links but form elements, such as submit buttons, as well. Secondly, I've added !important keyword to modify the specificity of the rule to override whatever the author of the page has set. You are now all set!

Comments

If you found materials on this site useful, please consider donating a few bucks to the author. Thank you.

Alternatively, purchase my books: