简体   繁体   English

CSS3 nth:子编号

[英]CSS3 nth:child number

I have a problem with my html page. 我的html页面有问题。 In this case, why the 1st element of p attribute is consider like the 2nd element ? 在这种情况下,为什么要考虑p属性的第一个元素像第二个元素?

 /* This won't work */ #div1 p:nth-child(1) { background: #ff0000; } /* This will work */ #div2 p:nth-child(2) { background: #ff0000; } 
 <div id="div1"> <h1>This is a heading</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> </div> <div id="div2"> <h1>This is a heading</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> </div> 

http://jsfiddle.net/34bz28a0/ http://jsfiddle.net/34bz28a0/

Thank you 谢谢

You need to use nth-of-type(1) instead of nth-child() since the latter will calculate the order irrespective of the element type. 您需要使用nth-of-type(1)而不是nth-child()因为后者将计算顺序,而与元素类型无关。

p:nth-child(1) will not work because the paragraph is not the first element of the parent. p:nth-child(1)将不起作用,因为该段落不是父项的第一个元素。

 p:nth-of-type(1) { background: #ff0000; } 
 <h1>This is a heading</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> 

nth-child counts the number of child elements in relation to parent. nth-child计算相对于父级的子元素数。 If you only want to count p elements, you should use nth-of-type , ie 如果只想计算p元素,则应使用nth-of-type ,即

p:nth-of-type(1) {
    background: #ff0000;
}

Your updated JSFiddle 您更新的JSFiddle

You could use the :first-child pseudo-selector instead. 您可以改用:first-child伪选择器。

Try this: 尝试这个:

#div1 p:first-child {
  ...
}

The reason your selector didn't work when using p:nth-child(1) is that the selectors work like and IF statement with AND between rules that are not separated by some specifier (such as space character or > , ~ and so on). 您的选择器在使用p:nth-child(1)时不起作用的原因是,这些选择器的工作方式类似于IF语句, 并且在未由某些说明符分隔的规则(例如空格字符或>~等)之间使用AND )。 For example, the selector div.myClass matches <div> elements whose class is myClass . 例如,选择器div.myClass匹配类为myClass <div>元素。 The same hold for pseudo classes: div:hover matches <div> elements that are being hovered. 伪类的保持力相同: div:hover匹配要div:hover <div>元素。 In your case, the combination is p:nth-child(1) which picks <p> elements that have 0 (the formula is an+b-1 ) sibling before it in the document tree, and there are no such elements. 在您的情况下,组合为p:nth-child(1) ,它在文档树中选择前一个具有0(公式为an + b-1 )同级的<p>元素,并且没有此类元素。 With p:nth-child(2) you get the 2nd element that is also a <p> . 使用p:nth-child(2)可以得到第二个元素,它也是一个<p>

As far as solving this, the most obvious solutions are to either use :nth-child(2) to pick the 2nd sibling regardless of type, or p:nth-of-type(1) 1 (or simply p:first-of-type ) to match the 1st element of type <p> . 就解决这个问题而言,最明显的解决方案是使用:nth-child(2)来选择第二个同级兄弟,而不管类型如何,或者使用p:nth-of-type(1) 1 (或简单地使用p:first-of-type )以匹配<p>类型的第一个元素。

1 A note about nth-of-type(an+b) : this pseudo-class matches elements that are the n th of their type, regardless of a leading type selector. 1的说明nth-of-type(an+b)这个伪类匹配而不管主导类型选择属于它们的类型的n个元素。 See next snippet: 查看下一个片段:

 :nth-of-type(2) { color: red; } 
 <span>span</span> <span>span</span> <span>span</span> <b>b</b> <b>b</b> <b>b</b> 

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

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