skip to Main Content

I used some code from another site to create a Bootstrap 5 carousel. Unfortunately, my carousel left aligns and I would like it to be in the center of the container.

<div class="container-lg my-3">
<div id="myCarousel" class="carousel slide" data-bs-ride="carousel">

    <!-- Carousel indicators -->
    <ol class="carousel-indicators">
        <li data-bs-target="#myCarousel" data-bs-slide-to="0" class="active"></li>
        <li data-bs-target="#myCarousel" data-bs-slide-to="1"></li>
        <li data-bs-target="#myCarousel" data-bs-slide-to="2"></li>
        <li data-bs-target="#myCarousel" data-bs-slide-to="3"></li>
        <li data-bs-target="#myCarousel" data-bs-slide-to="4"></li>
        <li data-bs-target="#myCarousel" data-bs-slide-to="5"></li>
    </ol>
   
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img src="images/carousel/rt4.jpg" class="d-block w-75" alt="Slide 1">
        </div>
        <div class="carousel-item">
            <img src="images/carousel/rt2.jpg" class="d-block w-75" alt="Slide 2">
        </div>
        <div class="carousel-item">
            <img src="images/carousel/rt3.jpg" class="d-block w-75" alt="Slide 3">
        </div>
        <div class="carousel-item">
            <img src="images/carousel/rt1.jpg" class="d-block w-75" alt="Slide 4">
        </div>
        <div class="carousel-item">
            <img src="images/carousel/rt7.jpg" class="d-block w-75" alt="Slide 5">
        </div>
            <div class="carousel-item">
            <img src="images/carousel/rt8.jpg" class="d-block w-75" alt="Slide 6">
        </div>
    </div> <!-- Close carousel inner -->

    <!-- Carousel controls -->
    <a class="carousel-control-prev" href="#myCarousel" data-bs-slide="prev">
        <span class="carousel-control-prev-icon"></span>
    </a>
    <a class="carousel-control-next" href="#myCarousel" data-bs-slide="next">
        <span class="carousel-control-next-icon"></span>
    </a>
</div> <!-- Close Carousel div -->

Any suggestions on how to center this?

2

Answers


  1. Add mx-auto margin utility class to each image, see: Aligning images

    .carousel-item {
    background:skyblue;
    
    }
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap demo</title>
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
      </head>
      <body>
        <h1>Hello, world!</h1>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
        
        
        <div class="container-lg my-3">
    <div id="myCarousel" class="carousel slide" data-bs-ride="carousel">
    
        <!-- Carousel indicators -->
        <ol class="carousel-indicators text-primary">
            <li data-bs-target="#myCarousel" data-bs-slide-to="0" class="active"></li>
            <li data-bs-target="#myCarousel" data-bs-slide-to="1"></li>
            <li data-bs-target="#myCarousel" data-bs-slide-to="2"></li>
            <li data-bs-target="#myCarousel" data-bs-slide-to="3"></li>
            <li data-bs-target="#myCarousel" data-bs-slide-to="4"></li>
            <li data-bs-target="#myCarousel" data-bs-slide-to="5"></li>
        </ol>
       
        <div class="carousel-inner">
            <div class="carousel-item active">
                <img src="https://placehold.co/600x400" class="d-block w-75 mx-auto" alt="Slide 1">
            </div>
            <div class="carousel-item">
                <img src="https://placehold.co/600x400" class="d-block w-75 mx-auto" alt="Slide 2">
            </div>
            <div class="carousel-item">
                <img src="https://placehold.co/600x400" class="d-block w-75 mx-auto" alt="Slide 3">
            </div>
            <div class="carousel-item">
                <img src="https://placehold.co/600x400" class="d-block w-75 mx-auto" alt="Slide 4">
            </div>
            <div class="carousel-item">
                <img src="https://placehold.co/600x400" class="d-block w-75 mx-auto" alt="Slide 5">
            </div>
                <div class="carousel-item">
                <img src="https://placehold.co/600x400" class="d-block w-75 mx-auto" alt="Slide 6">
            </div>
        </div> <!-- Close carousel inner -->
    
        <!-- Carousel controls -->
        <a class="carousel-control-prev" href="#myCarousel" data-bs-slide="prev">
            <span class="carousel-control-prev-icon"></span>
        </a>
        <a class="carousel-control-next" href="#myCarousel" data-bs-slide="next">
            <span class="carousel-control-next-icon"></span>
        </a>
    </div> <!-- Close Carousel div -->
    
    
    
    
      </body>
    </html>
    Login or Signup to reply.
  2. I think you should change the img tag class to w-100.
    if you want to img tag to have class d-75 you should add margin-auto to the img tag.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search