
CSS 派生选择器
经由过程依据元素在其位置的上下文关联来定义款式,你能够使标记越发简约。(引荐进修:CSS视频教程)
在 CSS1 中,经由过程这类体式格局来运用划定规矩的选择器被称为上下文选择器 (contextual selectors),这是由于它们依赖于上下文关联来运用或许防止某项划定规矩。在 CSS2 中,它们称为派生选择器,然则不管你怎样称谓它们,它们的作用都是雷同的。
派生选择器许可你依据文档的上下文关联来肯定某个标签的款式。经由过程合理地运用派生选择器,我们能够使 HTML 代码变得越发整齐。
比方说,你愿望列表中的 strong 元素变成斜体字,而不是一般的粗体字,能够如许定义一个派生选择器:
li strong { font-style: italic; font-weight: normal; }
请注意标记为 <strong> 的蓝色代码的上下文关联:
<p><strong>我是粗体字,不是斜体字,由于我不在列表当中,所以这个划定规矩对我不起作用</strong></p> <ol><li><strong>我是斜体字。这是由于 strong 元素位于 li 元素内。</strong></li><li>我是一般的字体。</li> </ol>
在上面的例子中,只要 li 元素中的 strong 元素的款式为斜体字,无需为 strong 元素定义迥殊的 class 或 id,代码越发简约。
再看看下面的 CSS 划定规矩:
strong { color: red; } h2 { color: red; } h2 strong { color: blue; }
下面是它施加影响的 HTML:
<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>
以上就是CSS 派生选择器经由过程什么来定义的细致内容,更多请关注ki4网别的相干文章!