我有一个如下所示的SVG精灵。
在使用<use>
标签技术时,似乎无法从CSS访问SVG的各个部分。
我知道通过使用fill: currentColor
(请参见#icon-two-parts
符号中的.path1
),可以通过同时使用color
和fill
值从CSS向同一SVG的不同部分应用两种不同的颜色。
在以这种方式使用SVG时,是否有任何方法可以将多于两种颜色应用于同一元素?
例如,是否可以将四个颜色应用于#icon-four-part
符号中的四个路径?
div {
display: inline-block;
}
.icon {
display: inline-block;
width: 80px;
height: 40px;
fill: currentColor;
}
.icon-one-part {
color: green;
}
.icon-two-part {
color: green;
fill: red;
}
<svg display="none" width="0" height="0" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="icon-one-part" viewBox="0 0 670 1024">
<title>icon-one-part</title>
<path class="path1" d="M355.486 512l-343.355-337.408c-16.148-15.872-16.148-41.314 0-57.147l107.362-105.551c16.148-15.833 42.023-15.833 58.171 0l479.823 471.513c16.148 15.872 16.148 41.354 0 57.226l-479.862 471.473c-16.108 15.833-41.984 15.833-58.171 0l-107.362-105.551c-16.148-15.833-16.148-41.275 0-57.147l343.394-337.408z"></path>
</symbol>
<symbol id="icon-two-part" viewBox="0 0 1339 1024">
<title>icon-two-part</title>
<path class="path1" fill="currentColor" d="M314.092 512l343.355 337.408c16.148 15.872 16.148 41.314 0 57.147l-107.362 105.551c-16.148 15.833-42.023 15.833-58.171 0l-479.783-471.513c-16.148-15.872-16.148-41.354 0-57.226l479.783-471.513c16.108-15.833 41.984-15.833 58.171 0l107.362 105.551c16.148 15.833 16.148 41.275 0 57.147l-343.355 337.447z"></path>
<path class="path2" d="M1025.024 512l-343.316-337.408c-16.148-15.872-16.148-41.314 0-57.147l107.323-105.551c16.148-15.833 42.063-15.833 58.171 0l479.862 471.513c16.148 15.872 16.148 41.354 0 57.226l-479.902 471.473c-16.069 15.833-41.984 15.833-58.171 0l-107.323-105.551c-16.148-15.833-16.148-41.275 0-57.147l343.355-337.408z"></path>
</symbol>
<symbol id="icon-four-part" viewBox="0 0 2272 1024">
<title>icon-four-part</title>
<path class="path1" d="M777.413 512l343.355 337.408c16.148 15.872 16.148 41.314 0 57.147l-107.362 105.551c-16.148 15.833-42.023 15.833-58.171 0l-479.783-471.473c-16.148-15.872-16.148-41.354 0-57.226l479.823-471.513c16.108-15.833 41.984-15.833 58.171 0l107.362 105.551c16.148 15.833 16.148 41.275 0 57.147l-343.394 337.408z"></path>
<path class="path2" d="M1488.384 512l-343.316-337.408c-16.148-15.872-16.148-41.314 0-57.147l107.323-105.551c16.148-15.833 42.063-15.833 58.171 0l479.862 471.513c16.148 15.872 16.148 41.354 0 57.226l-479.902 471.473c-16.069 15.833-41.984 15.833-58.171 0l-107.323-105.551c-16.148-15.833-16.148-41.275 0-57.147l343.355-337.408z"></path>
<path class="path3" d="M314.092 512l343.355 337.408c16.148 15.911 16.148 41.314 0 57.147l-107.362 105.551c-16.148 15.833-42.023 15.833-58.171 0l-479.783-471.473c-16.148-15.872-16.148-41.354 0-57.226l479.783-471.513c16.108-15.833 41.984-15.833 58.171 0l107.362 105.551c16.148 15.833 16.148 41.275 0 57.147l-343.355 337.408z"></path>
<path class="path4" d="M1958.203 512l-343.316-337.408c-16.148-15.872-16.148-41.314 0-57.147l107.323-105.551c16.148-15.833 42.063-15.833 58.171 0l479.862 471.513c16.148 15.872 16.148 41.354 0 57.226l-479.902 471.473c-16.069 15.833-41.984 15.833-58.171 0l-107.323-105.551c-16.148-15.833-16.148-41.236 0-57.147l343.355-337.408z"></path>
</symbol>
</defs>
</svg>
<div>
<svg class="icon icon-one-part"><use xlink:href="#icon-one-part"></use></svg>
</div>
<div>
<svg class="icon icon-two-part"><use xlink:href="#icon-two-part"></use></svg>
</div>
<div>
<svg class="icon icon-four-parts"><use xlink:href="#icon-icon-four-part"></use></svg>
</div>
<use>
标签用于创建相同的副本。如果这不是您想要的用例,那么<use>
并不是正确的答案。 - Robert Longson