skip to Main Content

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/[email protected]/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?

2

Answers


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

      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/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>
    Login or Signup to reply.
  2. I think you want to do something like this –> https://codepen.io/lynxSven/pen/qBQWVXZ

    <div class="container">
      <div class="row ">
    
        <div class="col-12 text-center">
          <h6>Alignment Testing</h6>
        </div>
    
      </div>
    </div>
    

    When you use col-auto the box is only as big as it needs to be.
    If you want to align text in it you have to give the text some space.

    align-items-center should be used in flex boxes (https://getbootstrap.com/docs/4.0/utilities/flex/)


    Edit alin two items
    If you want to cols to take as much as they need you can always use just class="col" They will then autoalign by percentage.

    <div class="container">
      <div class="row ">
    
        <div class="col text-center">
          <h6>Alignment Testing</h6>
        </div>
        <div class="col text-center" >
          second item
    
      </div>
    </div>
    

    So if you have 3 class="col" on the same node it will give every item they will all have 33% width.
    If you have 2 Nodes one with class="col", the other class="col-1"
    The class="col" will be like a "col-11"

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