2013-03-22 31 views
0

http://jsfiddle.net/RKwHk/ 我该如何改变我的选择器,以便它只选择第一段?由于Make:first-child忽略嵌套结构?

<div class="cont"> 
    <span> 
     <p>first</p> 
    </span> 
    <span> 
     <p>second</p> 
    </span> 
    <span> 
     <p>thrid</p> 
    </span> 
</div> 

.cont p { 
    color: green; 
} 
.cont p:first-child { 
    color: red; 
} 
+0

选择跨度的第一个孩子不段 – 2013-03-22 12:32:39

+2

为什么你在'span'元素'p'元素? – BoltClock 2013-03-22 18:15:47

回答

1

使用这种方式:

.cont p { 
    color: green; 
} 
.cont span:first-child p { 
    color: red; 
} 
0

我建议:

.cont div:first-of-type p, /* or the following */ 
.cont div:first-child p { 
    color: red; 
} 

JS Fiddle demo

请注意,我已将span元素更改为div元素,因为p不应包含在内联元素中。

0

将100%的工作用这个

.cont p { 
    color: green; 
} 

.cont span:first-child p{ 
    color:Red; 
}