Selecting a nested element with :nth-child

后端 未结 2 1013
你的背包
你的背包 2021-01-22 23:02

I\'m having problems targeting nested elements with the :nth-child() selector. How can I target the second and third .service?

相关标签:
2条回答
  • 2021-01-22 23:29

    Each .service is the only child of its parent .large-4.columns, all three of which are children of the same .services.row element. Your :nth-child() should therefore be on the .large-4.columns elements:

    .services > .columns:nth-child(n+2) > .service
    

    Or you can use a sibling selector instead of :nth-child() if you will only have three of these columns at most:

    .services > .columns ~ .columns > .service
    
    0 讨论(0)
  • 2021-01-22 23:40

    If you want to target anything other than the first element, use :not, like so:

    div {
      display: inline-block;
      background: black;
      color: white;
      height: 100px;
      width: 100px;
      float: left;
      text-align:center;
      line-height:100px;
    }
    div:not(:first-child) {
      border-left: 2px solid red;
    }
    <section>
      <div>One</div>
      <div>Two</div>
      <div>Three</div>
    </section>

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