【问题标题】:Change color of SVG background-image (Bootstrap 4 carousel)更改 SVG 背景图像的颜色(Bootstrap 4 轮播)
【发布时间】:2018-04-17 18:49:13
【问题描述】:

我有一个带有下一个/上一个控件的 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>

更改指示器的颜色很容易,因为只需将颜色设置为背景即可。但是为 prev/next 控件设置 background-color 或 color 并没有改变实际控件图标的颜色。

我发现图标是通过 background-image 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';
}

有没有办法在不覆盖整个背景图像行的情况下更改图标的颜色?

谢谢

【问题讨论】:

    标签: css svg background-image bootstrap-4 twitter-bootstrap-4


    【解决方案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");
        }
    

    【讨论】:

      【解决方案2】:

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

      您需要更改 SVG 本身。改变

      fill='%23fff'
      

      fill='%23000'
      

      或任何你想要的颜色。这里的“%23”只是“#”符号。在这样的数据 URI 中使用时,它必须是 URL-encoded

      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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-29
        • 2021-11-22
        • 2018-06-30
        • 2020-03-06
        • 2013-10-21
        • 1970-01-01
        • 2014-04-26
        • 1970-01-01
        相关资源
        最近更新 更多