更改链接CSS颜色而不更改:悬停颜色

这是我以前从未遇到的事情:

/* 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="https://stackoverflow.com/doesntexist">StackOverflow</a>
</div>

我试图改变just:link状态的颜色而不影响:悬停.这在CSS中可行吗?

前两个规则来自库,因此我无法更改它们或它们的顺序.

最佳答案
你的:链接有它之前的类,所以it is more specific,并且悬停当前放在:link之前,所以颜色被:link颜色覆盖.

Here is a neat Specificity Calculator.

受到限制

复制:悬停并将类放在它之前,以增加其特异性.确保使用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="https://stackoverflow.com/doesntexist">StackOverflow</a>
</div>

正确的方法 – 没有限制

>使用.example a:悬停.
>将:hover放在:link之后.确保使用LVHA order(:link,:visited,:hover,:active)(强调我的):

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.

工作实例

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

转载注明原文:更改链接CSS颜色而不更改:悬停颜色 - 代码日志