3

I have a couple of <div>s, and some of them have class: hidden, like this:

<div id="firstDiv" class="hidden">content</div>
<div id="secondDiv">content</div>

I know want to select the first <div> that has NOT the class: hidden. I was trying this so far in CSS: div:not(.hidden):first-child but it's not working.

How do I write the selectors correctly?

1
  • 3
    your current selector will catch the elements that don't have the .hidden class, and are first child of some element. Commented Oct 1, 2013 at 12:33

2 Answers 2

8

you can use something like this

html

<div id="firstDiv" class="hidden">content</div>
<div id="secondDiv">content</div> <!-- only this one will be selected -->
<div id="thirdDiv">content</div>

css

div:not(.hidden)
{
    background-color: red;
}
div:not(.hidden) ~ div:not(.hidden)
{
    background-color: white; /*reset everything to normal*/
}
Sign up to request clarification or add additional context in comments.

11 Comments

+1 Nice Answer, Although it's not yet fully cross compatible I would believe?
why not? it's a valid selector.
But don't worry, I am still upvoting =)!
the OP also had a :not selector in his question.. so no problem here. & thanks for the UV
@avrahamcool I am so so dumb. Even to dumb to simply copy&paste. I just had a misspelling. I guess I need a break right now ;_;
|
0

using the next sibling selector + you could write something that will always find the first div that is not hidden, after a hidden div.

div.hidden + div:not(.hidden) 
{
    background-color: #ff0;
}

http://jsfiddle.net/mbFFQ/

1 Comment

and if the not hidden is the first in the parent?

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.