改变SVG背景图(Bootstrap 4轮播)的颜色

15

我有一个带有下一个/上一个控件的BS 4轮播,目前它可以正常工作。我的背景很浅,所以我想改变控件的颜色(目前为白色)。

HTML:

<div id="carouselQuotes" class="carousel slide quotecaru" data-ride="carousel">
<ol class="carousel-indicators">
    <li data-target="#carouselQuotes" data-slide-to="0" class="active"></li>
    <li data-target="#carouselQuotes" data-slide-to="1"></li>
    <li data-target="#carouselQuotes" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
    ... carousel items ...
</div>
<a class="carousel-control-prev" href="#carouselQuotes" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselQuotes" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
</a>

更改指示器的颜色很容易,因为它们的背景只需要设置一个颜色即可。但是,为上一个/下一个控件设置background-color或color并不会改变实际控件图标的颜色。

我发现这些图标是通过背景图像SVG来设置的。其中有一部分规定了填充颜色:

.carousel-control-prev-icon {
    background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E");
}

我尝试过

.carousel-control-prev-icon, .carousel-control-next-icon {
    fill: #000;
}
.carousel-control-prev-icon, .carousel-control-next-icon {
    fill: '#000';
}
.carousel-control-prev-icon, .carousel-control-next-icon {
    fill: '%23000';
}

是否有一种方法可以在不覆盖整个background-image行的情况下更改图标的颜色?

谢谢

4个回答

28

使用CSS规则无法对SVG背景图像的内容进行样式设置。 在解码时,它被解析、处理并有效地转换为位图图像。

您需要更改SVG本身。更改

fill='%23fff'

to
fill='%23000'

或者你希望是任何颜色。这里的 "%23" 只是 "#" 符号。在像这样的数据 URI 中使用时,它需要进行URL编码

div {
  background-color: black;
}

button {
  width: 24px;
  height: 24px;
  border: none;
  background-color: transparent;
}

.carousel-control-prev-icon {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E");
}


div.inverted {
  background-color: white;
}

.inverted .carousel-control-prev-icon {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23000' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E");
}
<div>
  <button class="carousel-control-prev-icon"></button>
</div>

<div class="inverted">
  <button class="carousel-control-prev-icon"></button>
</div>


1

更改SVG背景图像的颜色

  .carousel-control-prev-icon {
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23ff0000' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e");
    }

0

对于Bootstrap 5,您必须这样做 假设您有默认的SVG

<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" fill="#00ff00" class="bi bi-check-circle" viewBox="0 0 16 16">
<svg>

将填充颜色更改为 fill="#00ff00" 以获得绿色, fill="#000000" 以获得黑色等。 您可以从互联网上读取十六进制代码。


0

我是这样做的

  • 从谷歌上复制十六进制代码,例如 #A569BD
  • 使用在线 URL 编码器 https://meyerweb.com/eric/tools/dencoder/
  • 它将十六进制代码 #A569BD 编码为 %23A569BD
  • 我在 SVG 中使用编码后的代码来填充元素,如下所示

enter image description here


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