【发布时间】:2015-01-18 01:41:33
【问题描述】:
我不确定从 HTML 文件中的通用内联 SVG“模板”调用的单个 SVG 实例的结构、选择和样式的正确标记。
<!-- first instance to style -->
<svg id="instance1" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
<!-- second instance, want different styles -->
<svg id="instance2" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
在我的 SVG 模板中,我有一个由几个 paths 组成的组 <g>,每个组都有一个类名。我已为该 SVG 组指定了一个 ID 为 template,并且正在调用它的实例。
<!-- first instance to style -->
<svg id="instance1" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
<!-- second instance, want to apply different styles -->
<svg id="instance2" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
现在我尝试在 CSS 中选择/设置这些实例的元素,如下所示(尽管这种语法不起作用!):
/* want to style each instance differently */
#instance1 .outline {
fill: red;
}
#instance2 .outline {
fill: green;
}
我认为这很容易,但我无法找出正确的方法来选择/设置<use> 一组通用路径的单个 SVG。
有什么想法吗?谢谢!
代码 sn-p 可在http://codepen.io/edhebert/pen/GgmKOo 上查看
【问题讨论】:
-
@RobertLongson 并非不可能。请参阅下面的答案。