011_CSS子元素选择器

1. 与后代选择器相比, 子元素选择器(Child selectors)只能选择作为某元素的子元素。

2. 选择子元素

2.1. 如果您不希望选择任意的后代元素, 而是希望缩小范围, 只选择某个元素的子元素, 请使用子元素选择器(Child selector)。

2.2. 例如, 如果您希望选择只作为h1元素子元素的strong元素, 可以这样写:

h1 > strong {
    color: red;
}

这个规则会把第一个h1下面的两个strong元素变为红色, 但是第二个h1中的strong不受影响:

<h1>This is <strong>very</strong> <strong>very</strong> important.</h1>
<h1>This is <em>really <strong>very</strong></em> important.</h1>

2.3. 例子

2.3.1. 代码

<!DOCTYPE html>
<html>
	<head>
		<title>CSS子元素选择器</title>
		<meta charset="utf-8" />

		<style type="text/css">
			h1 > strong {
				color: red;
			}
		</style>
	</head>
	<body>
		<h1>This is <strong>very</strong> <strong>very</strong> important.</h1>
		<h1>This is <em>really <strong>very</strong></em> important.</h1>
	</body>
</html>

2.3.2. 效果图

3. 语法解释

3.1. 您应该已经注意到了, 子选择器使用了大于号(子结合符)。

3.2. 选择器h1 > strong可以解释为"选择作为h1元素子元素的所有strong元素"。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值