2013-01-22 54 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; }