Favorites

Visual External Link with CSS

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

An external link is one that takes visitors away from your website to someone else's. A small icon next to these links tells people what to expect before they click, so they aren't surprised to land on a different site.

In this snippet we'll add an external link icon using the ::after pseudo-element, an extra piece of content that CSS adds to the end of an element, without touching your link text.

Hover over the external links: the icon changes colour along with the text, because it's drawn in the link's own colour.

Here are five links, two of which point to other websites. Each external link gets a class of external:

<ul>
  <li><a href="/">CSS Portal home</a></li>
  <li><a href="https://www.example.com/" class="external">An external website</a></li>
  <li><a href="https://www.example.org/" class="external">Another external website</a></li>
  <li><a href="/blog/">CSS Portal blog</a></li>
  <li><a href="/css-properties/">CSS Portal properties</a></li>
</ul>

Later we'll see how to skip the class altogether and let CSS spot external links on its own.

The original version of this snippet loaded a small PNG image with content: url(external.png). That works, but an image can't change colour on hover, doesn't resize with the text and needs a separate file. Instead, we'll draw the icon with a CSS mask:

a.external::after {
  content: "";
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-left: 0.25em;
  vertical-align: -0.05em;
  background-color: currentColor;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 4h6v6M20 4l-9 9M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5'/%3E%3C/svg%3E") no-repeat center / contain;
}

How it works

Think of a mask as a stencil. The pseudo-element is a small square filled with colour, and the mask, an SVG drawing of the icon, decides which parts of that square are visible. Only the lines of the icon show through.

LineWhat it does
content: "";Creates the pseudo-element. Without content, it won't appear at all.
display: inline-block;Lets the pseudo-element have a width and height while sitting in line with the text
width, heightSized in em, so the icon grows and shrinks with the link's font size
margin-left: 0.25em;A small gap so the icon doesn't touch the text. (See margin-left.)
vertical-align: -0.05em;Nudges the icon down slightly to line up with the letters. (See vertical-align.)
background-color: currentColor;Fills the square with the link's current text colour
maskThe SVG stencil, centred and scaled to fit the square

Because the colour comes from currentColor, the icon automatically matches the link, including hover colours, visited links and dark mode.

What's in that long url()?

The SVG icon is written straight into the CSS as a data URI, a way of including a small file as text instead of linking to it. Some characters, like < and >, are encoded as %3C and %3E so they're safe inside the URL. The result: no image file to upload, and nothing extra to download.

Tip: you can swap in any simple icon. Copy its SVG code, encode it with a tool such as our image to data URI converter or any online SVG encoder, and paste it in place of the one above. The colours inside the SVG don't matter, because the mask only uses its shape.

Adding a class to every external link is easy to forget. Instead, CSS attribute selectors can find them by looking at the link's address:

a[href^="http"]:not([href*="cssportal.com"])::after {
  /* the same icon styles as before */
}
  • [href^="http"] matches links whose address starts with "http", which covers both http:// and https://. Links to your own pages are usually written without it, such as /blog/.
  • :not([href*="cssportal.com"]) skips any link whose address contains your own domain, in case some internal links are written in full. Replace it with your own domain name.

No classes needed: only the two links to other websites get the icon.

Good to know: if you ever need to leave the icon off a particular external link, such as one that's already an image or button, add an exception like :not(.no-icon) to the selector, and give that link the class.

The icon is purely visual. Visitors who use a screen reader (software that reads pages aloud) won't hear anything to tell them a link leads off-site. The most reliable fix is a short piece of text that's hidden visually but still read aloud:

<a href="https://www.example.com/" class="external">
  An external website<span class="visually-hidden"> (external site)</span>
</a>
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

This shrinks the text to a single invisible pixel without removing it from the page, so screen readers still announce "An external website (external site)".

Why not just hide it with display: none? display: none hides content from screen readers as well as from the screen. The visually-hidden pattern above is specifically designed to hide text from sight only.

Links that open in a new tab

Opening links in a new tab with target="_blank" can be disorientating, especially for screen reader users, because the Back button no longer works as expected. If you do it, say so: add "(opens in a new tab)" as hidden text, or as visible text if you can. The original version of this post also had a small typo here, target"_blank" with the equals sign missing, which browsers ignore.

A different icon for new tabs

You can target links that open in a new tab with a[target="_blank"], and give them a distinct icon, such as two overlapping squares:

a[target="_blank"]::after {
  /* same size and position styles as before */
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='7' width='14' height='14' rx='2'/%3E%3Cpath d='M8 3h11a2 2 0 0 1 2 2v11'/%3E%3C/svg%3E") no-repeat center / contain;
}

The quickest option: a Unicode arrow

If you just need something fast, a Unicode character works with no SVG at all:

a.external::after {
  content: "\2197";  /* the north-east arrow: ↗ */
  margin-left: 0.15em;
}

The downside is that characters look slightly different in every font, and some devices may show them as a colourful emoji. The SVG mask gives you full control.

The complete code

/* Mark external links automatically. Replace cssportal.com with your domain. */
a[href^="http"]:not([href*="cssportal.com"])::after {
  content: "";
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-left: 0.25em;
  vertical-align: -0.05em;
  background-color: currentColor;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 4h6v6M20 4l-9 9M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5'/%3E%3C/svg%3E") no-repeat center / contain;
}

/* Text for screen readers only */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

Wrapping up

An external link icon is a small touch that helps visitors know where they're going. Draw it with a mask and currentColor so it always matches your links, let attribute selectors find external links for you, and add a few words of hidden text so screen reader users get the same information.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee