151

I am making a very advanced website. My question: Is it possible to select all the other children except for the :first-child and the :last-child? I know there is a :not() selector but it doesn't work with more than one not in the parentheses. This is what I have:

#navigation ul li:not(:first-child, :last-child) {
    background: url(images/UISegmentBarButtonmiddle@2x.png);
    background-size: contain;
}
5
  • You are mighty brave to use this. Do take note that it will only work on modern browsers, and it will definitely not work on the anti-browser that everyone is using.
    – rid
    Oct 16, 2011 at 23:16
  • 2
    @Radu: "I am making a very advanced website" No surprise there...
    – BoltClock
    Oct 16, 2011 at 23:18
  • 1
    @BoltClock, right, just saying... Word of warning. But anyway, if the target is the iOS Safari as the name of the image suggests, then it should be safe.
    – rid
    Oct 16, 2011 at 23:18
  • 1
    Now I see why this question looks so familiar... stackoverflow.com/questions/7403129/combining-not-selectors
    – BoltClock
    Oct 16, 2011 at 23:23
  • "anti-browser"? Is that the new Google Ultron I keep hearing about? Aug 4, 2014 at 17:24

2 Answers 2

348

Try:

#navigation ul li:not(:first-child):not(:last-child) {
  ...
}
3
  • 2
    rofl, the stupidity of me not() even trying this before googling :P genius
    – SidOfc
    May 19, 2015 at 12:41
  • 3
    That's wrong: not (A and B) is not equal to (not A) and (not B), that would rather be (not A) or (not B). It does not work.
    – Hibou57
    Feb 9, 2016 at 23:49
  • 3
    Nope what Salman Abbas said is right. You are thinking of the comma; #navigation ul li:not(:first-child), #navigation ul li:not(:last-child)
    – user847074
    Oct 31, 2016 at 20:01
27

Sure it will work, you just have to use two 'not' selectors.

#navigation ul li:not(:first-child):not(:last-child) {

It will continue down the line after the first one, saying "not the first child" and "not the last child".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.