Why is nth-child selector not working?

前端 未结 4 1816
醉梦人生
醉梦人生 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: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 
                  .
                  .
                  .
    

提交回复
热议问题