【问题标题】:How to set a External SVG color in HTML using CSS?如何使用 CSS 在 HTML 中设置外部 SVG 颜色?
【发布时间】:2015-03-28 14:27:15
【问题描述】:

我正在尝试在我的网页上使用 SVG。

但它的颜色是黑色的。 所以,我想改变它。 所以,我已经完成了-

.red_color_svg
{
    color: red;
    border: 5px solid currentColor;
    fill: currentColor;
}
<object type="image/svg+xml" data="https://rawcdn.githack.com/encharm/Font-Awesome-SVG-PNG/master/black/svg/heart.svg" class="weather_icon red_color_svg circle"></object>

导入heart_border.svg文件并使其颜色变为红色。但它不起作用,正如您在输出中看到的那样。

谁能帮我解决这个问题?

非常感谢您的帮助。

【问题讨论】:

  • 我需要看到你的 svg 来帮助你。
  • 你不能设置外部 svg 的样式。

标签: javascript html css svg


【解决方案1】:

使用您当前的代码,您可以在 object 元素上设置填充。

相反,您需要在 svg 元素上设置它。

类似这样的:

.red_color_svg svg {
   fill: currentColor;
}

【讨论】:

    【解决方案2】:

    问题在于您没有针对实际的 SVG 元素,而是针对“SVG 容器”。为了能够更改 SVG 中某个元素的颜色,您必须以该特定元素为目标。

    例如更改 SVG 中所有路径的填充颜色:

    .weather_icon path {
        fill: yellow;
    }
    

    如果您想更轻松地处理将类名添加到 svg 内的不同元素。

    <path class="my-class" ......... />
    

    这将可以通过其类来定位特定元素:

    .weather_icon .my-class {
        fill:blue;
        stroke:green;
    }
    

    【讨论】:

    • 这对外部 svg 不起作用,如原始问题中所述。
    【解决方案3】:

    CSS 不适用跨文档,这里有两个文档 heart_border.svg 和容器 html 文档。

    您需要在 heart_border.svg 中包含 CSS,例如通过添加&lt;link&gt; 元素或&lt;xml-stylesheet&gt; 处理指令或通过&lt;style&gt; 元素将其内联添加到该文件中。

    或者,如果您将 SVG 内联添加到 html 文档本身中,这样您只有一个文档,那么 CSS 就会应用。

    【讨论】:

      【解决方案4】:

      你真的需要 SVG 作为外部文件吗?您可能希望将 svg 放在本地一次文档中。

      <div style="display: none">
          <svg><g id="svg1"><path d="some exampe path"/></g></svg>
      </div>
      

      并在多个地方链接到它

      <svg viewBox="0 0 64 64"><use xlink:href="#svg1"></use></svg>
      

      你可以单独设置每个链接的样式

      【讨论】:

        【解决方案5】:

        这个帖子很旧,但我想分享我的解决方案,基于 SVG 过滤器。您只需要根据需要定义一个 feColorMatrix 过滤器并将其应用到 外部图像。有关详细信息,请参阅下面的示例。

        兼容任何可以处理 SVG 的浏览器。

        <svg width="100%" height="100%" class="draggable">
          <defs>
            <filter id="customColor1">
              <!-- Match hex color for #50A -->
              <feColorMatrix
                in="SourceGraphic"
                type="matrix"
                values="0 0 0 0 0.3333333333333333 0 0 0 0 0 0 0 0 0 0.6666666666666666 0 0 0 1 0"
              ></feColorMatrix>
            </filter>
            
            <filter id="customColor2">
              <!-- Match hex color for #0F0 -->
              <feColorMatrix
                in="SourceGraphic"
                type="matrix"
                values="0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0"
              ></feColorMatrix>
            </filter>
          </defs>
          <image href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/icon-bike-black.svg" width="50" height="50"></image>
          <image href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/icon-bike-black.svg" filter="url(#customColor1)" width="50" height="50" x="100"></image>
          <image href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/icon-bike-black.svg" filter="url(#customColor2)" width="50" height="50" x="200"></image>
          
        </svg>

        [奖金]

        // A little helper to generate matrix color from source and destination colors
        // To easily dive in : https://codepen.io/jacobberglund/pen/ORNQAr
        // To understand what's going on here read this article by A List Apart
        // https://alistapart.com/article/finessing-fecolormatrix/
        
        interface RgbColor {
          /** Values are in percent (ex: 255,127,0,255 => 1,0.5,0,1) */
          r: number;
          g: number;
          b: number;
          a: number;
        }
        
        export class ColorMatrixHelper {
          public static getMatrix(hexColor: string) {
            const rgbColor: RgbColor = ColorMatrixHelper.hexToRgb(hexColor);
            return ColorMatrixHelper.computeMatrixColor(rgbColor);
          }
        
          // Inspired by this answer : https://stackoverflow.com/a/5624139/11480016
          private static hexToRgb(hex3or6): RgbColor {
            // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
            const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
            const hex6 = hex3or6.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b);
        
            var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(hex6);
            const base = 1 / 255;
            return result
              ? {
                  r: parseInt(result[1], 16) * base,
                  g: parseInt(result[2], 16) * base,
                  b: parseInt(result[3], 16) * base,
                  a: result[4] ? parseInt(result[4], 16) * base : 1,
                }
              : null;
          }
        
          private static computeMatrixColor(rgbColor: RgbColor): string {
            let matrix;
            if (rgbColor) {
              // Ignore original colors and apply the new one
              matrix =
                `0 0 0 0 ${rgbColor.r} ` + // Red
                `0 0 0 0 ${rgbColor.g} ` + // Green
                `0 0 0 0 ${rgbColor.b} ` + // Blue
                `0 0 0 ${rgbColor.a} 0`; // Alpha
            } else {
              // Identity (keep orignal colors)
              matrix =
                `1 0 0 0 0 ` + // Red
                `0 1 0 0 0 ` + // Green
                `0 0 1 0 0 ` + // Blue
                `0 0 0 1 0`; // Alpha
            }
            return matrix;
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-08-28
          • 1970-01-01
          • 2017-05-19
          • 2015-09-16
          • 1970-01-01
          • 2017-09-04
          相关资源
          最近更新 更多