私はそれを数回試しましたが、次のことが起こるたびに::last-child
セレクターを使用すると、ドキュメントの最後の子のみが選択され、セレクターはドキュメントの最初のすべて:first-child
の子を選択します。誰かが私に理由を説明できますか?
例:
:第一子
<style>
p:first-child {background: pink;}
</style>
<body>
<p> This is the first child of the body so the background color is pink </p>
<p> This is some text in a paragraph </p>
<p> This is some text in a paragraph </p>
<div>
<p> This is the first child of the div so the background color is also pink </p>
<p> This is some text in a paragraph </p>
<p> This is some text in a paragraph </p>
</div>
</body>
:最後の子
<style>
p:last-child {background: pink;}
</style>
<body>
<p> This is some text in a paragraph </p>
<p> This is some text in a paragraph </p>
<p> I expect that this will have a pink background but it won't :( Why? </p>
<div>
<p> This is some text in a paragraph </p>
<p> This is some text in a paragraph </p>
<p> This is the last child of div and only this paragraph has a pink background </p>
</div>
</body>
ありがとう!