2021 Solution

a[href]:not(:where(
  /* exclude hash only links */
  [href^="#"],
  /* exclude javascript only links */
  [href^="javascript:" i],  
  /* exclude relative but not double slash only links */
  [href^="/"]:not([href^="//"]),
  /* domains to exclude */
  [href*="//stackoverflow.com"],
  /* subdomains to exclude */
  [href*="//meta.stackoverflow.com"],
)):after {
  content: '';
}
<strong>Internal sites:</strong>
<br>Lorem <a href="http://stackoverflow.com">http://stackoverflow.com</a> ipsum
<br>Lorem <a href="/a/5379820">/a/5379820</a> ipsum
<br>Lorem <a href="//stackoverflow.com/a/5379820">//stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="http://stackoverflow.com/a/5379820">http://stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="https://stackoverflow.com/a/5379820">https://stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="https://meta.stackoverflow.com/">https://meta.stackoverflow.com/</a> ipsum
<br>Lorem <a href="ftp://stackoverflow.com">ftp://stackoverflow.com</a> ipsum

<br><br>
<strong>External sites:</strong>
<br>Lorem <a href="ftp://google.com">ftp://google.com</a> ipsum
<br>Lorem <a href="https://google.com">https://google.com</a> ipsum
<br>Lorem <a href="http://google.com">http://google.com</a> ipsum
<br>Lorem <a href="https://www.google.com/search?q=stackoverflow">https://www.google.com/search?q=stackoverflow</a>
<br>Lorem <a href="//www.google.com/search?q=stackoverflow">//www.google.com/search?q=stackoverflow</a>

<br><br>
<strong>Other anchor types</strong>
<br>Lorem <a>no-href</a> ipsum
<br>Lorem <a href="#hash">#hash</a> ipsum
<br>Lorem <a href="javascript:">javascript:</a> ipsum

2014 Solution

Using some special CSS syntax you can easily do this. Here is one way that should work for both the HTTP and HTTPS protocols:

a[href^="http://"]:not([href*="stackoverflow.com"]):after,
a[href^="https://"]:not([href*="stackoverflow.com"]):after {
  content: '';
}
Answer from Shaz on Stack Overflow
🌐
W3Schools
w3schools.com › css › css_external.asp
CSS External Stylesheet
An external style sheet is used when you want to define styles for multiple pages. ... Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section.
Top answer
1 of 8
81

2021 Solution

a[href]:not(:where(
  /* exclude hash only links */
  [href^="#"],
  /* exclude javascript only links */
  [href^="javascript:" i],  
  /* exclude relative but not double slash only links */
  [href^="/"]:not([href^="//"]),
  /* domains to exclude */
  [href*="//stackoverflow.com"],
  /* subdomains to exclude */
  [href*="//meta.stackoverflow.com"],
)):after {
  content: '';
}
<strong>Internal sites:</strong>
<br>Lorem <a href="http://stackoverflow.com">http://stackoverflow.com</a> ipsum
<br>Lorem <a href="/a/5379820">/a/5379820</a> ipsum
<br>Lorem <a href="//stackoverflow.com/a/5379820">//stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="http://stackoverflow.com/a/5379820">http://stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="https://stackoverflow.com/a/5379820">https://stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="https://meta.stackoverflow.com/">https://meta.stackoverflow.com/</a> ipsum
<br>Lorem <a href="ftp://stackoverflow.com">ftp://stackoverflow.com</a> ipsum

<br><br>
<strong>External sites:</strong>
<br>Lorem <a href="ftp://google.com">ftp://google.com</a> ipsum
<br>Lorem <a href="https://google.com">https://google.com</a> ipsum
<br>Lorem <a href="http://google.com">http://google.com</a> ipsum
<br>Lorem <a href="https://www.google.com/search?q=stackoverflow">https://www.google.com/search?q=stackoverflow</a>
<br>Lorem <a href="//www.google.com/search?q=stackoverflow">//www.google.com/search?q=stackoverflow</a>

<br><br>
<strong>Other anchor types</strong>
<br>Lorem <a>no-href</a> ipsum
<br>Lorem <a href="#hash">#hash</a> ipsum
<br>Lorem <a href="javascript:">javascript:</a> ipsum

