CSS first-child selector doesn't select first element
Why if I put an element before the "p" lines, I don't see the first line yellow? Should p:first-child
select the very first p
and not just a very first tag?
<style type="text/css">
p:first-child
{
开发者_StackOverflow社区 background:yellow;
}
</style>
<i></i>
<p>I am a strong man. I am a strong man.</p>
<p>I am a strong man. I am a strong man.</p>
<p>I am a strong man. I am a strong man.</p>
:first-child
does not care about the type. By adding <i></i>
to your code, i
becomes the first child (assuming <style>
is within <head>
and the rest is in <body>
, of course). Your selector wants to match p
, but since p
is not the first child anymore, your style can't be applied.
If you want to filter by type, use the CSS3 :first-of-type
pseudo-class:
p:first-of-type
{
background:yellow;
}
精彩评论