2

I would like to give different color to different external links using just CSS.

For example, we have few external links:

<a href="http://stackoverflow.com">StackOverflow</a>
<a href="http://example.edu">home page</a>

I want to give different colors to links based on their domain names(.edu, .org, .com etc.)

Pang
  • 9,564
  • 146
  • 81
  • 122
pragya.go
  • 169
  • 4
  • 13
  • Possible duplicate of [CSS rule based on content](http://stackoverflow.com/questions/1777357/css-rule-based-on-content) – Roshan Bhumbra Sep 21 '16 at 20:10
  • 1
    Note that the 'domain names' are 'stackoverflow.com' and 'example,edu' what you're referring to is the 'TLD' (top level domain), the 'com' and 'edu' part. – David Thomas Sep 21 '16 at 20:20

1 Answers1

3

You can use an attr selector like this:

a[href$=".com"] {
  color: red;
}
a[href$=".edu"] {
  color: purple;
}
<a href="http://stackoverflow.com">StackOverflow</a> 
<a href="http://example.edu">home page</a>

In this case we can use $ as filter:

[attr$=value]

Represents an element with an attribute name of attr and whose last value is suffixed by "value".

You can use more combinations to evaluate the attribute you want

DaniP
  • 37,813
  • 8
  • 65
  • 74