- This topic is empty.
-
AuthorPosts
-
October 31, 2013 at 9:43 am #154738noahgelmanParticipant
<ul> <li class="apple"></li> <li class="banana"></li> <li class="banana"></li> <li class="banana"></li> <li class="apple"></li> <li class="banana"></li> <li class="banana"></li> <li class="banana"></li> </ul>
I want to select the first banana after every apple but I can’t seem to do it.
I’ve tried using ul li.banana:nth-of-type(3n+1) to select it but for some reason it keeps counting the apple li elements and it’s throwing it off.
I also can’t use ul li.banana:nth-of-type(4n+2) since this needs to work for cases without apples in it.
note: I can’t use the “~” selector. I need to stay within the “nth” rules.
October 31, 2013 at 9:49 am #154740SenffParticipantUnless the markup is always the same, you can’t do that with the rules you’ve set. You’ll probably going to have to use JS/jQuery.
October 31, 2013 at 9:52 am #154741Paulie_DMemberIndeed,
nth-child
&nth-of-type
etc. pseudo-classes only work on elements…not classes.October 31, 2013 at 9:53 am #154742noahgelmanParticipant@Senff I’m hoping thats not the case. This takes like 2 seconds to target with jQuery. I’m not sure why CSS has to make things so difficult, lol
October 31, 2013 at 9:58 am #154744Paulie_DMemberI’m not sure why CSS has to make things so difficult, lol
I’m sure
nth-of-class
is a major requirement for many but I seem to recall that the working group mentioned that there were major issues with inheritance and specificity in implementing it.Much like the long sought ‘parent’ selector.
In the end…that’s why JQ is such a boon and widely used.
October 31, 2013 at 10:01 am #154745noahgelmanParticipantOh wow. I never knew you couldn’t combine
nth-child
&nth-of-type
with a class selector. That’s the cause of the issue then. I was trying to figure out why it wasn’t working. It’s just ignoring the banana class and just selecting all list items.This isn’t doable with the rules in place.
Thanks a lot for the help guys.
October 31, 2013 at 10:14 am #154749Paulie_DMemberActually based on this specific requirement
I want to select the first banana after every apple
You can do this will CSS..
.apple + .banana { background:red; }
http://codepen.io/Paulie-D/pen/6fae0e9204a8bad638a83df24873b44c
Sometimes we overlook the obvious options… :)
-
AuthorPosts
- The forum ‘CSS’ is closed to new topics and replies.