---
title: "第2章 选择器  =>选择子元素 P54"
author: "conkeyn"
date: "2009-04-26T10:37:52Z"
tags: ["CSS", "HTML", "XHTML"]
slug: "2-p54"
draft: false
lastmod: "2009-04-26T10:54:16Z"
id: 374989
url: "https://conkeyn.iteye.com/blog/374989"
---

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>选择子元素</title>
<style type="text/css">
h1 > strong {
	color:red;
}
</style>
</head>

<body>
<p>在某些情况下,可能并不相选择一个任意的后代元素,而是希望缩小范围，只选择另一个元素的子元素。例如，可能想选择只作为一个h1元素的子元素（而不是后代元素）的strong元素。为此可以使用子结合符，即大于号（&gt;）</p>
<p>h1 &gt; strong {color:red;}</p>
<h1>This is <strong>very</strong> important.</h1>
<h1>This is <strong>very</strong> important</h1>
<h1>This is <em>really <strong>very</strong> important</em></h1>
<p>选择器h1 &gt; strong 可以解释为“选择作为h1元素的子元素的所有strong元素”。子结合符两边可以有空白符，这是可选的，因此h1 &gt; strong 、h1&gt; strong和h1&gt;strong都是一样的。</p>
</body>
</html>
```

 

**显示效果:**

![](/upload/attachment/97887/3817d213-4cf6-337c-9b80-a41556391b2b.bmp)

 

**详细说明:**

![](/upload/attachment/97889/ea2c5f0d-c24c-359d-bf2d-1455ae10e566.bmp)

例如，a元素是strong元素的父元素，但是它又是p元素的子元素。可以用p &gt; a和a &gt; strong选择器来匹配这个片段中的元素，但是p &gt; strong不行，因为strong是p的后代而不是其子元素。

还可以在同一个选择器中结合使用后代选择器和子选择器。因此，table.summay td &gt; p 会选择作为一个td元素子元素的所有p元素，这个td元素本身从table元素继承，该table元素有一个包含summary的class属性