简体   繁体   English

css:不是选择器,除了条件以外,所有目标

[英]css :not selector to target everything but a condition

http://jsfiddle.net/m7qLdstp/1/ http://jsfiddle.net/m7qLdstp/1/

<style>
:not(div p), p{
  color: red
}
</style>

<p>This is a paragraph that should be red.</p>
<div><p>This is a paragraph that should not be red.</p></div>

Is it possible to use the css :not selector to (in this case) turn all <p> color red, except for any <p> instead a <div> ? 是否可以使用css :not选择器(在这种情况下)将所有<p>颜色变成红色,但任何<p>代替<div>除外?

I ran a few different variations on jsfiddle but cannot get it to work?! 我在jsfiddle上运行了一些不同的变体,但无法正常工作?

The following will change the color of all <p> elements that are not direct descendants of a <div> : 以下内容将更改不是<div>直接后代的所有<p>元素的颜色:

:not(div) > p{
    color: red;
}

Demo fiddle 演示小提琴

You could just target them separately 您可以将它们分别定位

p{
  color: red
}

div p{color:black}

fiddle 小提琴

it works because div p is more specific than p 它之所以有效,是因为div pp更具体

According to CSS-Tricks , :not() selector can take only a "simple selector", defined as: 根据CSS-Tricks ,:not()选择器只能采用“简单选择器”,定义为:

a Type Selector, Universal Selector, Attribute Selector, Class Selector, ID Selector, or Pseudo Class Selector 类型选择器,通用选择器,属性选择器,类选择器,ID选择器或伪类选择器

but: 但:

may not contain additonal selectors or any pseudo-element selectors. 可能不包含附加选择器或任何伪元素选择器。

However, as other answers suggest, there are ways you could work around this issue and accomplish the behavior at least in this example case and other simple cases. 但是,正如其他答案所暗示的那样,至少在此示例案例和其他简单案例中,您可以通过多种方法来解决此问题并完成行为。

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

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