first-child(转自:http://www.aa25.cn/css2/pseudo_class_first-c677.shtml)

定义

: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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值