Can't align items – Bootstrap 4.5

I’m trying to align two objects in HTML using Bootstrap, but it’s not working. The code I’m using is this:

<div class="row align-items-center">

  <div class="col-auto">
    <h6>Alignment Testing</h6>
  </div>

  <div class="col-auto ml-auto">
    <!-- ml-auto -->
    <div class="input-group">
      <span class="input-group-text" id="basic-addon1"><i class="bi bi-search"></i></span>
      <input type="text" class="form-control" placeholder="Alignment Testing" aria-label="Username" aria-describedby="basic-addon1">
    </div>
  </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" >

Image of the problem

If I change the "items-align-center" to "items-align-start/end", the text (h6) changes position slightly. The interesting thing is that I applied the "items-align-center" in other places and it worked perfectly. Any idea what it could be? I saw some topics mentioning that the div must have a height, I already tried to put a height and it also did not align in the center.

Any ideas?

>Solution :

In your <h6> make mb-0 as h6 occupies margin bottom of 8px

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<div class="row align-items-center">
    <div class="col-auto">
        <h6 class="mb-0">Alignment Testing</h6>
    </div>            
    <div class="col-auto ml-auto">
        <div class="input-group">
            <span class="input-group-text" id="basic-addon1"><i class="bi bi-search"></i></span>
            <input type="text" class="form-control" placeholder="Alignment Testing" aria-label="Username" aria-describedby="basic-addon1">
        </div>
    </div>
</div>

Leave a Reply