2014 Solution

Using some special CSS syntax you can easily do this. Here is one way that should work for both the HTTP and HTTPS protocols:

a[href^="http://"]:not([href*="stackoverflow.com"]):after,
a[href^="https://"]:not([href*="stackoverflow.com"]):after {
  content: '';
}
2 of 8
18

This way shows external links ALA Wikipedia:

a[href^="http"]:after {
     content: " " url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAVklEQVR4Xn3PgQkAMQhDUXfqTu7kTtkpd5RA8AInfArtQ2iRXFWT2QedAfttj2FsPIOE1eCOlEuoWWjgzYaB/IkeGOrxXhqB+uA9Bfcm0lAZuh+YIeAD+cAqSz4kCMUAAAAASUVORK5CYII=);
}

​An example can be found here: http://jsfiddle.net/ZkbKp/

Discussions

Adding external css file to html doc
styles.css is not a magic file that exists implicitly. That's a placeholder for the name of a file that you must create. Make a styles.css file, put it in the same directory as the HTML document, then put the styles in that stylesheet file. More on reddit.com
🌐 r/learnprogramming
13
1
June 1, 2022
External file link explanation
In this project you’ll work with an external CSS file to style the page. We’ve already created a styles.css file for you. But before you can use it, you’ll need to link it to the page. Nest a link element within the head element. Give it a rel attribute set to stylesheet and an href attribute ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
July 3, 2023
Adding an "external" icon to target _blank links
If you add an icon using ::before/::after the icon will not be part of the anchor and clickable. You don't have to open external links in a new tab but you should either way use rel="noreferrer noopener" on the link. This will prevent tabjacking and other issues. If you are creating a link that doesn't include text and only has an icon then screen readers will have a hard time. Use aria-label="Whatever External Site Is" on the link in this case for accessibility. More on reddit.com
🌐 r/css
4
5
August 31, 2020
Can I get a little help with external CSS?

I think you should explain more about what you are trying to accomplish, what the result is from your code, and what about it is incorrect from what the desired result should be.

More on reddit.com
🌐 r/learnprogramming
9
1
October 11, 2020
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › link
<link>: The External Resource Link element - HTML | MDN
1 week ago - The <link> HTML element specifies relationships between the current document and an external resource. This element is most commonly used to link to stylesheets, but is also used to establish site icons (both "favicon" style icons and icons for the home screen and apps on mobile devices) among ...
🌐
W3Schools
w3schools.com › css › css_howto.asp
How to add CSS
... With an external style sheet, you can change the look of an entire website by changing just one file! Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section.
🌐
Reddit
reddit.com › r/learnprogramming › adding external css file to html doc
r/learnprogramming on Reddit: Adding external css file to html doc
June 1, 2022 -

I feel stupid and like I have taking 3 steps back in my learning. I am trying to figure out the 3 different implementations of css. Doing External now. Just trying this super simple code, the colors did not show up in the browser like they did on w3schools.

<!DOCTYPE html>

<html>

<head>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<h1>This is a heading</h1>

<p>This is a paragraph.</p>

</body>

</html>

Is it because I don't have a css file on my computer? Do I need to download one? Copy and paste from somewhere? I don't get it. I prefer building with the css and html on the same document because I haven't learned how to do anything else yet. I have been able to follow tutorials fine but for some reason, now it's not clicking.

