html - How to change the background color for Bootstrap's carousel plugin?


Keywords:html 


Question: 

So I'm having trouble trying to change the background color for the carousel. No matter what I do, it seems like the background is always white. I used the code from for my carousel.

<div class="container" id="slides">
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="" data-slide-to="0" class="active"></li>
      <li data-target="" data-slide-to="1"></li>
      <li data-target="" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img src="" alt="Connect K project" style="width:100%;">
      </div>

      <div class="item">
        <img src="" alt="Chicago" style="width:100%;">
      </div>

      <div class="item">
        <img src="" alt="MedAppJam project" style="width:100%;">
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

I've tried using CSS styles to change the background but for some reason, it doesn't seem to work. And I'm not sure why. Here's the CSS I added to try to change the background color.

#slides{
  color: grey;
  background-color: grey !important;
}

.carousel{
  color: grey;
  background-color: grey !important;
}
.carousel .item{
  color: grey;
  background-color: grey;
}

That's the link to my complete code. Sorry for all the weird colors. I was just messing around with it. Anyway, I've also checked out other questions like this one change twitter bootstrap carousel background? and it doesn't seem to work. Can anyone explain this to me?


1 Answer: 

The problem you have at the moment is that the carousel has the container class applied and is, therefore, adding a fixed width to the element. The white you're seeing either side is actually the background colour of the body.

There are two ways around this (I've shortened the code for the example):

Full width

<!-- Remove the container class from the slides div -->
<div id="slides">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">

        <!-- Indicators -->

        <!-- Wrapper for slides -->

        <!-- Left and right controls -->

    </div>
</div>

Image width (shows grey background)

<!-- Wrap carousel-inner in a container div -->
<div id="slides">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">

        <!-- Indicators -->

        <!-- Wrapper for slides -->
        <div class="container"> <!-- Added this line -->
            <div class="carousel-inner">
                <div class="item active">
                    <img src="" alt="Connect K project" style="width:100%;">
                </div>

                <div class="item">
                    <img src="" alt="Chicago" style="width:100%;">
                </div>

                <div class="item">
                    <img src="" alt="MedAppJam project" style="width:100%;">
                </div>
            </div>
        </div> <!-- Added this line -->

        <!-- Left and right controls -->

    </div>
</div>

I've forked your codepen for an example: