SVG 的 fill 属性在 Chrome 中无法正常工作

6

问题是我无法在Chrome中设置fill属性的值(颜色),但在Firefox中可以。我尝试了很多方法,但没有结果。我唯一看到改变SVG图标颜色的方法是通过jQuery(或JavaScript)更改下面的<symbol> id。请帮我解决这个问题!

这是我需要在Chrome中工作的内容:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        span:hover .pathCrossBtn{
            fill: blue;
        }
    </style>
</head>
<body>
    <svg width="0" height="0" style='display: none;' xmlns="http://www.w3.org/2000/svg">
    <symbol viewBox="0 0 2048 2048" id="crossBtn">
        <path class="pathCrossBtn" fill="red" d="M1618 1450q0 40-28 68l-136     136q-28 28-68 28t-68-28l-294-294-294 294q-28 28-68 28t-68-28l-136-136q-28-28-28-68t28-68l294-294-294-294q-28-28-28-68t28-68l136-136q28-28 68-28t68 28l294 294 294-294q28-28 68-28t68 28l136 136q28 28 28 68t-28 68l-294 294 294 294q28 28 28 68z"/>
    </symbol>
</svg>
<span>
    <svg class="crossBtn" viewBox="0 0 2048 2048" width="30" height="30">
        <use xlink:href="#crossBtn"></use>
    </svg>
</span>
</body>
</html>

这是一种不适合我的问题的糟糕解决方案。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
    <svg width="0" height="0" style='display: none;' xmlns="http://www.w3.org/2000/svg">
        <symbol viewBox="0 0 2048 2048" id="crossBtnBlue">
          <path class="pathCrossBtn" fill="blue" d="M1618 1450q0 40-28 68l-136 136q-28 28-68 28t-68-28l-294-294-294 294q-28 28-68 28t-68-28l-136-136q-28-28-28-68t28-68l294-294-294-294q-28-28-28-68t28-68l136-136q28-28 68-28t68 28l294 294 294-294q28-28 68-28t68 28l136 136q28 28 28 68t-28 68l-294 294 294 294q28 28 28 68z"/>
        </symbol>
        <symbol viewBox="0 0 2048 2048" id="crossBtnRed">
           <path class="pathCrossBtn" fill="red" d="M1618 1450q0 40-28 68l-136 136q-28 28-68 28t-68-28l-294-294-294 294q-28 28-68 28t-68-28l-136-136q-28-28-28-68t28-68l294-294-294-294q-28-28-28-68t28-68l136-136q28-28 68-28t68 28l294 294 294-294q28-28 68-28t68 28l136 136q28 28 28 68t-28 68l-294 294 294 294q28 28 28 68z"/>
        </symbol>
</svg>
<span>
    <svg class="crossBtn" viewBox="0 0 2048 2048" width="30" height="30">
        <use xlink:href="#crossBtnRed"></use>
    </svg>
</span>
<script>
;(function(){
    $('.crossBtn')
        .mouseover(function(){
            $('span svg use').attr('xlink:href','#crossBtnBlue');
        })
        .mouseleave(function(){
            $('span svg use').attr('xlink:href','#crossBtnRed');
        })
}());
</script>
</body>
</html>

@RobertLongson想确保我理解:您是说在使用use引用的SVG中存在一个错误,因为嵌入的SVG位于影子DOM中? - henry
@henry Firefox已经修复了其实现以匹配SVG 2规范。尝试使用夜间版并查看。我想如果Chrome还没有对齐SVG 2规范,它也会这样做。 - Robert Longson
@RobertLongson 但我仍然不清楚“this”在“这是一个已知的错误”中确切的含义。我尝试寻找错误票证,但迄今为止没有运气。 - henry
@henry https://bugzilla.mozilla.org/show_bug.cgi?id=265894 - Robert Longson
2个回答

12

<symbol>元素的path fill属性中使用currentcolor关键字。

此关键字代表元素颜色属性的实际值。这使您可以在默认情况下未接收该值的属性上使用颜色值。

然后,在将<use>元素实例化的<svg>容器类中添加color CSS属性。

.icon {
  width: 30px;
  height: 30px;
}

.icon--blue {
  color: blue;
}

.icon--red {
  color: red;
}
<svg width="0" height="0" style="display: none;" xmlns="http://www.w3.org/2000/svg">
  <symbol viewBox="0 0 2048 2048" id="crossBtn">
    <path class="pathCrossBtn" fill="currentcolor" d="M1618 1450q0 40-28 68l-136     136q-28 28-68 28t-68-28l-294-294-294 294q-28 28-68 28t-68-28l-136-136q-28-28-28-68t28-68l294-294-294-294q-28-28-28-68t28-68l136-136q28-28 68-28t68 28l294 294 294-294q28-28 68-28t68 28l136 136q28 28 28 68t-28 68l-294 294 294 294q28 28 28 68z"
    />
  </symbol>
</svg>

<svg class="icon icon--red" viewBox="0 0 2048 2048">
  <use xlink:href="#crossBtn"></use>
</svg>
<svg class="icon icon--blue" viewBox="0 0 2048 2048">
  <use xlink:href="#crossBtn"></use>
</svg>


0

虽然不是针对上述问题的通用解决方案,但我来到这里是因为遇到了类似的问题,在Chrome和Firefox以及其他浏览器中,fill="none"在某些情况下似乎无法正常工作。这段SCSS代码为我们解决了这个问题,在我们的情况下,它适用于通过react-svgr渲染的feather icons

svg:global(.feather) {
    // Fix for `fill="none"` not working properly in (at least) Chrome and Firefox
    // Part 1: If there is a `fill` property, reset `fill-opacity`.
    //         This is for children of elements with `fill="none"`
    &[fill], *[fill] {
        fill-opacity: initial;
    }
    // Part 2: If the element uses `fill="none"`, set `fill-opacity` to 0.
    &[fill='none'], *[fill='none'] {
        fill-opacity: 0;
    }

}

通用情况下的等效CSS:

svg[fill], svg *[fill] {
    fill-opacity: initial;
}
svg[fill='none'], svg *[fill='none'] {
    fill-opacity: 0;
}

请注意,这可能无法涵盖所有具有填充属性和具有不同值属性的子元素的SVG元素的情况,因此请根据您的需求进行调整。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接