1

I'm trying to make it so my links remain white in colour but when the mouse hovers on them they turn grey and have an underline but it isn't working, the link works but it just stays white, here's my CSS code:

a:hover {
    text-decoration: underline;
    color:#666666
}

a:visited {
    text-decoration: none;
    color:#FFFFFF
}

a:link {
    text-decoration: none;
    color:#FFFFFF
}

5 Answers 5

2

To clarify icedwater's answer a little, psuedoclasses such as :hover, :link and so on all have the same specificity as classes, and more importantly they have the same specificity as each other.

I'm with you, personally I think it'd be good if :visited implied !important since it's a user-controlled state, but that would make it more complicated so... yeah. Just rearrange your groups - in fact, the order you need is the exact opposite to the one you have now.

3
  • Thanks :) I didn't know that :hover was as specific as :link. I just shifted the last to the top. This order also leaves the visited links white, but you get my +1 for clarifying.
    – icedwater
    Commented Jul 4, 2013 at 3:26
  • 1
    If you move :hover to be last, then it will also affect visited links. You can even define :hover:visited if you want a different colour for when a link is both visited and hovered. Commented Jul 4, 2013 at 3:28
  • If you are going to clarify someone else's answer either provide a comment to that answer or add in your explanation to that answer. If icedwater changes his name or his answer gets deleted for whatever reason, no one will know who or what you are talking about.
    – Gordon
    Commented Jul 4, 2013 at 12:37
1

the hover properties set out for <a> are defined for the normal state (which means that it covers both visited and unvisited links) however you have defined a:visited and a:link as well (and also mentioned it after the :hover only declaration), the css parser will instead give the properties of these definitions higher precedence.

The Workarounds

Workaround #1

Make it more specific by changing:

a:hover {
    text-decoration: underline;
    color:#666666
}

to

a:hover, a:visited:hover, a:link:hover {
    text-decoration: underline;
    color:#666666
}

A demonstration @ http://jsfiddle.net/Wz6aR/

Workaround #2

To alter the precedence change the declaration order to:

a:visited {
    text-decoration: none;
    color:#FFFFFF
}

a:link {
    text-decoration: none;
    color:#FFFFFF
}

a:hover {
    text-decoration: underline;
    color:#666666
}

A demonstration @ http://jsfiddle.net/9cGPv/

1

You should order link pseudo classes like this:

a {...}
a:link {...}
a:visited {...}
a:focus {...}
a:hover {...}
a:active {...}
0

As CSS is cascading, the last rule overwrites the ones before. If you shift the a:link definition which is more general up, it should work fine. This is of course the most minimal change that happens to work.

0

You can track down such issues easily by examining the styles in Chrome devtools. Did you do that?

  1. Right-click on the element you're worried about and choose "Inspect element".

  2. The style tab will open. Next to element.style, over on the right, you'll see three icons. The middle one is a dotted rectangle with a little arrow.

  3. Click on that and a pane will open. Set the :hover state. See screenshot below.

  4. Now examine the rules being applied. You will notice that the properties being set by your :hover rule are crossed out. This means that they are being overidden. You will also see that the properties in your :link rule are the ones being applied. Why is that? Because, as other posters have pointed out, that rule comes later in your CSS and thus takes precedence.


Debugging CSS styles in Chrome devtools


A quick google of "hover css" would have given you the relevant page on MDN, always an excellent resource, which states in the very first paragraph:

The :hover CSS pseudo-class matches when the user designates an element with a pointing device, but does not necessarily activate it. This style may be overridden by any other link-related pseudo-classes, that is :link, :visited, and :active, appearing in subsequent rules. In order to style appropriately links, you need to put the :hover rule after the :link and :visited rules but before the :active one, as defined by the LVHA-order: :link — :visited — :hover — :active.

SO is not a crowd-sourced debugging forum. Learn to debug for yourself.

Not the answer you're looking for? Browse other questions tagged or ask your own question.