更改 SVG 背景图像的颜色(Bootstrap 4 carousel) [英] Change color of SVG background-image (Bootstrap 4 carousel)
问题描述
我有一个带有下一个/上一个控件的 BS 4 旋转木马,目前可以使用.我有一个浅色背景,所以我想更改控件的颜色(当前为白色).
HTML:
<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">以前的</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">下一步</span></a>
更改指示器的颜色很容易,因为只需将颜色设置为它们的背景即可.但是为上一个/下一个控件设置背景颜色或颜色并没有改变实际控件图标的颜色.
我发现图标是通过背景图像 SVG 设置的.其中有一部分说明了填充颜色:
.carousel-control-prev-icon {背景图像:url(数据:图像/svg+xml;字符集=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg'fill='%23fff' viewBox='0 0 88'%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 {填充:#000;}.carousel-control-prev-icon, .carousel-control-next-icon {填充:'#000';}.carousel-control-prev-icon, .carousel-control-next-icon {填充:'%23000';}
有没有办法在不覆盖整个背景图像行的情况下更改图标的颜色?
谢谢
您不能使用 CSS 规则为该 SVG 背景图像的内容设置样式.它被解析和处理,并在解码时有效地转换为位图图像.
您需要更改 SVG 本身.改变
fill='%23fff'
到
fill='%23000'
或您想要的任何颜色.这里的%23"只是#"符号.在像这样的数据 URI 中使用时,它需要URL 编码.
div {背景颜色:黑色;}按钮 {宽度:24px;高度:24px;边界:无;背景色:透明;}.carousel-control-prev-icon {背景图像:url(数据:图像/svg+xml;字符集=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg'fill='%23fff' viewBox='0 0 88'%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 {背景颜色:白色;}.inverted .carousel-control-prev-icon {背景图像:url(数据:图像/svg+xml;字符集=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg'fill='%23000' viewBox='0 0 88'%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");}
<button class="carousel-control-prev-icon"></button><div class="inverted"><button class="carousel-control-prev-icon"></button>
I have a BS 4 carousel with next/prev controls which works so far. I have a light background so I would like to change the color of the controls (currently white).
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>
Changing the color of the indicators was easy since that's simply set with a colour as their background. But setting background-color or color for the prev/next controls didn't change the color of the actual control icon.
I found that the icons are set via background-image SVG. There is a part of it stating the fill color:
.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");
}
I tried
.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';
}
Is there a way to change the color of the icon without overriding the entire background-image line?
Thank you
You can't style the contents of that SVG background image using CSS rules. It is parsed and processed and effectively turned into a bitmap image as it's decoded.
You would need to alter the SVG itself. Change the
fill='%23fff'
to
fill='%23000'
or to whichever colour you wish. The "%23" here is just the "#" symbol. It needs to be URL-encoded when used in a Data URI like this.
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>
这篇关于更改 SVG 背景图像的颜色(Bootstrap 4 carousel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!