controls of bootstrap 4 carousel not working

I'm learning, and tacher asked us a statis page with a carousel. So i want to use boostrap. The carousel kinda works but i can't figure out how to make it centered, and more importante, the controls doesn't work.

Here is the relevant code :

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- CSS & BOOTSTRAP -->
  <link rel="stylesheet" href="./css/bootstrap-4.5.2-dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/main.css">

  <title>Site Statique</title>

</head>
<body>
  <div class="container">
<!-- ======================================================================== -->
      <!-- CAROUSEL PLACEHOLDER -->
      <!-- ======================================================================== -->

     
      <div class="row">
        <div class="col-md-12">
          <div class="mx-auto">
            <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
              <ol class="carousel-indicators">
                <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
              </ol>

              <div class="carousel-inner">
                <div class="carousel-item active">
                  <img class="img-carousel d-block w-100"
                      src="./img/many_dices_1.png"
                      alt="First slide">
                </div>

                <div class="carousel-item">
                  <img class="img-carousel d-block w-100"
                      src="./img/dice_sheet.jpg"
                      alt="Second slide">
                </div>

                <div class="carousel-item">
                  <img class="img-carousel d-block w-100"
                      src="./img/D_20.webp"
                      alt="Third slide">
                </div>

              </div>
              <a class="carousel-control-prev" href="#carouselExampleIndicators" 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="#carouselExampleIndicators" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
              </a>

            </div>

          </div>
        </div>
      </div>
    <!-- ======================================================================== -->
    <!-- EO CAROUSSEL -->
    <!-- ======================================================================== -->

  <!-- Scripts tags -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="./css/bootstrap-4.5.2-dist/js/bootstrap.min.js"></script>
</body>
</html>

So the carousel works, and images slides, but small images aren't centered, and controls don't work. Any ideas why ? thanks in advance

Update : Now the controls works, but i can't figure out how to make the image to keep ratio. Here is my css rule

.img-carousel{
    width: auto;
    height: 100%;
    max-height: 500px;
}

Upvotes: 0

Views: 569

Answers (1)

Simone Rossaini
Simone Rossaini

Reputation: 8162

I fixed your markup. You confused things a bit:

  • in your example <image> have two class="" that's wrong, just continue the class with a space
  • Button next and prev doesn't have a href="#" so how can work?

Working example:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- CSS & BOOTSTRAP -->
   <!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
  <link rel="stylesheet" href="./css/main.css">

  <title>Site Statique</title>

</head>
<body>
  <div class="container">
<!-- ======================================================================== -->
      <!-- CAROUSEL PLACEHOLDER -->
      <!-- ======================================================================== -->

      <div class="row">
        <div class="col-md-12">
          <div class="mx-auto">
            <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
              <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>

              <div class="carousel-inner">
                <div class="carousel-item active">
                  <img class="img-carousel d-block w-100"
                      src="https://via.placeholder.com/150"
                      alt="First slide">
                </div>

                <div class="carousel-item">
                  <img class="img-carousel d-block w-100"
                      src="https://via.placeholder.com/150"
                      alt="Second slide">
                </div>

                <div class="carousel-item">
                  <img class="img-carousel d-block w-100"
                      src="https://via.placeholder.com/150"
                      alt="Third slide">
                </div>

              </div>
              <a class="carousel-control-prev" href="#carouselExampleIndicators" 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="#carouselExampleIndicators" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
              </a>

            </div>

          </div>
        </div>
      </div>

    <!-- ======================================================================== -->
    <!-- EO CAROUSSEL -->
    <!-- ======================================================================== -->

  <!-- Scripts tags -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</body>
</html>

Upvotes: 1

Related Questions