🌐
freeCodeCamp
forum.freecodecamp.org › html-css
External file link explanation - HTML-CSS - The freeCodeCamp Forum
July 3, 2023 - We’ve already created a styles.css file for you. But before you can use it, you’ll need to link it to the page. Nest a link element within the head element. Give it a rel attribute set to stylesheet and an href attribute ...
Find elsewhere
🌐
College Board
cssprofile.collegeboard.org
CSS Profile Home – CSS Profile | College Board
The CSS Profile is an online application used by colleges and scholarship programs to award non-federal institutional aid.
🌐
Hostinger
hostinger.com › home › tutorials › how to link css to html files in web development
How to Link CSS to HTML Files: An All-You-Need-to-Know Guide
December 16, 2025 - To link CSS to HTML, use the <link> tag within the <head> section of your HTML document, which creates a reference to an external CSS file.
🌐
GeeksforGeeks
geeksforgeeks.org › css › how-to-link-external-css-to-html
How to Link External CSS to HTML? - GeeksforGeeks
The <link> element should have ... link an external CSS file to an HTML document, you need to use the <link> element within the <head> section of your HTML file....
Published   October 8, 2024
🌐
Simmons University
web.simmons.edu › ~grovesd › comm244 › notes › week3 › css-linking
Attaching CSS to your document
In the example below, we're linking to a CSS document called styles.css. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>External Style Sheet Example</title> <link rel="stylesheet" href="styles.css" media="screen"> </head>
🌐
CSS { In Real Life }
css-irl.info › styling-external-links-with-attribute-selectors
CSS { In Real Life } | Styling External Links with Attribute Selectors
October 11, 2023 - We don’t know exactly what that value will be (and it wouldn’t be practical to add each individual URL to our stylesheet!), but we know that internal links (links to other posts on the site) will begin with a slash, whereas external links will begin with https://. So we can style only the links that begin with http by inserting a ^ character into our attribute selector:
🌐
Dummies
dummies.com › article › technology › programming-web-design › general-programming-web-design › how-to-link-external-css-to-a-page-166475
How to Link External CSS to a Page | dummies
July 2, 2025 - To link an HTML page to an external ... just insert a single line of code into the head of your page that references the name and location of the external CSS file relative to the root level of the server on which the site resides...
🌐
freeCodeCamp
freecodecamp.org › news › external-css-stylesheets-how-to-link-css-to-html-and-import-into-head
External CSS Stylesheets – How to Link CSS to HTML and Import into Head
August 24, 2021 - <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> </head> <body> </body> </html> The link element has many uses, and it is important to specify the right attributes so that you can use it to import an external CSS stylesheet.
🌐
DhiWise
dhiwise.com › post › how-to-link-css-to-html-the-ultimate-guide-for-web-designers
How to Link CSS to HTML: Everything You Need to Know
May 29, 2024 - This element is placed within the <head> section of your HTML file and is responsible for establishing the link between your HTML and CSS files. The <link> tag should be located inside the <head> section of your HTML document.
🌐
Tiiny Host
tiiny.host › blog › link-css-to-html
How to Quickly Link CSS to HTML: Stylesheet Steps to Success – Tiiny Host Blog
May 21, 2023 - If you name your CSS file “styles.css” then you’re in good company. The link we just examined is for connecting an external CSS file to your HTML. This is the preferred way to implement CSS styling, particularly if you have more than one CSS file to link.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Styling_basics › Getting_started
Getting started with CSS - Learn web development | MDN
Create a file in the same folder as your HTML document and save it as styles.css. To link styles.css to index.html, add the following line somewhere inside the <head> of the HTML document:
🌐
Michael Gearon
mgearon.com › css › css-style-external-links
CSS How to Style External Links | Michael Gearon
November 16, 2022 - The result is that all links that are not linking to codepen.io will be in a blue background, and those that are will be in an orange colour, example shown below. [codepen_embed height=”268″ theme_id=”0″ slug_hash=”bpEBPx” default_tab=”result” user=”michaelgearon”]See the Pen CSS How to Style External Links by Michael Gearon (@michaelgearon) on CodePen.[/codepen_embed] First published on March 7, 2016 ·
🌐
CodeSignal
codesignal.com › learn › courses › building-a-static-todo-list-with-html-css › lessons › linking-external-css-to-html
Linking External CSS to HTML
type: The type of media we are linking. For CSS, it's "text/css". href: The location of the stylesheet, that is, the path to our style.css file. Add this to the <head> section of your HTML document: If there are multiple external CSS files linked, the later ones will overwrite the rules of the previous ones.
🌐
Simplilearn
simplilearn.com › home › resources › software development › linking css files to html: the ultimate tutorial for you
How to Link CSS to HTML: A Beginner’s Guide
September 17, 2020 - How to Link CSS to HTML? To link the CSS to an HTML file, we use the tag inside the HTML section. link-css-to-html.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States