简体   繁体   English

如何从CSS继承中排除标签(:not不起作用)

[英]How to exclude a tag from css inheritance (:not is not working)

I have an that I want to exclude from the general inheritance of the css. 我有一个要从CSS的一般继承中排除的对象。 I have added ":not" on the css but it still passes the inheritance on the child. 我在CSS上添加了“:not”,但它仍将子级的继承传递给了它。 Also I have added both a new id and a new class and still doesn't follow non of them. 另外,我添加了新的ID和新的类,但仍然没有遵循它们。

HTML code <a id="epLogin" href="#" class="pLogin" > GET ON TOP! </a> HTML代码<a id="epLogin" href="#" class="pLogin" > GET ON TOP! </a> <a id="epLogin" href="#" class="pLogin" > GET ON TOP! </a>

CSS formating CSS格式

a:hover,
a:active,
a:focus,
#Mainbody div.tags a:hover,
#Mainbody a:not(#epLogin) { 
    color: #3498db; 
}

#epLogin {
margin-top: 0px;
background: #00B5F7;
color: #fff;
font-weight: bold;
right: 20px;
height: 38px;
padding: 0 18px;
position: absolute;
top: 10px;
line-height: 39px;
}

You can't prevent element to inherit properties from their parents other than setting those properties to a specific value. 除了将元素的属性设置为特定值之外,您不能阻止元素从其父元素继承属性。

You can exclude an element from a selector, but then you have to do that for each variant of that selector: 您可以从选择器中排除一个元素,但随后必须对该选择器的每个变体进行此操作:

a:not(#epLogin):hover,
a:not(#epLogin):active,
a:not(#epLogin):focus,
#Mainbody div.tags a:not(#epLogin):hover,
#Mainbody a:not(#epLogin)

But I wouldn't do that. 但是我不会那样做。 It's much simpler to set a property to a basic value again. 再次将属性设置为基本值要简单得多。 Especially when using a CSS preprocessor. 特别是在使用CSS预处理器时。 For example: 例如:

a:hover, a:active, a:focus {
    color: #3498db;
}

#epLogin a:hover, #epLogin a:active, #epLogin a:focus {
    color: #fff;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM