Style External Links with CSS
Styling external links is a common practice on most informational sites likes Wikipedia. As a user, it's nice to know when you're being sent to another resource. Many sites do the external links check on the server side, adding a `rel=external` attribute value or `external` class to external links. In some cases that isn't possible or plausible. After trolling around the interwebs, I found the following useful CSS snippet for styling external links:
/* long version */
a[href^="http://"]:not([href*="mysite.com"]),
a[href^="https://"]:not([href*="mysite.com"]),
a[href^="//"]:not([href*="mysite.com"]), {
}
/* shorter version! */
a[href*="//"]:not([href*="mysite.com"]) {
/* external link styles, use :before or :after if you want! */
}
First you have to qualify the start of the link, then qualify the domain. Internal links wont match and external links wont match the comparison. A useful snippet and something to keep in your library in case you need it!
![5 HTML5 APIs You Didn’t Know Existed]()
When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It." Can you blame us though? We watched the fundamental APIs stagnate for so long that a basic feature...
![I’m an Impostor]()
This is the hardest thing I've ever had to write, much less admit to myself. I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life. All of those feelings were very...
![HTML5’s window.postMessage API]()
One of the little known HTML5 APIs is the window.postMessage API. window.postMessage
allows for sending data messages between two windows/frames across domains. Essentially window.postMessage acts as cross-domain AJAX without the server shims. Let's take a look at how window.postMessage
works and how you...
![Advanced CSS Printing – Using JavaScript Double-Click To Remove Unwanted DIVs]()
Like any good programmer, I'm constantly searching around the internet for ideas and articles that can help me improve my code. There are thousands of talented programmers out there so I stumble upon some great articles and code snippets that I like to print out...
What if there is a link in my website like http://external.com/?referer=mysite.com ?
You add can another pattern match for whichever referer pattern you use:
Thanks for the snippets :)
David, you can do the same with shorter selector:
Thanks for the code :)
Nice snippets, I love this but how about browser compatibility?
So, what styles are typically used for external websites? A change in font color? Background color?
Is have used this slidely different for my wordpress site:
How to ignore Links With images?
example: http://jsfiddle.net/Ridermansb/NRw97/1/
@Riderman You don’t. CSS is designed to to be applied in one traversal of the DOM tree. This limits it to selectors that are based solemnly on what was before (higher up) in the dom tree. You cannot select on what comes after/deeper in the DOM tree in CSS.
In your example you are trying to style an a element based on the fact, that an img element is deeper in the tree.
Solution: use a css-class for such a elements that should ignore the styling done through the [href=??] matching selectors.
a.no-external-link-style {
/*undo styling through a[href*=”//”]:not([href*=”mysite.com”]) */
}
@Riderman Use JS / jQuery to add a class to links that wrap images, then add a css rule that removes or hides your
:before
, based on that class. I’m using:after
, and FontAwesome..