How to center items inside a col horizontally in Bootstrap 5

Issue

I have created a Grid system with just 2 columns. The first one is text and in the second one I have included a carousel. I have set the image size to custom for the carousel image. No matter what I do I am unable to center the carousel image inside my col to the center horizontally.

.title-sec {
  color: #fff;
  padding-top: 100px;
  padding-bottom: 100px;
}

.carousel {
  width: 400px;
  height: auto;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="container title-sec">
  <div class="row">
    <div class="col text-center my-auto">
      <h1 class="heading1"> Sample Text</h1>
      <h2 class="heading2">Sample Breifing</h2>
    </div>
    
    <div class="col m-auto">
      <div id="carouselExampleFade" class="carousel slide carousel-fade" data-bs-ride="carousel">
        <div class="carousel-inner">
          <div class="carousel-item active">
            <img src="https://via.placeholder.com/300" class="d-block w-100" alt="...">
          </div>
          
          <div class="carousel-item">
            <img src="https://via.placeholder.com/300" class="d-block w-100" alt="...">
          </div>
          
          <div class="carousel-item">
            <img src="https://via.placeholder.com/300" class="d-block w-100" alt="...">
          </div>
        </div>
        
        <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleFade" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Previous</span>
         </button>

         <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleFade" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Next</span>
        </button>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

Solution

You need to apply centering to the carousel, not the container (unless you apply flexbox, which isn’t really needed).

I put .mx-auto on .carousel.

.title-sec {
  color: #fff;
  padding-top: 100px;
  padding-bottom: 100px;
}

.carousel {
  width: 400px;
  height: auto;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="container title-sec">
  <div class="row">
    <div class="col text-center my-auto">
      <h1 class="heading1"> Sample Text</h1>
      <h2 class="heading2">Sample Breifing</h2>
    </div>
    
    <div class="col bg-secondary">
      <div id="carouselExampleFade" class="carousel slide carousel-fade mx-auto" data-bs-ride="carousel">
        <div class="carousel-inner">
          <div class="carousel-item active">
            <img src="https://via.placeholder.com/600" class="d-block w-100" alt="...">
          </div>
          
          <div class="carousel-item">
            <img src="https://via.placeholder.com/600" class="d-block w-100" alt="...">
          </div>
          
          <div class="carousel-item">
            <img src="https://via.placeholder.com/600" class="d-block w-100" alt="...">
          </div>
        </div>
        
        <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleFade" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Previous</span>
         </button>

         <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleFade" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Next</span>
        </button>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

Answered By – isherwood

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.