Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS nth-child or nth-of-type or something else?

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #272649
    drfingerless
    Participant

    I’ve got some HTML that looks like this:

    <div class="chicken"></div>
    <div class="chicken"></div>
    <div class="egg"></div>
    <div class="egg"></div>
    <div class="chicken"></div>
    <div class="egg"></div>
    

    How do I target the third chicken classed div specifically?

    #272650
    drfingerless
    Participant

    I put my html inside the three back-ticks but it seems to have disappeared. Some advice on posting?

    #272651
    Shikkediel
    Participant

    Test…

    Nope, no advice to be given I think. Used to work that way. It seems to be filtering out all HTML.

    #272652
    Shikkediel
    Participant

    I don’t think there is a “nth instance of a class” selector in CSS by the way.

    #272657
    chris_2080
    Participant

    neither nor..

    try something like this

    $(".chicken").filter(function(index, element){
        return index % 3 == 2;
    }).addClass("third");
    
    #272668
    drfingerless
    Participant

    Hey chris_2080, thanks for this however I am not a coder as such which is why I was looking for a CSS solution. According to Shikkediel there may not be one… Your (jQuery?) solution may work but I will need to get a little help in working out how to use it.

    #272735
    chris_2080
    Participant

    ok, jQuery is not needed, put this script into your html at the end of body between script tag

    var thirdChicken = document.getElementsByClassName("chicken")[2];
      /* do something with thirdChicken */ 
    

    what is your goal?

    #272736
    Paulie_D
    Member

    I don’t think there is a “nth instance of a class” selector in CSS by the way.

    This is correct.

    https://stackoverflow.com/questions/5545649/can-i-combine-nth-child-or-nth-of-type-with-an-arbitrary-selector

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘CSS’ is closed to new topics and replies.