0

I am looking to do something similar to jQuery's eq() selector to target elements on a page based on their order. :nth-child() is exactly what I need except I want to target elements are are not children. For example:

<div></div>
<div></div>

If I wanted to target the 2nd div above, what can I use in css (without actually giving it an id or class)?

1
  • 1
    Your example is pretty poor because both divs are children of an implicit body. Commented Oct 13, 2011 at 3:58

2 Answers 2

2

You cannot pick the nth element out of a set of arbitrary, unrelated matches with CSS selectors.

You can only do this in jQuery using $('div:eq(1)').

Sign up to request clarification or add additional context in comments.

3 Comments

Ooo nth-match ... interesting. Have an upboat.
@Russell: I have a dreadful feeling that I should be keeping CSS4 stuff under wraps instead of dropping hints like this here and there :S
@Russell: I reviewed the CSS4 spec, and it doesn't seem that :nth-match() will do the same thing as jQuery's :eq() because :nth-match() behaves like :nth-child() in that it limits itself to sibling elements :/ Here's the link again: dev.w3.org/csswg/selectors4/#nth-match
0

If those are the only to adjacent div elements, then you could use div + div. Otherwise that would match all divs starting from the 2nd one.

In that case, you'd also have to reset whatever CSS you've applied by specifying an div + div + div rule. That would apply to all following divs starting from the 3rd.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.