2016-07-07 139 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>

相關問題