访问时禁用锚标记的颜色更改
我必须在访问时禁用锚标记的颜色更改。我这样做了:(
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您只是希望锚点颜色与锚点的父元素保持相同,您可以利用继承:
注意,无需为每个选择器重复该规则;只需使用逗号分隔的选择器列表(对于锚伪元素来说顺序很重要)。另外,如果您想有选择地禁用特殊锚点颜色,您可以将伪选择器应用于类:
您的问题仅询问访问过的状态,但我假设您指的是所有状态。如果您希望允许对所有未访问的选择器进行颜色更改,您可以删除其他选择器。
If you just want the anchor color to stay the same as the anchor's parent element you can leverage 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:
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.
你不能。您只能设置访问状态的样式。
对于发现此内容的其他人,请确保它们的顺序正确:
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:
对于
:hover
覆盖:visited
,并确保:visited
与初始颜色相同,:hover 必须位于
:visited
之后。因此,如果您想禁用颜色更改,
a:visited
必须位于a:hover
之前。像这样:要禁用
:visited
更改,您可以使用非伪类对其进行样式设置: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 beforea:hover
. Like this:To disable
:visited
change you would style it with non-pseudo class:可以使用
LinkText
系统颜色value 从 CSS 4 颜色模块获取浏览器默认值(如果您希望遵循该值)。但请注意:
它至少可以在 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.However note:
It at least appears to work in Firefox 98 and Chromium 99.
如果您使用 SASS 之类的预处理器,则可以使用
@extend
功能:因此,您将看到为每个带有
的样式自动添加的
选择器,所以要小心使用它,因为你的样式表的大小可能会增加很多。a:visited
选择器>a作为妥协,您可以仅在您真正需要的块中添加@extend。
If you use some pre-processor like SASS, you can use
@extend
feature:As a result you will see automatically-added
a:visited
selector for every style witha
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.
您可以通过同时调用
a:link
和a:visited
选择器来解决此问题。然后使用a:hover
选择器。You can solve this issue by calling
a:link
anda:visited
selectors together. And follow it witha:hover
selector.我认为如果我为
a:visited
设置颜色,那就不好了:你必须知道标签a
的默认颜色,并且每次都将其与a 同步:访问过。
我不想知道默认颜色(它可以在应用程序的
common.css
中设置,或者您可以使用外部样式)。我认为这是一个很好的解决方案:
HTML
:CSS
:I think if I set a color for
a:visited
it is not good: you must know the default color of taga
and every time synchronize it witha: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
:CSS
:!important
的作用是,除非使用另一个!important
,否则无法覆盖相关属性。除非绝对必要,否则通常认为使用!important
是不好的做法;但是,我想不出任何其他仅使用 CSS 来“禁用”:visited
的方法。!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.对于那些动态应用类(即活动)的人:
只需在“a”标签内添加一个带有 href 属性的“div”标签即可:
For those who are dynamically applying classes (i.e. active):
Simply add a "div" tag inside the "a" tag with an href attribute:
删除选择器或将其设置为与文本正常显示的颜色相同。
Either delete the selector or set it to the same color as your text appears normally.
使用:
但只会影响尚未点击的链接。
Use:
But it will only affect links that haven't been clicked on yet.