Forums

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

Home Forums CSS :last-child won’t target

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #40914
    alexmccabe
    Participant

    Normally I don’t have any issues with CSS, however this one has me completely stumped!

    If you take a look at [this pen](http://codepen.io/alexmccabe/pen/Esurd “this pen”), you can see that there are 5 . hr div classes. In the CSS the last one should be hidden, but it won’t. I set the colour to red just to see if it was being targeted at all. And it is not.

    Anyone have any ideas? Would be much appreciated. Causing me actual issues in my project. I can target it using nth-child(n) but I don’t know how many children there will be. Bloody odd.

    first-child doesn’t work either.

    #115065
    Watson90
    Member

    Hi @alexmccabe

    You can use this;

    .hr:last-of-type

    like so – http://codepen.io/anon/pen/lIxqD

    #115066
    Paulie_D
    Member

    You’re using the the wrong pseudo class

    .hr:last-of-type

    EDIT…Aw, SNAP

    Browser support ain’t great though.

    #115069
    Watson90
    Member

    I think last child only works if it has a parent element such as an unordered list with 5 list items, if you wanted to target the last item, you would then use last-child.

    #115070
    Paulie_D
    Member

    Because each item is not a child of anything.

    EDIT: FFS….SNAP again.

    #115071
    Paulie_D
    Member

    OK….here’s something…why create a div with that class instead of just styling your ‘hr’ tag?

    CSS Tricks does.

    See:


    #115073
    Watson90
    Member

    Lol :)

    Yeah I’d agree with @Paulie_D on this one, its just much more simple and a lot less markup (which is always a good thing)

    #115076
    Watson90
    Member

    Shouldn’t your css be as simple as;

    border: 0px none;
    height: 1px;
    background: -moz-linear-gradient(left center , transparent, rgba(0, 0, 0, 0.5), transparent) repeat scroll 0% 0% transparent;
    margin: 35px 0px 33px;

    just like Chris uses…

    #115079
    Paulie_D
    Member

    I think he was just using -moz as an example.

    It’s understood that we would target respective browsers using the correct prefix.

    …and who gives a flying ‘fig’ about IE? LOL

    #115082
    Paulie_D
    Member
    #115084
    Paulie_D
    Member

    As for why your previous version failed, I am having no luck.

    It could be a specificity issue but I can’t get it to function ‘as expected’ regardless of how detailed I get.

    #115091
    wolfcry911
    Participant

    it fails because it’s not the last child

    #115092
    Paulie_D
    Member

    @wolfcry911… yeah, we’re past that and on to why last-of-type isn’t working here.

    http://codepen.io/alexmccabe/pen/Esurd

    #115098
    wolfcry911
    Participant

    well the pen he links to asking why it still fails uses last-child

    last-of-type is going to fail as well, because type refers to the element (not a specific classed element).

    #115101
    Paulie_D
    Member

    >last-of-type is going to fail as well, because type refers to the element (not a specific classed element).

    Boom…and there’s the answer we were looking for. :)

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