2014-11-25 45 views
0

我要申請紅底所有元素<EM>段落的第一個孩子在偶數位置選擇每em元素段落的第一個孩子。你可以幫幫我嗎?CSS在偶數位置

我想:

p:nth-child(even)>em:first-child{background-color:red;} 

但它不工作..

+0

'第一child'和'背景color'。嘗試修復拼寫。 ;) – Scimonster 2014-11-25 13:55:23

回答

2

試試這個:

p:nth-child(even) em{ font-weight: bold; background: red; } 

小提琴:http://jsfiddle.net/moonspace/uj6jwwo5/1/

+0

作品!謝謝 – 2014-11-25 13:59:47

+0

@Filomena德爾SORBO:如果你試過沒有工作,那麼無論你的第一個EM是不是第一個孩子,或者是段落內的一些其他元素的第一個孩子。如果你的段落只會最多隻能有一個他們,那麼這將工作,否則可能會匹配過多的電磁元件(即所有的人)。 – BoltClock 2014-11-25 14:52:21

+0

p:nth-​​child(偶數)em:first-child {font-weight:bold;背景:紅色; } - 將解決多個EMS。 – 2014-11-25 14:59:38

0

變化是這樣的:

p:nth-child(even)>em:nth-child(1){background-colour:red;} 

或本:

p:nth-child(even)>em:first-child{background-colour:red;} 

JSFiddle

0

此代碼應該做你想做的。

<p><em>first</em> <em>dasdas</em></p> 
<p><em>first</em> <em>dasdas</em></p> 
<p><em>first</em> <em>dasdas</em></p> 
<p><em>first</em> <em>dasdas</em></p> 
<p><em>first</em> <em>dasdas</em></p> 
<p><em>first</em> <em>dasdas</em></p> 
<p><em>first</em> <em>dasdas</em></p> 

p:nth-child(even) > em:first-child{ background-color: red;} 

http://jsfiddle.net/y5938zqm/1/

你必須有幾個錯別字:

  • firstichild代替first-child
  • background-colour,而不是background-color(沒有顏色u