1

I am facing issues writing a slightly complex CSS selector.

I want to select a div with "class" containing 'btn-group', but not 'open'

So I have something like;

div[class*='btn-group']:not([class='open'])

Now the issue is that there are around 5-6 elements that match the above condition. But I want to select the first out of that. How do I do the same?

Would prefer doing using nth-child..

3 Answers 3

6

What about: div[class*='btn-group']:not(.open):first-of-type?

[Edit]: This trick does not work if you have <div class="btn-group open"></div> as the first child... (as explained by @Jukka below) a JS-based trick will work, tho:

$("div[class*='btn-group']").not(".open").first()
    .css({...});

    // OR add a class
    // .addClass("class");

http://jsfiddle.net/teddyrised/LdDCH/

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

2 Comments

Whether it works depends on context, but generally it doesn’t, because :first-of-type only tests whether the element is the first div child of its parent.
True enough — therefore I've added the jQuery alternative.
2

try like this

div [class*='btn-group']:not([class='open']):nth-child(1) {
    color:Red;
}

Using this you can select first child

Working Fiddle

Comments

1

You cannot. CSS selectors can’t be used that way. But if you provide a more specific HTML context (including containers for the div elements and a description of a pattern that the markup follows), there might be a way that works under some assumptions.

In particular, :nth-child and :nth-of-type only test whether the element is the *n*th child, or the *n*th child of its kind, of its parent. It does not take e.g. classes into account; the is no “nth of a class” selector.

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.