15

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

bertcc423
  • 203
  • 1
  • 4
  • 6

4 Answers4

28

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>
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
1

Change color of SVG background-image

  .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");
    }
Fahimeh Ahmadi
  • 813
  • 8
  • 13
0

for bootstrap 5 you have to do it like this suppose you have svg with defaults

<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>

change fill to fill="#00ff00" for green fill="#000000" for black etc. you can read the hex code from internet

Sadique Khan
  • 230
  • 3
  • 9
0

I did it like this

  • copied hexa decimal code from google for example #A569BD
  • used url encoder online https://meyerweb.com/eric/tools/dencoder/
  • it encoded Hexa code #A569BD to %23A569BD
  • I used encoded code in svg to fill elements like this

enter image description here

Rohit Kumar
  • 983
  • 9
  • 11