派生选择器允许你根据元素在其位置的上下文关系来定义某个标签的样式。
在 CSS1 中,通过这种方式来应用规则的选择器被称为上下文选择器 (contextual selectors),这是由于它们依赖于上下文关系来应用或者避免某项规则。在 CSS2 中,它们称为派生选择器,但是无论你如何称呼它们,它们的作用都是相同的。
实例
li strong {font-style: italic; font-weight: normal;}注释:上面的代码中的派生选择器将列表中的 strong 元素变为斜体字,而不是通常的粗体字。
<p><strong>我是粗体字,不是斜体字,因为我不在列表当中,所以这个规则对我不起作用</strong></p> <ol> <li><strong>我是斜体字。这是因为 strong 元素位于 li 元素内。</strong></li> <li>我是正常的字体。</li> </ol>提示:注意标记为 <strong> 的代码的上下文关系,只有 li 元素中的 strong 元素的样式为斜体字。
strong {color: red;} h2 {color: red;} h2 strong {color: blue;}
<p>The strongly emphasized word in this paragraph is<strong>red</strong>.</p> <h2>This subhead is also red.</h2> <h2>The strongly emphasized word in this subhead is<strong>blue</strong>.</h2>
评论