CSS link:hover 样式仅适用于第一个实例,即使使用 class

CSS link:hover style only applies to the first instance even with using class

我已经在这件事上卡了一段时间了,没能想出解决办法。我有一些使用 class 设置样式的 link。但是,似乎 :hover 和 :visited 状态样式只适用于第一个 link,即使我专门使用 class 来设置所有它们的样式。真不知道哪里有问题。

这是我的代码:

<div class="container">
<div class="row">
  <div class="col-md-6 push-md-3 max-auto main animsition">
    <h3 class="text-center">Contact <span class="dev">Me</span></h3>
    <p class="contactLinks"><a href="mailto:#"><i class="fa fa-envelope" aria-hidden="true"></i> - Email me</a></p>
    <p class="contactLinks"><a href="#" target="_blank"><i class="fa fa-facebook-official" aria-hidden="true"></i> - Facebook</a></p>
    <p class="contactLinks"><a href="#" target="_blank"><i class="fa fa-twitter-square" aria-hidden="true"></i> - Twitter</a></p>
    <p class="contactLinks"><a href="#" target="_blank"><i class="fa fa-linkedin-square" aria-hidden="true"></i> - LinkedIn</a></p>
  </div>
</div>
</div>

CSS:

.main > h3 {
  padding-bottom: 30px;
}

.contactLinks a {
  font-size: 150%;
  color: #262626; 
}

.contactLinks a:hover {
  color: #6E8A71;
  text-decoration: none;
}

.contactLinks a:visited {
  color: #262626;
  text-decoration: none;
}

尝试将 :hover 放在 :visited 事件之后。您还可以添加 .contactLinks a:visited:hover,这样您就可以确保将鼠标悬停在已访问的 link 上时,您会得到想要的结果

如您所知,您不能在 :visited link 上设置 text-decoration。它与浏览器历史记录安全原因有关。看这里 > Privacy visited links

您可以改为使用 border-bottom 来模拟下划线。请参阅下面更新的代码段

.main > h3 {
  padding-bottom: 30px;
}

.contactLinks a {
  font-size: 150%;
  color: #262626; 
  text-decoration: none;
  border-bottom:1px solid black;
}



.contactLinks a:visited {
  color: #262626;
  border-bottom:1px solid transparent;
  
}

.contactLinks a:hover,.contactLinks a:visited:hover{
  color: #6E8A71;
  border-bottom:1px solid transparent;
}
<div class="container">
<div class="row">
  <div class="col-md-6 push-md-3 max-auto main animsition">
    <h3 class="text-center">Contact <span class="dev">Me</span></h3>
    <p class="contactLinks"><a href="mailto:#"><i class="fa fa-envelope" aria-hidden="true"></i> - Email me</a></p>
    <p class="contactLinks"><a href="#" target="_blank"><i class="fa fa-facebook-official" aria-hidden="true"></i> - Facebook</a></p>
    <p class="contactLinks"><a href="#" target="_blank"><i class="fa fa-twitter-square" aria-hidden="true"></i> - Twitter</a></p>
    <p class="contactLinks"><a href="#" target="_blank"><i class="fa fa-linkedin-square" aria-hidden="true"></i> - LinkedIn</a></p>
  </div>
</div>

我将你的代码粘贴到 codepen 中,它似乎工作得很好。 您是在本地主机还是服务器上尝试这个? 您是否尝试清理缓存?

因为"href="#""。将其更改为 "href = "#something""