如何使用CSS在HTML中设置外部SVG颜色?

Abr*_*hin 6 html javascript css svg css3

我想在我的网页上使用SVG.

但它的颜色是黑色的.所以,我希望它能被改变.所以,我做了 -

.red_color_svg
{
    color: red;
    border: 5px solid currentColor;
    fill: currentColor;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

导入heart_border.svg文件并使其颜色为红色.但它不会像你看到输出那样起作用.

任何人都可以帮我解决这个问题吗?

非常感谢您提前帮助.

Rob*_*son 7

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

您需要在heart_border.svg中包含CSS,例如通过添加<link>元素或<xml-stylesheet>处理指令,或者通过元素将其添加到该文件中<style>.

或者,如果您在html文档本身中添加SVG内联,以便您只有一个文档,那么CSS将会应用.


小智 6

该线程很旧,但我想分享我的基于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>
Run Code Online (Sandbox Code Playgroud)

[奖金]

// 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 : /sf/answers/393689761/
  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;
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 -5

问题是您没有定位实际的 SVG 元素,而是定位“SVG 容器”。为了能够更改 SVG 内某个元素的颜色,您必须定位该特定元素。

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

.weather_icon path {
    fill: yellow;
}
Run Code Online (Sandbox Code Playgroud)

如果您想让处理更容易,请将类名添加到 svg 内的不同元素。

<path class="my-class" ......... />
Run Code Online (Sandbox Code Playgroud)

这将使通过其类定位特定元素成为可能:

.weather_icon .my-class {
    fill:blue;
    stroke:green;
}
Run Code Online (Sandbox Code Playgroud)

  • 正如原始问题中所问,这不适用于外部 svg。 (15认同)