请输入您要查询的百科知识:

 

词条 :first-child
释义

定义和用法

:first-child 伪类向元素的第一个子元素添加样式。

说明

利用 :first-child 这个伪类,只有当元素是另一个元素的第一个子元素时才能匹配。例如,p:first-child 会选择作为另外某个元素第一个子元素的所有 p 元素。一般可能认为这会选择作为段落第一个子元素的元素,但事实上并非如此,如果要选择段落的第一个子元素,应当写为 p > *:first-child。

实例

例子 1 - 匹配第一个 <p> 元素

在下面的例子中,选择器匹配属于任意元素的第一个子元素的 <p> 元素:

<style type="text/css"> p:first-child { font-weight:bold }</style> <p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p> <p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>例子 2 - 匹配所有 <p> 元素中的第一个 <em> 元素 在本例中,选择器匹配属于 <p> 元素中的第一个子元素的 <em> 元素:

<style type="text/css"> p > em:first-child { font-weight:bold }</style> <p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p> <p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>例子 3 - 匹配所有第一个子元素 <p> 元素中的所有 <em> 元素 在下面的例子中,选择器匹配属于其他元素的第一个子元素的 <p> 元素中的所有 <em>:

<style type="text/css"> p:first-child em { font-weight:bold }</style> <p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p> <p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/1 10:51:12