访问时禁用锚标记的颜色更改

发布于 2024-12-02 12:53:46 字数 177 浏览 0 评论 0原文

我必须在访问时禁用锚标记的颜色更改。我这样做了:(

a:visited{ color: gray }

链接在访问之前颜色是灰色的。)但这是我在访问链接后明确声明颜色的一种方式,这又是一种颜色变化。

如何在访问时禁用锚标记的颜色更改而不进行任何显式颜色更改?

I have to disable the color change of an anchor tag when visited. I did this:

a:visited{ color: gray }

(The link is gray in color before visited.) But this is a way where I explicitly state the color after the link is visited, which is again a color change.

How can I disable the color change of an anchor tag when visited without doing any explicit color changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

温柔少女心 2024-12-09 12:53:46

如果您只是希望锚点颜色与锚点的父元素保持相同,您可以利用继承:

a, a:visited, a:hover, a:active {
  color: inherit;
}

注意,无需为每个选择器重复该规则;只需使用逗号分隔的选择器列表(对于锚伪元素来说顺序很重要)。另外,如果您想有选择地禁用特殊锚点颜色,您可以将伪选择器应用于类:

.special-link, .special-link:visited, .special-link:hover, .special-link:active {
  color: inherit;
}

您的问题仅询问访问过的状态,但我假设您指的是所有状态。如果您希望允许对所有未访问的选择器进行颜色更改,您可以删除其他选择器。

If you just want the anchor color to stay the same as the anchor's parent element you can leverage inherit:

a, a:visited, a:hover, a:active {
  color: inherit;
}

Notice there is no need to repeat the rule for each selector; just use a comma separated list of selectors (order matters for anchor pseudo elements). Also, you can apply the pseudo selectors to a class if you want to selectively disable the special anchor colors:

.special-link, .special-link:visited, .special-link:hover, .special-link:active {
  color: inherit;
}

Your question only asks about the visited state, but I assumed you meant all of the states. You can remove the other selectors if you want to allow color changes on all but visited.

睡美人的小仙女 2024-12-09 12:53:46

你不能。您只能设置访问状态的样式。

对于发现此内容的其他人,请确保它们的顺序正确:

