CSS :first-child selector

:first-child selector for class selection

<!DOCTYPE html>
<html>
<head>
<style>
.a:first-child
{
background-color:yellow;
}
</style>
</head>
<body>

<p class = 'a'>这个段落是其父元素(body)的首个子元素。</p>

<h1>欢迎访问我的主页</h1>
<p class = 'a'>这个段落不是其父元素的首个子元素。</p>

<div>
<p class = 'a'>这个段落是其父元素(div)的首个子元素。</p>
<p class = 'a'>这个段落不是其父元素的首个子元素。</p>
</div>

<p><b>注释:</b>对于 IE8 及更早版本的浏览器中的 :first-child,必须声明 <!DOCTYPE>。</p>

</body>
</html>

But once we remove the first class a under the div

<!DOCTYPE html>
<html>
<head>
<style>
.a:first-child
{
background-color:yellow;
}
</style>
</head>
<body>

<p class = 'a'>这个段落是其父元素(body)的首个子元素。</p>

<h1>欢迎访问我的主页</h1>
<p class = 'a'>这个段落不是其父元素的首个子元素。</p>

<div>
<p>这个段落是其父元素(div)的首个子元素。</p>
<p class = 'a'>这个段落不是其父元素的首个子元素。</p>
</div>

<p><b>注释:</b>对于 IE8 及更早版本的浏览器中的 :first-child,必须声明 <!DOCTYPE>。</p>

</body>
</html>

 

not yellowed

first-child requirements. the first element, and the corresponding class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325231648&siteId=291194637