nth-of-type vs nth-child

后端 未结 7 1001
梦谈多话
梦谈多话 2020-11-22 02:30

I am a bit confused about the nth-of-type pseudo class, and how this is supposed to work - especially as compared to the nth-child class.

M

相关标签:
7条回答
  • 2020-11-22 03:03

    The nth-child pseudo-class refers to the "Nth matched child element", meaning if you have a structure as follows:

    <div>
        <h1>Hello</h1>
    
        <p>Paragraph</p>
    
        <p>Target</p>
    </div>
    

    Then p:nth-child(2) will select the second child which is also a p (namely, the p with "Paragraph").
    p:nth-of-type will select the second matched p element, (namely, our Target p).

    Here's a great article on the subject by Chris Coyier @ CSS-Tricks.com


    The reason yours breaks is because type refers to the type of element (namely, div), but the first div doesn't match the rules you mentioned (.row .label), therefore the rule doesn't apply.

    The reason is, CSS is parsed right to left, which means the the browser first looks on the :nth-of-type(1), determines it searches for an element of type div, which is also the first of its type, that matches the .row element, and the first, .icon element. Then it reads that the element should have the .label class, which matches nothing of the above.

    If you want to select the first label element, you'll either need to wrap all of the labels in their own container, or simply use nth-of-type(3) (assuming there will always be 2 icons).

    Another option, would (sadly) be to use... wait for it... jQuery:

    $(function () {
        $('.row .label:first')
            .css({
                backgroundColor:"blue"
            });
    });
    
    0 讨论(0)
  • 2020-11-22 03:03

    Here is an example:

    <div>
        <div >0</div>
        <div >1</div>
        <div >2</div>
        <div >3</div>
        <div >4</div>
        <div >5</div>
    </div>
    

    this selector: div div:nth-child(1) will select the first child of the div but with another condition that child must be a div. here first child is a <div>0</div> but if the first child was a paragraph p: <p>0</p> this selector will not effect the page because there is no first child div the first child is p.

    but this selector: div div:nth-of-type(1) if the first child was a <div>0</div> will effect it, but if the first child is <p>0</p> now he will effect the second child <div>1</div> because it's the first child of his type div.

    0 讨论(0)
  • 2020-11-22 03:12

    enter image description here

    in the picture out of added 10 elements, 8 are <p> and 2 are <i>, the two shaded part elements are indicating <i> remaining eight are <p>

    the css for the page goes here:

    <style>
        * {
            padding: 0;
            margin:0;
        }
        section p {
            width: 20px;
            height: 20px;
            border:solid 1px black;
            border-radius: 15px;
            margin:20px;
            float: left;
        }
        section i {
            width: 20px;
            height: 20px;
            border:solid 1px black;
            border-radius: 15px;
            margin:20px;
            float: left;
        }
       section p:nth-child(1) {
            background-color: green; /*first-child of p in the flow*/
        }
       section i:nth-child(1) {
            background-color: red;  /*[first-child of i in the flow]NO */
        }
       section i:nth-of-type(1) {
            background-color: blue;  /*the type i of first child in the flow*/
        }
        </style>
    
    </head>
    
    <body>
    
        <section>
            <p></p>
            <p></p>
            <p></p>
            <p></p>
            <i></i>
            <p></p>
            <i></i>
            <p></p>
            <p></p>
            <p></p>
        </section>
    </body>
    

    the first green bulb indicates

     section p:nth-child(1) {
                    background-color: green; /*first-child of p in the flow*/
                }
    

    and second red bulb in the code does not work because i is not first elements in the flow

    section i:nth-child(1) {
                background-color: red;  /*[first-child of i in the flow]NO */
            }
    

    and the blue bulb in the picture indicates the first type of i in the flow

    section i:nth-of-type(1) {
                background-color: blue;  /*the type i of first child in the flow*/
            }
    
    0 讨论(0)
  • 2020-11-22 03:21

    :nth-of-type is used to select a sibling of a particular type. By type I mean a type of tag as in <li>, <img>, <div> etc.

    :nth-child is used to select children of a particular parent tag without regard to a type

    Example of :nth-of-type

    HMTL:

      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item 6</li>
        <li>Item 7</li>
        <li>Item 8</li>
        <li>Item 9</li>
        <li>Item 10</li>
        <li>Item 11</li>
        <li>Item 12</li>
        <li>Item 13</li>
        <li>Item 14</li>
        <li>Item 15</li>
        <li>Item 16</li>
      </ul>
    

    CSS:

    Notice that there is no space between the <li> tag and the pseudo-class nth-of-type.

    li:nth-of-type(odd) { background-color: #ccc; }

    Result: https://jsfiddle.net/79t7y24x/

    Example of :nth-child

    HTML:

      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item 6</li>
        <li>Item 7</li>
        <li>Item 8</li>
        <li>Item 9</li>
        <li>Item 10</li>
        <li>Item 11</li>
        <li>Item 12</li>
        <li>Item 13</li>
        <li>Item 14</li>
        <li>Item 15</li>
        <li>Item 16</li>
      </ul>
    

    CSS:

    Notice here that there is a space between the <ul> tag and the :nth-child pseudo-class

    ul :nth-child(even) { background-color: red }

    Result: https://jsfiddle.net/o3v63uo7/

    0 讨论(0)
  • 2020-11-22 03:26
    .label:nth-of-type(1)
    

    "type" here refers to the type of element. In this case, div, not to the class. This does not mean the first element which is .label, it instead means the first element of its type which also has a class of label.

    There are no elements with a class of label which are at index 1 of their type.

    0 讨论(0)
  • 2020-11-22 03:27

    Heres's a simple example which shows the difference between nth-child vs nth-of-type.

    Consider the following html

    <div>
    <p>I am first</p>
    <div>I am secong</div>
    <p>I am 3rd</p>
    </div>
    

    Using nth-of-child

    p:nth-of-child(2){
        background:red;
    }
    

    The red background will be applied to the 2nd p element inside the div.

    This happens because nth-of-child bascially means to find nth p tag(in this case 2nd p tag) inside a container

    Using nth-child

    p:nth-child(2){
        background:red;
    }
    

    Here no css is applied.

    This happens because nth-child basically means to find nth tag inside a container(in this case 2nd tag is div) and check if it is p tag

    0 讨论(0)
提交回复
热议问题