3

I have the following divs:

<div id="container">
    <div class="different_class"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>

I want to apply a border to all the children divs with (.child), except the first one. so in the end all the children divs except (.different_class) and (.child:eq(0)) (I don't want to use nth-child as in the real problem I might not have .different_class div) will have the border.

I think the solution might be to use :not() selector, but I'm not quite sure how. Thanks'

2
  • :eq() is not a CSS selector by the way. It's part of jQuery. Commented Aug 18, 2012 at 2:01
  • Thanks, I have been very confused with that. Commented Aug 18, 2012 at 7:59

3 Answers 3

6

try this http://jsfiddle.net/5XuE3/

#container div.child + div.child
{
border: solid 1px black;
}
Sign up to request clarification or add additional context in comments.

Comments

2
.child { some css }
.child:first-child { other styling }

EDIT

Why does not work? Check this jsfiddle.

3 Comments

This doesn't work, "so in the end all the children divs except (.different_class) and (.child:eq(0))" thanks anyway
@Alvaro please see the update. Although I see now that you do not ask for the nth-child, it still works. Only tested with Chrome though.
There is no .different_class in your fiddle!
1

+ selector will work in this case because all elements that have child class are together but it would not work if another different element (or with another class) will be between them.

This happens because + selector will match all elements that are placed immediately after another div.child.

Example:

#container div.child + div.child{
  border: 1px solid;
}
<div id="container">
    <div class="different_class">Test</div>
    <div class="child">Test</div>
    <div class="child">Test</div>
    <div class="different_class">Test</div>
    <div class="child">Test</div>
    <div class="child">Test</div>
</div>

To avoid that, we can use ~ selector, that will match all elements that are preceded by div.child, not necessary that it will be immediately after it. So it will not match the first div with child class but will match the rest of divs with child class without take in consideration the rest of elements.

Example:

#container div.child ~ div.child{
  border: 1px solid;
}
<div id="container">
    <div class="different_class">Test</div>
    <div class="child">Test</div>
    <div class="child">Test</div>
    <div class="child">Test</div>
    <div class="child">Test</div>
</div>

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.