:visited

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

:visited CSS 伪类在用户访问过链接后应用。出于隐私原因,可以使用此选择器修改的样式非常有限。:visited 伪类仅适用于具有 href 属性的 <a><area> 元素。

试一试

p {
  font-weight: bold;
}

a:visited {
  color: forestgreen;
  text-decoration-color: hotpink;
}
<p>Pages that you might have visited:</p>
<ul>
  <li>
    <a href="https://mdn.org.cn">MDN Web Docs</a>
  </li>
  <li>
    <a href="https://www.youtube.com/">YouTube</a>
  </li>
</ul>
<p>Pages unlikely to be in your history:</p>
<ul>
  <li>
    <a href="https://mdn.org.cn/missing-1">Random MDN page</a>
  </li>
  <li>
    <a href="https://example.com/missing-1">Random Example page</a>
  </li>
</ul>

:visited 和未访问的 :link 伪类定义的样式,可以被任何后续具有至少相同特异性的用户操作伪类(:hover:active)覆盖。为了正确设置链接样式,请按照 LVHA-order:link:visited:hover:active)将 :visited 规则置于 :link 规则之后,但在 :hover:active 规则之前。:visited 伪类和 :link 伪类是互斥的。

隐私限制

出于隐私原因,浏览器严格限制了你可以使用此伪类应用的样式,以及它们的使用方式

注意: 有关这些限制及其原因的更多信息,请参阅 隐私与 :visited 选择器

语法

css
:visited {
  /* ... */
}

示例

原本没有颜色或透明的属性不能用 :visited 修改。在可以使用此伪类设置的属性中,你的浏览器可能只为 colorcolumn-rule-color 设置了默认值。因此,如果你想修改其他属性,你需要为它们在 :visited 选择器之外提供一个基本值。

HTML

html
<a href="#test-visited-link">Have you visited this link yet?</a><br />
<a href="">You've already visited this link.</a>

CSS

css
a {
  /* Specify non-transparent defaults to certain properties,
     allowing them to be styled with the :visited state */
  background-color: white;
  border: 1px solid white;
}

a:visited {
  background-color: yellow;
  border-color: hotpink;
  color: hotpink;
}

结果

规范

规范
HTML
# selector-visited
选择器 Level 4
# visited-pseudo

浏览器兼容性

另见