英文:
Can't align items - Bootstrap 4.5
问题
我正在尝试使用Bootstrap在HTML中对齐两个对象,但它不起作用。我正在使用的代码是这样的:
<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>
如果我将"items-align-center"更改为"items-align-start/end",文本(h6)的位置会略有变化。有趣的是,我在其他地方应用了"items-align-center",它完美地起作用。有任何想法是什么原因吗?我看到一些帖子提到div必须有一个高度,我已经尝试设置高度,但它仍然无法在中间对齐。有什么想法吗?
英文:
I'm trying to align two objects in HTML using Bootstrap, but it's not working. The code I'm using is this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<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" >
<!-- end snippet -->
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?
答案1
得分: 1
在您的代码中,在<h6>
标签上添加mb-0
类,以使h6
的底部外边距为8px
。
<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>
英文:
In your <h6>
make mb-0
as h6
occupies margin bottom of 8px
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<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>
<!-- end snippet -->
答案2
得分: 1
我认为你想要做类似这样的事情 --> https://codepen.io/lynxSven/pen/qBQWVXZ
对齐测试
当你使用col-auto时,框的大小只会根据需要而定。
如果你想要对齐其中的文本,你需要为文本留出一些空间。
align-items-center应该在 flex boxes 中使用(https://getbootstrap.com/docs/4.0/utilities/flex/)
编辑两个项目的对齐
如果你想让列自动占用所需的空间,你可以始终使用class="col"。它们将自动按百分比对齐。
对齐测试
所以,如果在同一个节点上有3个class="col",它们将都具有33%的宽度。
如果有2个节点,一个使用class="col",另一个使用class="col-1",
那么class="col"将会像"col-11"一样。
英文:
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"
答案3
得分: 1
使用Bootstrap类以最少的代码行数:
<div class="container">
<div class="row">
<div class="col-12 d-flex align-items-center">
<h6 class="mb-0">对齐测试</h6>
<div class="input-group ml-auto">
<span class="input-group-text" id="basic-addon1"><i class="bi bi-search"></i></span>
<input type="text" class="form-control" placeholder="对齐测试" aria-label="用户名" aria-describedby="basic-addon1">
</div>
</div>
</div>
</div>
请注意,我已经将"Alignment Testing"翻译为"对齐测试"以保持上下文一致。
英文:
Using of bootstrap class with minimum line of code:
<div class="container">
<div class="row ">
<div class="col-12 d-flex align-items-center">
<h6 class="mb-0">Alignment Testing</h6>
<div class="input-group ml-auto">
<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>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论