Search code examples
csstwitter-bootstrap

How can make bootstrap carousel center


Good day, I' using bootstrap 3 and trying to make my bootstrap carousel in center. Here is the script which i try so far.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>

 .navbar.transparent.navbar-inverse .navbar-inner {
    border-width: 0px;
    -webkit-box-shadow: 0px 0px;
    box-shadow: 0px 0px;
    background-color: rgba(0,0,0,0.0);
    background-image: -webkit-gradient(linear, 50.00% 0.00%, 50.00% 100.00%, color-stop( 0% , rgba(0,0,0,0.00)),color-stop( 100% , rgba(0,0,0,0.00)));
    background-image: -webkit-linear-gradient(270deg,rgba(0,0,0,0.00) 0%,rgba(0,0,0,0.00) 100%);
    background-image: linear-gradient(180deg,rgba(0,0,0,0.00) 0%,rgba(0,0,0,0.00) 100%);
  }
  #Intro {
      display: inline-block;
  }
  #topnavbar {
    top: 20px;
   }

.carousel {
  width:600px;
  height:400px;
}
.carousel-inner > .item > img {
    margin: 0 auto;
}
div.c-wrapper{
    width: 100%; /* for example */
    margin: auto;
}

.carousel-inner > .item > img, 
.carousel-inner > .item > a > img{
width: 100%; /* use this, or not */
margin: auto;
}
.container{
  border: 1px;
}

  </style>
</head>
<body>
   <div class="container" style="border:1px solid #cecece;">

<div id="Intro" class="col-md-offset-5">
  LOGO
</div>

    <nav id="topnavbar" class="navbar transparent">
        <div class="navbar-header">
            <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            
        </div>
        <!-- Collection of nav links, forms, and other content for toggling -->
        <div id="navbarCollapse" class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">About Us</a></li>
                <li><a href="#">Product</a></li>
                <li><a href="#">Promo</a></li>
                <li><a href="#">Competition</a></li>
                <li><a href="#">Ladies Room</a></li>
                <li><a href="#">Need Help?</a></li>


            </ul>
          
        </div>
    </nav>
    <div class="c-wrapper">
 <div id="myCarousel" class="carousel slide">
<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>
<!-- Carousel items -->
<div class="carousel-inner">
  <div class="active item">
      <img src="https://dummyimage.com/600x400/000/fff">
  </div>
  <div class="item">
     <img src="http://via.placeholder.com/350x150">
  </div>
  <div class="item"> 
      <img src="http://via.placeholder.com/350x150">
  </div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
 </div>
</div>

</body>
</html>

But, with my script above my Caraousel still in the left of screen. then i try to searching with google and i find this https://stackoverflow.com/questions/19582340/make-bootstraps-carousel-both-center-and-responsive but still no help . Any solution ? thanks in advance and sorry for my bad english


Solution

  • You're simply missing one line of code:

    .carousel {
      margin: 0 auto;
    }
    

    The carousel is a <div>, which is a block-level element, so you centralise it by setting the horizontal margins to auto. The shorthand margin: 0 auto sets the top and bottom margins to 0 while setting the horizontal margins to auto.

    Here's a working example (click 'Full page' after running the snippet):

    .navbar.transparent.navbar-inverse .navbar-inner {
      border-width: 0px;
      -webkit-box-shadow: 0px 0px;
      box-shadow: 0px 0px;
      background-color: rgba(0, 0, 0, 0.0);
      background-image: -webkit-gradient(linear, 50.00% 0.00%, 50.00% 100.00%, color-stop( 0%, rgba(0, 0, 0, 0.00)), color-stop( 100%, rgba(0, 0, 0, 0.00)));
      background-image: -webkit-linear-gradient(270deg, rgba(0, 0, 0, 0.00) 0%, rgba(0, 0, 0, 0.00) 100%);
      background-image: linear-gradient(180deg, rgba(0, 0, 0, 0.00) 0%, rgba(0, 0, 0, 0.00) 100%);
    }
    
    #Intro {
      display: inline-block;
    }
    
    #topnavbar {
      top: 20px;
    }
    
    .carousel {
      width: 600px;
      height: 400px;
      margin: 0 auto;
    }
    
    .carousel-inner>.item>img {
      margin: 0 auto;
    }
    
    div.c-wrapper {
      width: 100%;
      /* for example */
      margin: auto;
    }
    
    .carousel-inner>.item>img,
    .carousel-inner>.item>a>img {
      width: 100%;
      /* use this, or not */
      margin: auto;
    }
    
    .container {
      border: 1px;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    
    <body>
      <div class="container" style="border:1px solid #cecece;">
        <div id="Intro" class="col-md-offset-5">
          LOGO
        </div>
        <nav id="topnavbar" class="navbar transparent">
          <div class="navbar-header">
            <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
               <span class="icon-bar"></span>
            </button>
          </div>
          <!-- Collection of nav links, forms, and other content for toggling -->
          <div id="navbarCollapse" class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#">About Us</a></li>
              <li><a href="#">Product</a></li>
              <li><a href="#">Promo</a></li>
              <li><a href="#">Competition</a></li>
              <li><a href="#">Ladies Room</a></li>
              <li><a href="#">Need Help?</a></li>
            </ul>
          </div>
        </nav>
        <div class="c-wrapper">
          <div id="myCarousel" class="carousel slide">
            <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>
            <!-- Carousel items -->
            <div class="carousel-inner">
              <div class="active item">
                <img src="https://dummyimage.com/600x400/000/fff">
              </div>
              <div class="item">
                <img src="http://via.placeholder.com/350x150">
              </div>
              <div class="item">
                <img src="http://via.placeholder.com/350x150">
              </div>
            </div>
            <!-- Carousel nav -->
            <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
            <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
          </div>
        </div>
      </div>
    </body>
    
    </html>

    Hope this helps! :)