Why is nth-child selector not working?

前端 未结 4 1790
醉梦人生
醉梦人生 2020-11-29 11:09

I am using the nth-child selector to add background images for different social icons. However, all icons are appearing the same. What am I doing wrong?

相关标签:
4条回答
  • 2020-11-29 11:38

    On my test page it was because the <hr /> tag will break the count on the selector logic.

    0 讨论(0)
  • 2020-11-29 11:39

    Try this!

    <div id="social-links">
      <a href=""><div class="social-logo"></div></a>
      <a href=""><div class="social-logo"></div></a>
      <a href=""><div class="social-logo"></div></a>
      <a href=""><div class="social-logo"></div></a>
    </div>
    

    CSS

    .social-logo {
        display: inline-block;
        width: 24px;
        height: 24px;
        transition: background-image .2s;
    }
    
    #social-links a:nth-child(1) .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-linkedin.svg');
    }
    
    #social-links a:nth-child(1):hover .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-linkedin-copy.svg');
    }
    
    #social-links a:nth-child(2) .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-dribbble.svg');
    }
    
    #social-links a:nth-child(2):hover .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-dribbble-copy.svg');
    }
    
    #social-links a:nth-child(3) .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-email.svg');
    }
    
    #social-links a:nth-child(3):hover .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-email-copy.svg');
    }
    
    #social-links a:nth-child(4) .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-insta.svg');
    }
    
    #social-links a:nth-child(4):hover .social-logo {
        background-image: url('https://mysql-raigovind93.c9users.io/Cally%20Dai//img/footer/logo-insta-copy.svg');
    }
    

    Demo live - https://jsfiddle.net/g59wa8uf/

    0 讨论(0)
  • 2020-11-29 11:47

    The nth-child selector counts siblings (i.e., elements having the same parent).

    In your HTML structure, div.social-logo is always the first, last and only child of a. So nth-child has only one element to count.

    However, there are multiple anchor elements, all of which are siblings (children of #social-links), so nth-child can target each one.

    #social-links a:nth-child(1) div 
    #social-links a:nth-child(2) div 
    #social-links a:nth-child(3) div 
                  .
                  .
                  .
    
    0 讨论(0)
  • 2020-11-29 11:58

    try adding space between space before ':' example:-

    tr :nth-child(2)
    {
    text-align: right;
    }
    
    0 讨论(0)
提交回复
热议问题