155

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;
}
Volker E.
  • 5,911
  • 11
  • 47
  • 64
Daniel
  • 1,555
  • 2
  • 9
  • 4
  • 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 '11 at 23:16
  • 2
    @Radu: "I am making a very advanced website" No surprise there... – BoltClock Oct 16 '11 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 '11 at 23:18
  • 1
    Now I see why this question looks so familiar... http://stackoverflow.com/questions/7403129/combining-not-selectors – BoltClock Oct 16 '11 at 23:23
  • "anti-browser"? Is that the new Google Ultron I keep hearing about? – Jonathan Dumaine Aug 04 '14 at 17:24

2 Answers2

356

Try:

#navigation ul li:not(:first-child):not(:last-child) {
  ...
}
David
  • 4,336
  • 2
  • 23
  • 31
Samuel Katz
  • 24,066
  • 8
  • 71
  • 57
  • 2
    rofl, the stupidity of me `not()` even trying this before googling :P genius – SidOfc May 19 '15 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 09 '16 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 '16 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".

random_user_name
  • 25,694
  • 7
  • 76
  • 115
animuson
  • 53,861
  • 28
  • 137
  • 147