【问题标题】:Svg color not changing from css color?svg颜色不会从css颜色改变?
【发布时间】:2022-01-01 12:54:10
【问题描述】:

通常我通过在我的 css 中定位 svg 并设置 color 属性来更改 svgs 颜色,但这对波纹管 svg 元素没有影响。知道为什么吗?我读过 svg 必须有一个我添加的 fill="currentColor" 属性,但颜色仍然没有改变。有没有办法可以从 css 动态设置 svg 颜色?

<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="ionicon" viewBox="0 0 512 512"><title>Logo Instagram</title><path d="M349.33 69.33a93.62 93.62 0 0193.34 93.34v186.66a93.62 93.62 0 01-93.34 93.34H162.67a93.62 93.62 0 01-93.34-93.34V162.67a93.62 93.62 0 0193.34-93.34h186.66m0-37.33H162.67C90.8 32 32 90.8 32 162.67v186.66C32 421.2 90.8 480 162.67 480h186.66C421.2 480 480 421.2 480 349.33V162.67C480 90.8 421.2 32 349.33 32z"/><path d="M377.33 162.67a28 28 0 1128-28 27.94 27.94 0 01-28 28zM256 181.33A74.67 74.67 0 11181.33 256 74.75 74.75 0 01256 181.33m0-37.33a112 112 0 10112 112 112 112 0 00-112-112z"/></svg>

【问题讨论】:

标签: html css svg


【解决方案1】:

使用此代码。
我将fill 设置为路径。

<svg xmlns="http://www.w3.org/2000/svg" class="ionicon" viewBox="0 0 512 512"><title>Logo Instagram</title><path d="M349.33 69.33a93.62 93.62 0 0193.34 93.34v186.66a93.62 93.62 0 01-93.34 93.34H162.67a93.62 93.62 0 01-93.34-93.34V162.67a93.62 93.62 0 0193.34-93.34h186.66m0-37.33H162.67C90.8 32 32 90.8 32 162.67v186.66C32 421.2 90.8 480 162.67 480h186.66C421.2 480 480 421.2 480 349.33V162.67C480 90.8 421.2 32 349.33 32z"/><path d="M377.33 162.67a28 28 0 1128-28 27.94 27.94 0 01-28 28zM256 181.33A74.67 74.67 0 11181.33 256 74.75 74.75 0 01256 181.33m0-37.33a112 112 0 10112 112 112 112 0 00-112-112z" fill="currentColor" /></svg>

【讨论】:

  • 试过了,没用
  • currentColor 是 svg 父元素的颜色。
【解决方案2】:

你可以试试:

<style>
    #svg{
        --currentColor: #00ff00;
    }
</style>
<svg id="svg" width="100" height="100" viewbox="0 0 100 100" xlmns="http://www.w3.org/2000/svg">
    <style>
        rect{
            fill: var(--currentColor);
        }
    </style>
</svg>

这个方法行得通。

【讨论】:

    【解决方案3】:

    你的 CSS 很可能有问题。
    可能是一个具体问题(例如之前声明的 svg 颜色样式)。

    它应该像这样工作:

    body{
    font-size: 3vw;
    }
    
    .green{
    color: green
    }
    
    .red{
    color: red
    }
    
    .fill-gray svg{
    fill: #ccc;
    }
    
    .svg-inline{
    display:inline-block;
    font-size:1em;
    height:1em;
    width:1em;
    }
    <p class="green">
    <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="ionicon svg-inline" viewBox="0 0 512 512">
    <title>Logo Instagram</title>
    <g id="icon-instagram">
    <path id="path1" d="M349.33 69.33a93.62 93.62 0 0193.34 93.34v186.66a93.62 93.62 0 01-93.34 93.34H162.67a93.62 93.62 0 01-93.34-93.34V162.67a93.62 93.62 0 0193.34-93.34h186.66m0-37.33H162.67C90.8 32 32 90.8 32 162.67v186.66C32 421.2 90.8 480 162.67 480h186.66C421.2 480 480 421.2 480 349.33V162.67C480 90.8 421.2 32 349.33 32z"/>
    <path id="path2" d="M377.33 162.67a28 28 0 1128-28 27.94 27.94 0 01-28 28zM256 181.33A74.67 74.67 0 11181.33 256 74.75 74.75 0 01256 181.33m0-37.33a112 112 0 10112 112 112 112 0 00-112-112z"/>
    </g>
    </svg>
     green icon
    </p>
    
    <p class="red">
    <svg class="svg-inline" fill="currentColor" viewBox="0 0 512 512">
    <use href="#icon-instagram" />
    </svg> Inline Icon (fill inherited by parent's color)
    </p>
    
    <p class="red fill-gray">
    <svg class="svg-inline" fill="currentColor" viewBox="0 0 512 512">
    <use href="#icon-instagram" />
    </svg> Inline Icon (fill color overridden by fill property)
    </p>
    
    <p class="red">
    <svg class="svg-inline" viewBox="0 0 512 512">
    <use href="#icon-instagram" />
    </svg> Inline Icon (fill color can't be inherited, since fill="currentColor" is missing )
    </p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-29
      • 2020-10-07
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      相关资源
      最近更新 更多