Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change link CSS color without changing :hover color

Tags:

css

Here's something I've never encountered before:

/* These first two rules are in a CSS library */
a { 
  color: #1EAEDB;
}

a:hover { 
  color: #0FA0CE;
}

/* This rule is my own */
.example a:link {
  color: #000;
}
<div class="example">
  <a href="http://stackoverflow.com/doesntexist">StackOverflow</a>
</div>

I'm trying to change the color of just :link state without affecting :hover. Is this possible in CSS?

The first two rules are from a library, so I can't change them or their order.

like image 898
nathancahill Avatar asked Dec 16 '14 00:12

nathancahill


People also ask

How do you make links change color when you hover over them?

To change the color of your link on hover, use the :hover pseudo property on the link's class and give it a different color.

How do you change the link color when hovering over a link in HTML?

If you want to change the link color when moving the mouse over a link, you only need the A:hover line. hover - The hover option is the color that the text changes to when the mouse is over the link. In this example, the link changes to a blue color when a mouse cursor is hovering over a link.

How do I stop links from changing color?

Preventing the change You need only create a hyperlink, highlight it, and open the font color dropdown via the “Home” tab. From there, you may select your intended link color. Following the link will not alter this color.


1 Answers

Your :link has the class before it, so it is more specific, and the hover is currently placed before the :link, so the color is overwritten by the :link color.

Here is a neat Specificity Calculator.

With the limitations imposed

Duplicate the :hover and place the class before it, to increase its specificity. Make sure that you use the LVHA order (:link, :visited, :hover, :active)

a {
  color: #1EAEDB;
}
a:hover {
  color: #0FA0CE;
}
.example a:link {
  color: #000;
}
.example a:hover {
  color: #0FA0CE;
}
<div class="example">
  <a href="http://stackoverflow.com/doesntexist">StackOverflow</a>
</div>

The proper way - without the limitations

  1. Use .example a:hover.

  2. Place the :hover after the :link. Make sure that you use the LVHA order (:link, :visited, :hover, :active) (Emphasis mine):

    The :link CSS pseudo-class lets you select links inside elements. This will select any link, even those already styled using selector with other link-related pseudo-classes like :hover, :active or :visited. In order to style only non-visited links, you need to put the :link rule before the other ones, as defined by the LVHA-order: :link — :visited — :hover — :active.

Working Example

a {
  color: #1EAEDB;
}
.example a:link {
  color: #000;
}
.example a:hover {
  color: #0FA0CE;
}
<div class="example">
  <a href="http://stackoverflow.com/doesntexist">StackOverflow</a>
</div>
like image 128
misterManSam Avatar answered Sep 28 '22 12:09

misterManSam