CSS3伪类与SVG可渲染元素的兼容性是什么?

我一直在学习SVG,并发现了 this informative article.作者说

Most CSS selectors can be used to select SVG elements. In addition to the general type,class and ID selectors,SVGs can be styled using CSS2’s dynamic pseudo-classes (:hover,:active and :focus) and pseudo-classes (:first-child,:visited,:link and :lang. The remaining CSS2 pseudo-classes,including those having to do with generated content (such as ::before and ::after),are not part of the SVG language definition and,hence,have no effect on the style of SVGs.

这篇作者在网上有很多文章,看起来非常有见识.然而,“剩余的CSS2伪类….”对SVG的风格没有影响“这个说法让人想到了CSS3伪类.以Codepen生成的这个例子(FF为浏览器).

<svg width="220" height="220" xmlns="http://www.w3.org/2000/svg">
    <rect x="10" y="10" width="100" height="100" />
    <rect x="110" y="110" width="100" height="100" />
</svg>

<style>
    svg { border: 3px dashed #999 }
    svg > rect:hover { fill: green }
    rect:nth-child(even) { fill:red }
</style>

CSS3:第n个孩子伪类在这里工作得很好(填充第二个矩形红色).另一个例子:用另一个CSS3伪类选择符替换上面的第n个子规则:a:not rule(其余的都保持不变):

rect:not([x="110"]) { fill:red } // fills the 1st rectangle red

我找到了this reference,但并没有帮助我.

CSS3伪类与SVG元素的兼容性是什么?

注意:我假设这些伪类规则只适用于SVG renderable elements.

解决方法

这是一个 pen,它通过将填充或笔触属性应用于SVG可渲染元素来演示CSSG伪类选择器(由@andrewli概述)在SVG上的应用.

我将不同的SVG元素定义在两个不同的组(两个父元素,笔中显示为两列),以适应所有选择不同种类的子类的伪类:

<svg width="450" height="300">
  <g transform="translate(5,5)">
    <rect x="0" y="0" width="25" height="25" />
    <rect x="0" y="40" width="25" height="25" />
    <rect x="0" y="80" width="25" height="25" />
    <circle cx="15" cy="132" r="13.5"/>
    <circle cx="15" cy="170" r="13.5"/>
    <polygon points="2,200 28,200 14,225"/>
    <rect x="0" y="240" width="25" height="25" />
  </g>
  <g transform="translate(5,5)">
    <rect x="220" y="0" width="25" height="25" />
  </g>
  <g transform="translate(5,5)" font-family="Verdana" font-size="16" fill="#444">
    <text x="40" y="20" >:root</text>
    <text x="40" y="60">:nth-child(2)</text>
    <text x="40" y="100">:nth-of-type(3)</text>
    <text x="40" y="140">:first-of-type</text>
    <text x="40" y="180">:nth-last-of-type(1)</text>
    <text x="40" y="220">:only-of-type,:last-of-type</text>
    <text x="40" y="260">:nth-last-child(1),:last-child</text>
    <text x="260" y="20">:only-child,:last-child</text>
  </g>
</svg>

有趣的发现:

>:root似乎工作.如预期的那样,它将应用于文档中的所有元素,包括svg元素.
>因为:空被应用于所有void svg元素(< rect>,< circle>,< ellipse>,< polygon>等),它是用于定位SVG形状元素的便利选择器.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


css的bordercolor属性怎么使用
CSS中contain属性的作用和语法
深入学习响应式布局框架:适合初学者到专家的详尽指南
CSS3选择器优先级规则
margin-top用法
选择最适合您的响应式布局框架:综合评估不同工具
使用CSS中的content属性
设计一个无缝适应不同屏幕尺寸的网站
如何处理CSS样式的层叠问题
探究最佳响应式布局框架:竞争激烈!
学习基本数据类型的快速入门:掌握常用操作技巧
CSS中float布局介绍
一同探讨响应式布局的益处
掌握响应式设计的益处,让网页在不同设备上展现完美适配!
可能导致CSS加载失败的原因有哪些?
各种基本数据类型的全面操作指南
CSS3选择器是否用于设计界面结构?
响应式布局优化移动设备适配的策略与实用技巧
伪元素怎么清除浮动
利用CSS响应式布局创作独特网页设计的设计技巧