如何从继承 CSS 规则中排除特定元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5507253/
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-30 00:06:21  来源:igfitidea点击:

How can I exclude a specific element from inheriting CSS rules?

csscss-selectors

提问by Justin Meltzer

I have this CSS:

我有这个 CSS:

a {
  color:#19558D;
  padding:3px 5px;
  text-decoration:none;
}

a:hover {
  background-color:#D1E1EA;
  color:#19558D;
  text-decoration:none;
}

It applies to all links, but what if I don't want it to apply to a specific link on the page? What can I do?

它适用于所有链接,但如果我不希望它适用于页面上的特定链接怎么办?我能做什么?

采纳答案by NotMe

You can apply your own class or inline style to the link in question.

您可以将自己的类或内联样式应用于相关链接。

for example:

例如:

<a href="#" class="MyNewClass" />

or

或者

<a href="#" style="color:red;" />

回答by Alex

There are two ways you can do this.

有两种方法可以做到这一点。

First way is to use the :not()selector and give your link that you don't want the styles applied to class:

第一种方法是使用:not()选择器并提供您不希望将样式应用于类的链接:

a:not(.unstyled):hover {
  background-color:#D1E1EA;
  color:#19558D;
  text-decoration:none;
}

However, the :not()selector is not supported in IE8 or less, so the second option is to give your unstyled links a class, and override those properties for that link with that class:

但是,:not()IE8 或更低版本不支持选择器,因此第二个选项是为您的无样式链接提供一个类,并使用该类覆盖该链接的这些属性:

a.unstyled:hover {
  background-color:none;
  color:#000
  text-decoration:none;
}