CSS选择偶数位置中段落的每个em元素first-child

时间:2014-11-25 13:50:18

标签: css css-selectors

我必须将红色背景应用于偶数位置的所有元素<EM>段落的第一个子元素。你能帮帮我吗?

我试过了:

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

但它不起作用..

3 个答案:

答案 0 :(得分:2)

试试这个:

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

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

答案 1 :(得分:0)

像这样改变:

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

或者这个:

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

JSFiddle

答案 2 :(得分: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