SVG图标更改填充颜色内部

时间:2017-05-16 16:49:02

标签: css css3 svg

我有这个facebook SVG图标,我需要能够更改圆形颜色和内部颜色(图标本身)。如何将其从透明更改为白色?

我有什么 what I have 我需要的 what I need

SVG代码:

    <?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<div id="facebook">
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652" style="enable-background:new 0 0 49.652 49.652;"
     xml:space="preserve">
<g>
    <g>
        <path d="M24.826,0C11.137,0,0,11.137,0,24.826c0,13.688,11.137,24.826,24.826,24.826c13.688,0,24.826-11.138,24.826-24.826
            C49.652,11.137,38.516,0,24.826,0z M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845
            v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z
            "/>
    </g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>
</div>

4 个答案:

答案 0 :(得分:3)

问题是SVG中的<path>只是定义了图标黑色部分的形状。

如果你想制作&#34; f&#34;白色,有两种选择。

  1. 在路径后面添加一个白色的形状,以便透过孔显示。
  2. &#13;
    &#13;
    div {
      background-color: orange;
    }
    &#13;
    <div id="facebook">
    <svg id="Capa_1"
         width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652">
      <circle cx="25" cy="25" r="20" fill="white"/>
      <path d="M24.826,0C11.137,0,0,11.137,0,24.826c0,13.688,11.137,24.826,24.826,24.826c13.688,0,24.826-11.138,24.826-24.826
                C49.652,11.137,38.516,0,24.826,0z M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845
                v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z"/>
    </svg>
    </div>
    &#13;
    &#13;
    &#13;

    1. 将路径分成两部分:外圈和&#34; f&#34;形状。制作&#34; f&#34;形状白色。
    2. &#13;
      &#13;
      div {
        background-color: orange;
      }
      &#13;
      <div id="facebook">
      <svg id="Capa_1"
           width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652">
        <path d="M24.826,0C11.137,0,0,11.137,0,24.826c0,13.688,11.137,24.826,24.826,24.826c13.688,0,24.826-11.138,24.826-24.826 C49.652,11.137,38.516,0,24.826,0z"/>
        <path d="M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845 v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z" fill="white"/>
      </svg>
      </div>
      &#13;
      &#13;
      &#13;

      选择你喜欢的那个。

答案 1 :(得分:0)

嗯,你不能改变徽标中f的颜色,因为黑色都是一个形状,所以它实际上是从圆圈中切出来的。您可以做的是在SVG中添加徽标后面的另一种形状。

我减少了您拥有的SVG代码:

<强> SVG:

<div id="facebook">
  <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652" style="enable-background:new 0 0 49.652 49.652;" xml:space="preserve">
  <circle cx="25" cy="25" r="20" class="circle"/>
        <path d="M24.826,0C11.137,0,0,11.137,0,24.826c0,13.688,11.137,24.826,24.826,24.826c13.688,0,24.826-11.138,24.826-24.826
            C49.652,11.137,38.516,0,24.826,0z M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845
            v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z
            " />


  </svg>
</div>

<强> CSS

#facebook {
  background-color: red;
}

.circle {
  fill: white;
}

工作小提琴:https://jsfiddle.net/disinfor/gvpeLn91/

答案 2 :(得分:0)

最明显的当然是编辑SVG,另一个选择是添加一个与黑色圆圈匹配的伪元素。

如果例如从CDN或类似物品加载无法编辑SVG的图标,这个技巧可能很方便。

&#13;
&#13;
div {
  position: relative;
  display: inline-block;
  background-color: red;
  padding: 10px;
}

div svg {
  position: relative;
  display: block;
}

div::before {
  content: '';
  position: absolute;
  left: 10px;
  top: 10px;
  width: calc(100% - 20px);
  height: calc(100% - 20px);
  border-radius: 50%;
  background-color: white;
}
&#13;
<div id="facebook">
  <svg id="Capa_1" width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652">
  <path d="M24.826,0C11.137,0,0,11.137,0,24.826c0,13.688,11.137,24.826,24.826,24.826c13.688,0,24.826-11.138,24.826-24.826
            C49.652,11.137,38.516,0,24.826,0z M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845
            v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z"/>
</svg>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

到目前为止,我发现最好的方法是使用CSS clip-path属性。它适用于任何透明图像。

img {
  background: #ff1234;
  clip-path: circle(50% at 50% 50%);
}
<img src="https://image.flaticon.com/icons/png/512/8/8730.png" width="150" height="150" />

有关在https://css-tricks.com/clipping-masking-css/处进行剪切和遮罩的更多信息。