Hi, Guest ~ Login or Register

Auto-Stylize External Links

Auto-Stylize External Links

Listed In Uncategorized » — Viewing Full Tutorial

Use CSS 3 selectors to automatically add an icon to external links



First of all, let me start off by mentioning the glaring limitation of this technique: it wont work in browsers that dont support modern web standards, for example, Internet Explorer 6. However it works perfectly in modern versions of Opera and Firefox. If these restrictions are problematic for the use you have planned, you will probably want to take the conventional approach and assign different classes to internal and external links.

So why would you want to use this technique then? For one it is an easy way of adding a useful accessibility feature, it is also lower maintenance than manually assigning a class to each link and much easier on CPU than manually adding it. I use it on my site as a 'perk feature' for those with compatible browsers; its absence doesn't take anything away from the average user's experience but having the ability can be a pleasant bonus.

On to the code...

First of all we will style all the links so that an icon is visible to their right indicating that they are external. Feel free to use this one http://img.avengex.com/a2z_11303472.gif and colorize as necessary.
a[href^="http:"] {
background: url(images/external.gif) right center no-repeat;
padding-right: 12px;

[href^="http:"] is a selector which indicates a link with any href attribute. So, potentially an external one.

Now we will cancel out the rule for all links pointing to our selected domain. (that should be the domain at which the page is hosted)#body a[href^="http://www.example.com"] {
background: transparent;
padding-right: 0px;
}
#body a[href^="http://example.com"] {
background: transparent;
padding-right: 0px;
}

Note: we create a rule for both http://domain and http://www.domain because they are both link methods commonly used and do not detect each other.

I hope you found this technique helpful or at least that you learned something new.

Working Beta

  1. The Forums
    These are mostly functional. If you see any weird bugs, post a thread about it and an administrator will do something.
  2. Tutorial Writing
    You can now submit tutorials to the brand new management system.
  3. Tutorials Home
    View tutorials by categories and search for them here.
  4. Shoutbox
    See below. Registered users only!

Register

Newest User

Say hi to rayjo! rayjo joined on Wednesday, 3rd December.

Sponsor

Shoutbox