2013-01-22 56 views
0

我有一個問題,將CSS應用於具有特定類的li元素。我想改變那個李的背景顏色,但我找不到一種方法來做到這一點。我不確定這是否會改變'父',因爲我知道css無法控制父母。CSS將屬性應用於具有特定類的li

CSS

.navi ul li { padding:20px; background-color: #00; } 


.navi ul li + .current_location { background-color: #FFF; } 

or 

.navi ul li .current_location { background-color: #FFF; } 

//will only change the background color of letters inside, no the li element. 

current_location + ul li { background-color: #FFF; } 

// will only apply to the children of the li which has class .current_location. 



I want to change the li background color where it has class .current_location; 

HTML

<nav class="navi"> 
      <ul> 
       <li> 
       <a>General Config</a> 
       </li> 
       <li class="current_location"> 
       <a>Menu</a> 
       </li> 
       <li> 
       <a>User Level</a> 
       </li> 
       <li> 
       <a>User</a> 
       </li> 
       <li> 
       <a>Tag</a> 
       </li> 
       <li> 
       <a>Log</a> 
       </li> 
      </ul> 
      </nav> 

非常感謝你的建議,

回答

3

.navi ul li + .current_location應該.navi ul li.current_location

Fiddle

+選擇器被用於該元素之後選擇next元件。
例如:div+p將選擇所有p元素,這些元素都在div元素之後。 example

+0

非常感謝你的例子。有用。但是,我的代碼怎麼會只適用於信背景而不是李?請指教 – Till

+1

這可能意味着還有其他干擾css風格,你可以檢查'li'元素和共享樣式 –

1

.current_location & 之間刪除的空間。這樣寫:

.navi ul li.current_location { background-color: #FFF; } 
+0

非常感謝,但它只適用於不是li的信,我說在我的問題 – Till

+0

它是對我不清楚。請你可以創建一個更好理解的例子。 – sandeep

1

.navi ul li.current_location {background-color:#FFFFFF; }