如何使用伪类来选择除第一个和最后一个之外的所有子项? [英] How do I use pseudo-classes to select all children except first and last?
问题描述
在CSS中,对于下面显示的示例,我如何使样式适用于所有段落,除了第一个和最后一个段落?
< div class =entry>
< p> ...< / p>
< p> ...< / p>
< p> ...< / p>
< p> ...< / p>
< p> ...< / p>
< / div>
我尝试过以下操作来排除第一段,但不起作用: p>
div.entry p:nth-child(n + 1){
/ * ... * / $ b $我也试过了 nth-of-type()
/ code>和 not()
,但无法让他们按照我认为的方式工作。
编辑:我决定将所有要应用样式的< p>
到< div>
中。我接受了bozdoz答案,因为它最接近解决原始问题(即使它只解决了一半)。
解决方案更新的答案:
使用:not(:first-child)
和:not(:last-child)
div.entry p:not(:儿童):not(:last-child)
查看更新 JSFiddle 。
In CSS, for the example shown below, how can I make it such that the styles get applied to all paragraphs, except for the first and the last paragraph?
<div class="entry">
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
</div>
I've tried the following to exclude the first paragraph, but that doesn't work:
div.entry p:nth-child(n+1) {
/* ... */
}
I've also tried nth-of-type()
and not()
, but couldn't get them to work the way I thought they would.
Edit: I've decided to wrap all the <p>
s which I want to apply the style to in a <div>
. I've accepted bozdoz answer, because it came the closest to solving the original problem (even though it solved only half of it).
解决方案 Updated answer:
Use both :not(:first-child)
and :not(:last-child)
div.entry p:not(:first-child):not(:last-child)
See updated JSFiddle.
这篇关于如何使用伪类来选择除第一个和最后一个之外的所有子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!