CSS 派生选择器
派生选择器
通过依据元素在其位置的上下文关系来定义样式,你可以使标记更加简洁。
在 CSS1 中,通过这种方式来应用规则的选择器被称为上下文选择器 (contextual selectors),这是由于它们依赖于上下文关系来应用或者避免某项规则。在 CSS2 中,它们称为派生选择器,但是无论你如何称呼它们,它们的作用都是相同的。
派生选择器允许你根据文档的上下文关系来确定某个标签的样式。通过合理地使用派生选择器,我们可以使 HTML 代码变得更加整洁。
比方说,你希望列表中的 strong 元素变为斜体字,而不是通常的粗体字,可以这样定义一个派生选择器:
1 2 3 4 | < code >li strong</ code > { font-style : italic ; font-weight : normal ; } |
1 2 3 4 5 6 | <p><strong>我是粗体字,不是斜体字,因为我不在列表当中,所以这个规则对我不起作用</strong></p> <ol> < code ><li><strong></ code >我是斜体字。这是因为 strong 元素位于 li 元素内。< code ></strong></li></ code > <li>我是正常的字体。</li> </ol> |
在上面的例子中,只有 li 元素中的 strong 元素的样式为斜体字,无需为 strong 元素定义特别的 class 或 id,代码更加简洁。
再看看下面的 CSS 规则:
1 2 3 4 5 6 7 8 9 10 11 | strong { color : red ; } h 2 { color : red ; } < code >h 2 strong</ code > { color : blue ; } |
1 2 3 | <p>The strongly emphasized word in this paragraph is<strong> red </strong>.</p> <h 2 >This subhead is also red .</h 2 > < code ><h 2 ></ code >The strongly emphasized word in this subhead is< code ><strong></ code > blue < code ></strong></ code >.< code ></h 2 ></ code > |