简体   繁体   English

类型n不能按预期工作

[英]nth-of-type not working as expected

I've got some simple markup 我有一些简单的标记

<div class='1'>
    <div class='11'></div>
    <div class='12'>
        <div class='121'></div>
    </div>
</div>

And I'm using the following CSS to try and target the .11 and .12 我正在使用以下CSS尝试定位.11和.12

div div:nth-of-type(1) { // some rules for the first div }
div div:nth-of-type(2) { // some rules for the second div }

However, it seems that div .121 is being targeted by the first rule. 但是,第一个规则似乎将div .121作为目标。 Is there a reason for this? 是否有一个原因?

How would i target .11 in the first rule and .12 in the second only? 我将如何在第一个规则中定位.11和仅在第二个规则中定位.12?

It's because div.121 is the first div -type child of div.12 . 这是因为div.121div.121的第一个div类型的子div.12

To exclude it, qualify your div.1 's class and add the child combinator > , which will only match children of div.1 rather than any element inside it ( this answer illustrates another example): 要排除它,请限定div.1的类并添加child combinator > ,它将仅匹配div.1孩子,而不匹配其中的任何元素( 此答案说明了另一个示例):

div.1 > div:nth-of-type(1)
div.1 > div:nth-of-type(2)

因为div.121 也是 div的子级的第一个div,因此被同一选择器匹配。

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

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