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" >
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
In your
<h6>
makemb-0
ash6
occupies margin bottom of8px
I think you want to do something like this –> https://codepen.io/lynxSven/pen/qBQWVXZ
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.
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"