Chris Beechey
Chris Beechey

Reputation: 65

bootstrap carousel with mobile size

am trying to add a carousel to this page, so when it is viewed on a mobile device the carousel adjusts, currently it squeezes down to size and is un viewable (sorry its probably simply, but this is my first rodeo with bootstrap!)

html:

 <!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img class="first-slide" src="imgs/facebook%20page.jpg" alt="First slide">
      <div class="container">
        <!--<div class="carousel-caption">
          <h1>Example headline.</h1>
          <p>Note: If you're viewing this page via a <code>file://</code> URL, the "next" and "previous" Glyphicon buttons on the left and right might not load/display properly due to web browser security rules.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Sign up today</a></p>
        </div>-->
      </div>
    </div>
    <div class="item">
      <img class="second-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Second slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>Another example headline.</h1>
          <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Learn more</a></p>
        </div>
      </div>
    </div>
    <div class="item">
      <img class="third-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Third slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>One more for good measure.</h1>
          <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Browse gallery</a></p>
        </div>
      </div>
    </div>
  </div>
  <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div><!-- /.carousel -->


/* CUSTOMIZE THE NAVBAR

-------------------------------------------------- */

   /* Special class on .container surrounding .navbar, used for positioning it into place. */
    .navbar-wrapper {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
  z-index: 20;
}

    /* Flip around the padding for proper display in narrow viewports */
    .navbar-wrapper > .container {
      padding-right: 0;
      padding-left: 0;
    }
    .navbar-wrapper .navbar {
     padding-right: 15px;
  padding-left: 15px;
}
.navbar-wrapper .navbar .container {
  width: auto;
}


/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */

/* Carousel base class */
.carousel {
  height: 500px;
  margin-bottom: 60px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
  z-index: 10;
}

/* Declare heights because of positioning of img element */
.carousel .item {
  height: 500px;
  background-color: #777;
}
.carousel-inner > .item > img {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  height: 500px;
}

I've probably bombarded the question with too much code...such a noob!

Thanks in advance

Upvotes: 1

Views: 3118

Answers (1)

Steve K
Steve K

Reputation: 9045

So what I like to do with bootstrap carousels are give the item class a pading-bottom of a percentage instead of giving a height then play with this percentage untill you get the desired height. This will make them responsive and resize with the screen. Then instead of putting the image inside of the carousel give each carousel item another class like first-item, second-item, third-item and so forth. Then give this class a background image in your css. You can give the class of item a background-size:cover; and a background position: center;. Then instead of having an h1 tag in your carousel I like to just put a p tag and give it a class of carousel heading and then in your other p tag give it a class of carousel description. The reason I don't like to put h1 tags in the carousel is because there should be only one h1 tag on your page and you can specify that later but you if you want the h1 tag in the carousel just give it a class of carousel heading. The you can resize the text in these elements at samller screens in your css. So hopefully all of that made sense.

Check out this JSFiddle and drag the display window so you can see it resize- JSFiddle

Then here is your html and css:

Your Css:

#myCarousel .item{
  padding-bottom: 50%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  min-height: 250px;
}
#myCarousel .first-item{
  background-image: url('http://placehold.it/500x500');
}
#myCarousel .second-item{
  background-image: url('http://placehold.it/500x500');
}
#myCarousel .third-item{
  background-image: url('http://placehold.it/500x500');
}
#myCarousel .carousel-heading{font-size: 40px;}
@media screen and (max-width: 767px){
    #myCarousel .carousel-heading{font-size: 18px;}
    #myCarousel .carousel-description{font-size: 12px;}
    #myCarousel .btn-primary{font-size: 14px;}
}

And Your Html:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner" role="listbox">
    <div class="item active first-item">
      <div class="container">
        <div class="carousel-caption">
          <p class="carousel-heading">Example Heading</p>
          <p class="carousel-description">Note: If you're viewing this page via a <code>file://</code> URL, the "next" and "previous" Glyphicon buttons on the left and right might not load/display properly due to web browser security rules.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Sign up today</a></p>
        </div>
      </div>
    </div>
    <div class="item second-item">
      <div class="container">
        <div class="carousel-caption">
          <p class="carousel-heading">Example Heading</p>
          <p class="carousel-description">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Learn more</a></p>
        </div>
      </div>
    </div>
    <div class="item third-item">
      <div class="container">
        <div class="carousel-caption">
          <p class="carousel-heading">Example Heading</p>
          <p class="carousel-description">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Browse gallery</a></p>
        </div>
      </div>
    </div>
  </div>
  <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div><!-- /.carousel -->

Upvotes: 1

Related Questions