2017-12-23 19 views
-2

私は初心者のhtmlです。 「スタイル」タグと「p」タグの間にタグを置くと、コードが機能しないのはなぜですか?擬似クラスが動作していません

p:first-child { 
 
    color: red; 
 
}
<h1>example</h1> 
 
<!-- problem is here --> 
 

 
<p>Hello venus</p> 
 
<p>hello world</p>

+0

を使用することをお勧めします。最初の子はここで 'h1'です。 – panther

+0

あなたは既にあなたのコードで問題を指摘しています... –

答えて

3

必要があるかもしれません。

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

 
<p>Hello venus</p> 
 
<p>hello world</p>

First-child段落の最初の、ちょうど最初の子ではありません。

:first-child {color:red;} 
 

 
div h1:first-child {color: green;} 
 
div p:first-child {color: blue} /* match nothing */
<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>

1

p:first-child体の最初の子を狙っています。あなたは、あなたがfirst-of-typeを使用する必要がp:first-of-type

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

 
<p>Hello venus</p> 
 
<p>hello world</p>

1

それは、任意の要素の最初の子である何の要素 `p`はありません:first-of-typeセレクタ

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

 
<p>Hello venus</p> 
 
<p>hello world</p>

関連する問題