a {color:#FF0000;}         /* Unvisited link  */
a:visited {color:#00FF00;} /* Visited link    */
a:hover {color:#FF00FF;}   /* Mouse over link */
a:active {color:#0000FF;}  /* Selected link   */

You can't. You can only style the visited state.

For other people who find this, make sure that you have them in the right order:

a {color:#FF0000;}         /* Unvisited link  */
a:visited {color:#00FF00;} /* Visited link    */
a:hover {color:#FF00FF;}   /* Mouse over link */
a:active {color:#0000FF;}  /* Selected link   */
空城仅有旧梦在 2024-12-09 12:53:46

对于 :hover 覆盖 :visited,并确保 :visited 与初始颜色相同,:hover 必须位于 :visited 之后。

因此,如果您想禁用颜色更改,a:visited 必须位于 a:hover 之前。像这样:

a { color: gray; }
a:visited { color: orange; }
a:hover { color: red; }

要禁用 :visited 更改,您可以使用非伪类对其进行样式设置:

a, a:visited { color: gray; }
a:hover { color: red; }

For :hover to override :visited, and to make sure :visited is the same as the initial color, :hover must come after :visited.

So if you want to disable the color change, a:visited must come before a:hover. Like this:

a { color: gray; }
a:visited { color: orange; }
a:hover { color: red; }

To disable :visited change you would style it with non-pseudo class:

a, a:visited { color: gray; }
a:hover { color: red; }
回忆那么伤 2024-12-09 12:53:46

可以使用 LinkText 系统颜色value 从 CSS 4 颜色模块获取浏览器默认值(如果您希望遵循该值)。

a:visited { color: LinkText; }
<a href="https://stackoverflow.com">link</a>

但请注意:

这些值也可以在其他上下文中使用,但并未得到浏览器的广泛支持。

它至少可以在 Firefox 98 和 Chromium 99 中运行。

It’s possible to use the LinkText system color value from the CSS 4 Color Module to obtain the browser default value if one wishes to defer to that.

a:visited { color: LinkText; }
<a href="https://stackoverflow.com">link</a>

However note:

These values may also be used in other contexts, but are not widely supported by browsers.

It at least appears to work in Firefox 98 and Chromium 99.

女皇必胜 2024-12-09 12:53:46

如果您使用 SASS 之类的预处理器,则可以使用 @extend 功能:

a:visited {
  @extend a;
}

因此,您将看到为每个带有 的样式自动添加的 a:visited 选择器>a 选择器,所以要小心使用它,因为你的样式表的大小可能会增加很多。

作为妥协,您可以仅在您真正需要的块中添加@extend。

If you use some pre-processor like SASS, you can use @extend feature:

a:visited {
  @extend a;
}

As a result you will see automatically-added a:visited selector for every style with a selector, so be carefully with it, because your style-table may be increase in size very much.

As a compromise you can add @extend only in those block wich you really need.

树深时见影 2024-12-09 12:53:46

您可以通过同时调用 a:linka:visited 选择器来解决此问题。然后使用 a:hover 选择器。

a:link, a:visited
{color: gray;}
a:hover
{color: skyblue;}

You can solve this issue by calling a:link and a:visited selectors together. And follow it with a:hover selector.

a:link, a:visited
{color: gray;}
a:hover
{color: skyblue;}
深海少女心 2024-12-09 12:53:46

我认为如果我为 a:visited 设置颜色,那就不好了:你必须知道标签 a 的默认颜色,并且每次都将其与 a 同步:访问过。

我不想知道默认颜色(它可以在应用程序的 common.css 中设置,或者您可以使用外部样式)。

我认为这是一个很好的解决方案:

HTML:

<body>
    <a class="absolute">Test of URL</a>
    <a class="unvisited absolute" target="_blank" href="google.ru">Test of URL</a>
</body>

CSS:

.absolute{
    position: absolute;
}
a.unvisited, a.unvisited:visited, a.unvisited:active{
    text-decoration: none;
    color: transparent;
}

I think if I set a color for a:visited it is not good: you must know the default color of tag a and every time synchronize it with a:visited.

I don't want know about the default color (it can be set in common.css of your application, or you can using outside styles).

I think it's nice solution:

HTML:

<body>
    <a class="absolute">Test of URL</a>
    <a class="unvisited absolute" target="_blank" href="google.ru">Test of URL</a>
</body>

CSS:

.absolute{
    position: absolute;
}
a.unvisited, a.unvisited:visited, a.unvisited:active{
    text-decoration: none;
    color: transparent;
}
北斗星光 2024-12-09 12:53:46
a {
    color: orange !important;
}

!important 的作用是,除非使用另一个 !important,否则无法覆盖相关属性。除非绝对必要,否则通常认为使用 !important 是不好的做法;但是,我想不出任何其他仅使用 CSS 来“禁用”:visited 的方法。

a {
    color: orange !important;
}

!important has the effect that the property in question cannot be overridden unless another !important is used. It is generally considered bad practice to use !important unless absolutely necessary; however, I can't think of any other way of ‘disabling’ :visited using CSS only.

哥,最终变帅啦 2024-12-09 12:53:46

对于那些动态应用类(即活动)的人:
只需在“a”标签内添加一个带有 href 属性的“div”标签即可:

<a href='your-link'>
  <div>
    <span>your link name</span>
  </div>
</a>

For those who are dynamically applying classes (i.e. active):
Simply add a "div" tag inside the "a" tag with an href attribute:

<a href='your-link'>
  <div>
    <span>your link name</span>
  </div>
</a>
九厘米的零° 2024-12-09 12:53:46

删除选择器或将其设置为与文本正常显示的颜色相同。

Either delete the selector or set it to the same color as your text appears normally.

半世蒼涼 2024-12-09 12:53:46

使用:

a:visited {
    text-decoration: none;
}

但只会影响尚未点击的链接。

Use:

a:visited {
    text-decoration: none;
}

But it will only affect links that haven't been clicked on yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文