2016-07-07 151 views

回答

2

可以连同:not()使用选择。

CSS伪类:not(X)是一个函数表示法服用简单的选择X作为参数。它匹配不是由参数表示的元素。

li:last-child:not(:first-child) { 
 
    color: red; 
 
}
<ul> 
 
    <li>item a</li> 
 
    <li>item b</li> 
 
    <li>item c</li> 
 
</ul> 
 

 
<ul> 
 
    <li>only item</li> 
 
</ul>

+0

完全是我以后的事。完善。 –

1

你可以留下你的CSS,因为它是和补充,覆盖它的规则,如果它是第一个孩子:

/* What should happen in the last child */ 
.className li:last-child { 
    stuff: here; 
} 

/* If there is only one child the previous one will be overwritten by this one */ 
.className li:first-child { 
    /* default values */ 
} 

或者,你可以使用一些更先进:

/* Select the last child if it is not the first child */ 
li:last-child:not(:first-child) { 
    stuff: here; 
} 

我会使用第二个,但这取决于你对CSS的熟悉程度。

更多分析

  • :last-child - 选择最后一个子
  • :not() - 如果不是
  • :first-child - 第一个子
+0

接下来的问题是我不得不通过定义第一个孩子来为LI设置样式。 –

+0

看看我的编辑。 –

1

您可以像li:last-child一种化合物选择之前预先准备* +以防止它选择的第一个孩子:

.className > * + li:last-child { 
 
    color: red; 
 
}
<ul class="className"> 
 
    <li>item a</li> 
 
    <li>item b</li> 
 
    <li>item c</li> 
 
</ul> 
 
<ul class="className"> 
 
    <li>only item</li> 
 
</ul>

相关问题