Due to some circumstances, this blog is now up for sale, for more enquires contact: Plushista@gmail.com
RealcomBiz
Pin It

Adding Icon After External Links with CSS or JavaScript

by Unknown | Monday, October 27, 2014 | | 36 Comments

Styling external links with text or icon is common practice for many sites, like Wikipedia. It serves as a simple visual cue indicating that if you click a particular link you will be taken to another site.


You can simply implement this using CSS or JavaScript without having to add any additional elements to your anchor tags or additional image tags to the HTML source.

Example is given below:



CSS Solution

You can archive that with common HTML and CSS like this:

HTML:

<a href="#" class="external">external link</a>

CSS:

.external a {
  background: url(external.jpg) center right no-repeat;
  margin: 0 12px 0 0;
}

Imagine the extra work you have do while passing class="external" to all external links, which is easy enough for you to forget.

However, there's a CSS that can apply the indicator icon to all external links:

a[href^="http://"],
a[href^="https://"] {
background: url(external.jpg) center right no-repeat;
margin: 0 12px 0 0;
}

There are two common urls, like http://www.google.com and https://www.google.com/webmasters. So we have target the two protocols with href^="http://" and href^="https://".

Now, the code above will apply the icon to all links starting with http:// and https:// (including links to your own domain). Luckily there's a way we can play around this. What we have to do is, declare a rule matching links that start with http:// and https:// with the current page domain and set the background and margin values back to the defaults.

If I'm to do that for this blog, I will write something like this:

a[href^="http://www.realcombiz.com"] {
background: "";
margin: 0;
}

The above code will prevent the external icon style on that specific url.

You can also style external link in your web pages with target="_blank" only if that in your visual editor you check that the link should open in new tab. The code that does the magic is as follow:

a[target="_blank"] {
background: url(external.jpg) center right no-repeat;
margin: 0 12px 0 0;
}

Using Font Awesome

If you have Font Awesome installed you can use the following:

a[target="_blank"]:after {
  font-family: 'FontAwesome';
  content: " \f08e";
}

That f08e is called unicode. It is readable by your browser and is ultimately what renders your icon.

Note: If you use an icon font other than Font Awesome, you will need to change the font-family to the name of the font you are using to render your icons.


JavaScript Solution

With JavaScript you can also display icon after external link. The technique is simple: we simply declare a rule that select element with id content, but you will need to make sure the id tally with the id that wraps your content, then we select all the anchor links within the page with getElementsByTagName( 'a' ).

var length = document.getElementById('content').getElementsByTagName('a').length;
var href;
for(var i=0;i&lt;length;i++)
{
 href = document.getElementById('content').getElementsByTagName('a').item(i).getAttribute('href');
 if(!href.match(/your-site.com/))
 {
  document.getElementById('content').getElementsByTagName('a').item(i).innerHTML = document.getElementById('content').getElementsByTagName('a').item(i).innerHTML + "&lt;img src='external.jpg'/&gt;";
 }
}

Now to differentiate external link from internal, we set a rule that check if the current link is external or not. If the link contains the domain name your-site.com then the code in the 'if' block will not be executed. But it finds any link other than your-site.com it will add <img src='external.jpg'/> to its current HTML code.



Go Social:

Subscribe For Free Updates!

*Please confirm the email sent to your inbox after clicking "Sign Up!".

36 comments : Post Yours! Read Comment Policy ▼
PLEASE NOTE:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

  1. This is very interesting post! By following your given points one can add icons to their website and make a more impressive website design.

    ReplyDelete
  2. Thanks for this codes im gonna try this to my proj. thanks a lot

    Gary Volk Design Reviews

    ReplyDelete
  3. This is a great exciting article. I am pretty much satisfied with your good work. You put really very useful information. I am delighted to see that people are in fact writing about this subject in such an elegant method, presenting us all diverse parts to it.
    essay writing service reviews

    ReplyDelete
  4. Im no expert, but I believe you just made an excellent You certainly understand what youre speaking about, and I can truly get behind that.
    Regards,
    Python Training|Python Training Institutes in Chennai|python training in chennai

    ReplyDelete
  5. Great post. having a business sector benevolent substance, and including a fundamental checkout and uncomplicated shopping basket. website design

    ReplyDelete
  6. Great post with amazing website designing and development with very low rates thanks for sharing website design and development

    ReplyDelete
  7. Excellent post!!! The future of .net application development is on positive note. It offers huge career prospects for talented professionals all over the world. Training on .net technology will ensure good salary package. Best DOT NET Training in Chennai|Dot net courses in chennai |.net training in chennai|.net course in chennai

    ReplyDelete
  8. I have seen so many sites that provide java programming homework help but nothing satisfied me. I need some authentic resources that can genially help me.

    ReplyDelete
  9. IOS has recently launched new version of Apple Operating System called IOS 10.To upgrade your IOS to Latest IOS 10 new release by Apple Inc. you can get New iOS 10 free from CydiaNerd on your Browser.

    ReplyDelete
  10. Visit for find valentines day images and greeting - http://www.happyvalentinesdaywishess.com

    ReplyDelete
  11. It’s really a great and helpful piece of line. I am glad that you shared this helpful info with us. Please keep us informed like this.
    Thanks for sharing
    whatsapp web

    ReplyDelete
  12. Thank you for bringing more information to this topic for me. I’m truly grateful and really impressed. Really appreciate for your amazing article. Thank you for this valuable information. filmywap apk

    ReplyDelete
  13. The counterfeit plants can expand the tasteful excellence of the whole climate. Henceforth they are considered as a need as opposed to a mold image. There is no additional cost for support while it is comprehending your motivation. Read more

    ReplyDelete
  14. It is a great article. You will surely like this also because it is a great stuff, yeah it’s give us lots of interest and pleasure. Their opportunities are so fantastic and working style so speedy. Thank you for sharing the nice article. fastpokemap

    ReplyDelete
  15. Thanks for your article, it gave me a lot of new and useful information. Hope you have more posts to share with everyone.
    bloxorz

    ReplyDelete
  16. The strategy you have updated here will make me to get trained in future technologies. By the way you are running a great blog. Thanks for sharing this.nice information about this valuable article,which helps to learn more
    Web designing course in chennai

    ReplyDelete
  17. Thanks for your article! Although simple, but your posts have made it more attractive and interesting!

    ReplyDelete
  18. The sites you have referred were good. This is a great article, that I really enjoyed reading. Thanks for sharing. http://ssapptricks.com/yelp/
    open in the Yelp app

    ReplyDelete
  19. it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points.







    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery






    ReplyDelete
  20. Enjoyed reading the article above , really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles. To know step by step process use in mictest
    while using different types of microphones for various purposes visit this profile this is something will help you out.

    ReplyDelete

Recent Posts

Let's Connect

Site Links

Copyright © 2014 RealcomBiz. All Rights Reserved.
Powered by Blogger