欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!

 :first-child 伪类来决意元素的第一个子元素。这个特定伪类很容易遭到误解,以是有紧要举例来阐明。思索以下标记:

 

<div>
<p>These are the necessary steps:</p>
<ul>
<li>Intert Key</li>
<li>Turn key <strong>clockwise</strong></li>
<li>Push accelerator</li>
</ul>
<p>Do <em>not</em> push the brake at the same time as the accelerator.</p>
</div>

在上面的例子中,作为第一个元素的元素蕴含第一个 p、第一个 li 与 strong 与 em 元素。

给定下列划定规矩:

 

p:first-child {font-weight: bold;}
li:first-child {text-transform:uppercase;}

第一个规定将作为某元素第一个子元素的一切 p 元素设置装备摆设为粗体。第二个规则将作为某个元素(在 HTML 中,这肯定是 ol 或 ul 元素)第一个子元素的悉数 li 元素酿成大写。

请会面该链接,来查抄这个 :first-child 实例的成就。

暗示:最思空见贯的纰谬是以为 p:first-child 之类的决意器会决定 p 元素的第一个子元素。

表白:必须声名 <!DOCTYPE>,如许 :first-child 技巧在 IE 中见效。

为了使您更透彻天文解 :first-child 伪类,咱们其他供应了 3 个例子:

例子 1 - 成家第一个 <p> 元素

不才面的例子中,决定器受室作为任何元素的第一个子元素的 p 元素:

 

<html>
<head>
<style type="text/css">
p:first-child {
  color: red;
  } 
</style>
</head>

<body>
<p>some text</p>
<p>some text</p>
</body>
</html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/css-tool/t52314.shtml