CSS: Add Icon to Links

By Xah Lee. Date: . Last updated: .

You can add a little icon to links to indicate what type of link it is.

For example, suppose you have links to Twitter like this:

<a href="https://twitter.com/X7RgW">Xah Lee</a>

And you want browser to show a icon, like this

CSS Google Plus Twitter Facebook icon link-2
Link with favicons, Twitter, Facebook, Google Plus

Instead of using a img tag for each link, you should use CSS to add them, by matching the URL.

Here is the CSS code:

a[href*="twitter.com/"] {
background:url(https://twitter.com/favicon.ico) no-repeat left center;
padding-left:19px;
}

a[href*="facebook.com/"] {
background:url(https://www.facebook.com/favicon.ico) no-repeat left center;
padding-left:19px;
}

The CSS selector a[href*="twitter.com/"] matches any “a” tag with “href” attribute that has value contanining the string “twitter.com/”. For a full list of such selector syntax, see: CSS 3 Selector Syntax .

CSS: Background Image