CSS 如何仅更改锚链接文本的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14831999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:38:41  来源:igfitidea点击:

How to change color of only text of anchor link?

csshover

提问by Neel

I have code like this

我有这样的代码

<a href="#" title="some title">
  <span>Do not change color of this text</span>
  Want to only change this text color
</a>

I want to change the color of anchor tag text only, not color of text in span. Using CSS not jQuery.

我只想更改锚标记文本的颜色,而不是span. 使用 CSS 而不是 jQuery。

回答by Spontifixus

Create a style to ensure the correct colors are being applied:

创建样式以确保应用正确的颜色:

a {
    color: #00f;
}
p, a span {
    color: #000;
}

This sets the color of normal text (p) and spans inside links (a span) to black, whereas it sets the color of the rest of the text in links to blue.

这会将普通文本 ( p) 和跨链接 ( a span)内的颜色设置为黑色,而将链接中其余文本的颜色设置为蓝色。

回答by Neel

You would have to apply two styles - one to your a tag and one to your span tag:

您必须应用两种样式 - 一种应用于您的 a 标签,一种应用于您的 span 标签:

a {color:#000000; /*new colour*/}
a span {color:#aaaaaa; /*originalcolour*/}

回答by SQLGuru

I am assuming you have a CSS for the anchors. Why not create a second CSS for the span within the anchor?

我假设你有一个用于锚点的 CSS。为什么不为锚点内的跨度创建第二个 CSS?

a span {color:#000000;}