我是html的新手。当我在" style"之间放置任何标签时,为什么代码不起作用?和" p"标签
p:first-child {
color: red;
}

<h1>example</h1>
<!-- problem is here -->
<p>Hello venus</p>
<p>hello world</p>
&#13;
答案 0 :(得分:3)
您需要使用first-of-type
。
p:first-of-type{
color:red;
}
&#13;
<h1>example</h1> <!-- problem is here -->
<p>Hello venus</p>
<p>hello world</p>
&#13;
First-child
只是第一个孩子,而不是第一个孩子。
:first-child {color:red;}
div h1:first-child {color: green;}
div p:first-child {color: blue} /* match nothing */
&#13;
<h1>example</h1> <!-- problem is here -->
<p>Hello venus</p>
<p>hello world</p>
<div>
<h1>example</h1> <!-- problem is here -->
<p>Hello venus</p>
<p>hello world</p>
</div>
&#13;
答案 1 :(得分:1)
p:first-child
定位于身体的第一个孩子。您可能需要p:first-of-type
p:first-of-type {
color: red;
}
&#13;
<h1>example</h1>
<!-- problem is here -->
<p>Hello venus</p>
<p>hello world</p>
&#13;
答案 2 :(得分:1)
最好使用:first-of-type
选择器
&#13;
p:first-of-type{
color:red;
}
&#13;
<h1>example</h1> <!-- problem is here -->
<p>Hello venus</p>
<p>hello world</p>
&#13;