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

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. Informative article on responsive website design!!! With the expansion of mobile phones, most of us access internet from our mobile and other portable gadgets. Having responsive website for your business will help you to target customers widely. web designing course | Web designing course in Chennai | website design in Chennai

    ReplyDelete
  5. These all are notable facts… I stick with responsive website design for my business. With the mobile marketing is at its peak stage, having website that goes comfy on all devices will help your business. You can check my website for more details on the importance of responsive website design. PHP Training Institute in Chennai | Best PHP training in Chennai | PHP Training Institute in Chennai

    ReplyDelete
  6. 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
  7. Great post. having a business sector benevolent substance, and including a fundamental checkout and uncomplicated shopping basket. website design

    ReplyDelete
  8. This is great do you have a catologue if so I would love one to share with friends and family.
    IPL Opening Ceremony 2016 Date
    IPL 2016 Schedule Time Table Announced

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

    ReplyDelete
  10. 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
  11. Let’s keep the best falsh games for your child! abc | slither io | pokemon go | wings.io | happy wheels | happy wheels the game free: are safe place to play the very best free games for kids!

    ReplyDelete
  12. 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
  13. This is one of the cult game now, a lot of people enjoy playing them . Also you can refer to the game :
    gold mine strike | pokemon go 2
    The game controls are shown just under . Movement mechanisms primarily include acceleration and tilting controls.
    stickman games | stick war 2 | animal jam 2

    ReplyDelete
  14. 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
  15. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Web Designing Course in Chennai | web designing training in chennai

    ReplyDelete
  16. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Dot net training in Chennai | dot net course in Chennai

    ReplyDelete
  17. اهم شركات كشف تسربات المياه بالدمام كذلك معرض اهم شركة مكافحة حشرات بالدمام والخبر والجبيل والخبر والاحساء والقطيف كذكل شركة تنظيف خزانات بجدة وتنظيف بجدة ومكافحة الحشرات بالخبر وكشف تسربات المياه بالجبيل والقطيف والخبر والدمام
    شركة تنظيف خزانات بجدة
    شركة مكافحة حشرات بالدمام
    شركة كشف تسربات المياه بالدمام

    ReplyDelete
  18. اهم شركات نقل العفش والاثاث بالدمام والخبر والجبيل اولقطيف والاحساء والرياض وجدة ومكة المدينة المنورة والخرج والطائف وخميس مشيط وبجدة افضل شركة نقل عفش بجدة نعرضها مجموعة الفا لنقل العفش بمكة والخرج والقصيم والطائف وتبوك وخميس مشيط ونجران وجيزان وبريدة والمدينة المنورة وينبع افضل شركات نقل الاثاث بالجبيل والطائف وخميس مشيط وبريدة وعنيزو وابها ونجران المدينة وينبع تبوك والقصيم الخرج حفر الباطن والظهران
    شركة نقل عفش بالرياض
    شركة نقل عفش بالطائف
    شركة نقل عفش بالدمام
    شركة نقل عفش بجدة
    شركة نقل عفش بمكة
    شركة نقل عفش بالمدينة المنورة
    شركة نقل عفش بينبع
    شركة نقل عفش بالخرج
    شركة نقل عفش بالقصيم

    ReplyDelete

About Me

My Photo
Durodola Ridwan is a Professional blogger, SEO Consultant, Certified Web developer/designer and founder of www.realcombiz.com. He love writing about Blogging tips, SEO, and play around with codes. You can connect with him on facebook, twitter @realcombiz, and Google+.

Recent Posts

Let's Connect

Site Links

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