css - 更改链接 CSS 颜色而不更改 :hover color

标签 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="http://stackoverflow.com/doesntexist">StackOverflow</a>
</div>

我正在尝试仅更改 :link 状态的颜色而不影响 :hover。这在 CSS 中可能吗?

前两个规则来自库,所以我无法更改它们或它们的顺序。

最佳答案

你的 :link 前面有类,所以 it is more specific ,悬停当前位于 :link 之前,因此颜色被 :link 颜色覆盖。

Here is a neat Specificity Calculator .

施加限制

复制 :hover 并将类放在它之前,以增加其特异性。确保使用 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>

正确的方法——不受限制

  1. 使用.example a:hover

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

关于css - 更改链接 CSS 颜色而不更改 :hover color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27495411/

相关文章:

html - CSS webkit 错误 : stacked floats with width 0 and hidden overflow

html - 当我将鼠标悬停在其父 li 上方时,为什么链接的颜色没有改变?

javascript - 使 Canvas 与父级一样宽和一样高

html - 将整个标签元素对齐到其父 div 的底部

javascript - 使用 jQuery 在一组图像上定位文本

html - 相同的目标 ="_blank"对于不同的 href 表现不同

html - 在全屏图像上对齐按钮

javascript - CSS 在两个元素之间画一条线

jquery - 通过 jQuery 更改 CSS 属性显示简短的视觉过渡

jquery - 新的 FireFox BorderRadius/Rotate 动画错误?