Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of SVG background-image (Bootstrap 4 carousel)

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

like image 640
bertcc423 Avatar asked Nov 05 '17 14:11

bertcc423


1 Answers

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>
like image 162
Paul LeBeau Avatar answered Sep 29 '22 08:09

Paul LeBeau