除了第一个孩子和最后一个孩子之外的CSS选择器

Dan*_*iel 95 html css css-selectors

我正在建立一个非常先进的网站.我的问题:是否可以选择除了:first-child和之外的所有其他孩子:last-child?我知道有一个:not()选择器,但它不适用于不在括号中的多个选择器.这就是我所拥有的:

#navigation ul li:not(:first-child, :last-child) {
    background: url(images/UISegmentBarButtonmiddle@2x.png);
    background-size: contain;
}
Run Code Online (Sandbox Code Playgroud)

Sal*_*bas 237

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

  • rofl,我什至在谷歌搜索之前尝试过这个“not()”的愚蠢:P天才 (2认同)
  • 这是错误的:`not (A and B)` 不等于`(not A) and (not B)`,而是`(not A) or (not B)`。这是行不通的。 (2认同)
  • 不,萨尔曼·阿巴斯所说的是对的。您正在考虑逗号;#navigation ul li:not(:first-child),#navigation ul li:not(:last-child) (2认同)

ani*_*son 19

当然它会工作,你只需要使用两个'不'选择器.

#navigation ul li:not(:first-child):not(:last-child) {
Run Code Online (Sandbox Code Playgroud)

它将在第一个之后继续下线,说"不是第一个孩子"和"不是最后一个孩子".