15

Is it possible to underline an anchor tag with a color other than its text color? Any example will be appreciated.

EDIT: Is it possible to specify color as hex e.g. #8f867c ?

Taz
  • 3,718
  • 2
  • 37
  • 59
Umer Hayat
  • 1,993
  • 5
  • 31
  • 58

5 Answers5

24

You cannot specify the underline color separately, but you can use a little trick:

a {
    color: red;
    text-decoration: none;
    border-bottom: 1px solid green;
}

Note that the border will not appear exactly where the underline would have appeared, sorry.

Salman A
  • 262,204
  • 82
  • 430
  • 521
5

CSS3 allows you to use text-decoration-color: #8f867c to set the color directly.

p {
  color: green;
  text-decoration: underline;
  text-decoration-color: #8f867c;
}

span {
  color: #8f867c;
}
<p>My underline is <span>#8f867c</span></p>
Chad
  • 2,161
  • 1
  • 19
  • 18
3

Assuming you want the "border" to only show when user moves his mouse over it you should do something like this:

a {
  text-decoration: none;
}

a:hover {
  border-bottom: 1px solid blue;
  text-decoration: none;
}
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
2

It's better you can give border to it. write like this:

a{
text-decoration: none;
color:green;
border-bottom:1px solid red;
}

Check this http://jsfiddle.net/dgc4q/

sandeep
  • 91,313
  • 23
  • 137
  • 155
1

Is it possible to underline an anchor tag with a color other than its text color? Any example will be appreciated.

I find that weird, but I just learned: Yes, it is, if you wrap a span inside.

html:

<a href='#'><span>Howdy Partner</span></a>

css (sass)

html
  font-size: 6em

a
  color: red

span  
  color: green 

enter image description here

little advantage over border-bottom-solution:

You are certainly not affecting base-line, line-height, margin collapse, spacing between lines... (depending on situtation, border-bottom could do that)

Frank N
  • 9,625
  • 4
  • 80
  • 110