伪类不起作用

时间:2017-12-23 07:42:51

标签: javascript html css

我是html的新手。当我在" style"之间放置任何标签时,为什么代码不起作用?和" p"标签



p:first-child {
  color: red;
}

<h1>example</h1>
<!-- problem is here -->

<p>Hello venus</p>
<p>hello world</p>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

您需要使用first-of-type

&#13;
&#13;
p:first-of-type{
color:red;
}
&#13;
<h1>example</h1> <!-- problem is here -->

<p>Hello venus</p>
<p>hello world</p>
&#13;
&#13;
&#13;

First-child只是第一个孩子,而不是第一个孩子。

&#13;
&#13;
: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;
&#13;
&#13;

答案 1 :(得分:1)

p:first-child定位于身体的第一个孩子。您可能需要p:first-of-type

&#13;
&#13;
p:first-of-type {
  color: red;
}
&#13;
<h1>example</h1>
<!-- problem is here -->

<p>Hello venus</p>
<p>hello world</p>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

最好使用:first-of-type选择器

&#13;
&#13;
   
&#13;
p:first-of-type{
    color:red;
 }
&#13;
<h1>example</h1> <!-- problem is here -->

<p>Hello venus</p>
<p>hello world</p>
&#13;
&#13;
&#13;