无法对齐项目 – Bootstrap 4.5

huangapple go评论64阅读模式
英文:

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 -->

&lt;div class=&quot;row align-items-center&quot;&gt;

  &lt;div class=&quot;col-auto&quot;&gt;
    &lt;h6&gt;Alignment Testing&lt;/h6&gt;
  &lt;/div&gt;

  &lt;div class=&quot;col-auto ml-auto&quot;&gt;
    &lt;!-- ml-auto --&gt;
    &lt;div class=&quot;input-group&quot;&gt;
      &lt;span class=&quot;input-group-text&quot; id=&quot;basic-addon1&quot;&gt;&lt;i class=&quot;bi bi-search&quot;&gt;&lt;/i&gt;&lt;/span&gt;
      &lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Alignment Testing&quot; aria-label=&quot;Username&quot; aria-describedby=&quot;basic-addon1&quot;&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css&quot; &gt;

<!-- end snippet -->

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?

答案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 &lt;h6&gt; make mb-0 as h6 occupies margin bottom of 8px

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

<!-- 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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;row &quot;&gt;

    &lt;div class=&quot;col-12 text-center&quot;&gt;
      &lt;h6&gt;Alignment Testing&lt;/h6&gt;
    &lt;/div&gt;

  &lt;/div&gt;
&lt;/div&gt;

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.

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;row &quot;&gt;

    &lt;div class=&quot;col text-center&quot;&gt;
      &lt;h6&gt;Alignment Testing&lt;/h6&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col text-center&quot; &gt;
      second item

  &lt;/div&gt;
&lt;/div&gt;

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:

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row &quot;&gt;
      &lt;div class=&quot;col-12 d-flex align-items-center&quot;&gt;
        &lt;h6 class=&quot;mb-0&quot;&gt;Alignment Testing&lt;/h6&gt;
        &lt;div class=&quot;input-group ml-auto&quot;&gt;
          &lt;span class=&quot;input-group-text&quot; id=&quot;basic-addon1&quot;&gt;&lt;i class=&quot;bi bi-search&quot;&gt;&lt;/i&gt;&lt;/span&gt;
          &lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Alignment Testing&quot; aria-label=&quot;Username&quot; aria-describedby=&quot;basic-addon1&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月5日 18:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定