Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSS选择不跟随元素的元素_Css_Css Selectors - Fatal编程技术网

CSS选择不跟随元素的元素

CSS选择不跟随元素的元素,css,css-selectors,Css,Css Selectors,我想选择所有不在H3或p之后的p,因此: <img> <p> this is selected</p> <p> this is not</p> <p> this is not</p> <span> <p> this is selected</p> <h3> <p> this is not</p> 但它们不起作用 解决办法是什么

我想选择所有不在H3或p之后的p,因此:

<img>

<p> this is selected</p>

<p> this is not</p>

<p> this is not</p>

<span>

<p> this is selected</p>

<h3>

<p> this is not</p>
但它们不起作用


解决办法是什么?请帮助

使用这样的类

<img>
<p class="sel"> this is selected</p>
<p> this is not</p>
<p> this is not</p>
<span>
<p class="sel"> this is selected</p>
<h3>
<p> this is not</p>

您可以将其表示为css选择器,如下所示:

*:非(h3):非(p)+p

见工作


您最好不要为此使用单个选择器。大部分浏览器不支持这种高级选择器,因此我的建议是将
元素分组到
标记中,然后对它们应用一个类。通过这种方式,你可以在块之前和之后以元素为目标。我只想对所选元素进行文本缩进,这更像是一种渐进式的增强,而不是一种破坏网站的行为。如果是任何属性,我会做一些不同的事情。@Ozzy,一般来说,首先在
p
上设置文本缩进,然后用适用于某些元素后面的
p
元素的规则覆盖它,这样做更简单(也更跨浏览器)。Nelson是第一个。看看他的答案。没有必要,我添加它是为了更好地表达选择器的作用。顺便说一句,问题中的一个解决方案,
:not(h3,p)+p
将在jQuery中工作,并暂时指定为选择器4,但在当前实现选择器3的浏览器中不工作。请参阅,当然,还有一个潜在的浏览器支持问题。如果您需要选择
p
,如果它是第一个子项,只需将其添加到选择器中,这样您就拥有了两个:
p:first child,:not(h3):not(p)+p
<img>
<p class="sel"> this is selected</p>
<p> this is not</p>
<p> this is not</p>
<span>
<p class="sel"> this is selected</p>
<h3>
<p> this is not</p>
.sel {
   /* Styling here */
}
:not(h3):not(p) + p {
    ...
}