在Bootstrap按钮中居中对齐文本。

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

Center align text in Bootstrap button

问题

我在尝试将文本居中放置在Bootstrap按钮内时遇到了困难。以下是我尝试过的代码:

<div class="d-grid gap-2 col-6 mx-auto">
  <a class="btn btn-primary d-flex align-items-center" type="button" style="height: 200px; margin-bottom: 150px; font-size: 3rem; text-align: center; font-weight: bold;" href="all-listings.html">浏览所有列表</a>
</div>

结果如下图所示:

在Bootstrap按钮中居中对齐文本。

有关如何解决这个问题的提示吗?

英文:

I have a hard time centering the text inside a Bootstrap button. Here is what I tried

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

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

&lt;div class=&quot;d-grid gap-2 col-6 mx-auto&quot;&gt;

  &lt;a class=&quot;btn btn-primary d-flex align-items-center&quot; type=&quot;button&quot; style=&quot;height: 200px;margin-bottom: 150px;font-size: 3rem; text-align: center; font-weight: bold;&quot; href=&quot;all-listings.html&quot;&gt; Browse All Listings&lt;/a&gt;

&lt;/div&gt;

<!-- end snippet -->

The result is shown in picture:

在Bootstrap按钮中居中对齐文本。

Any tip on how to fix that?

答案1

得分: 0

style="text-align: center;"添加到容器 div。

<div class="d-grid gap-2 col-6 mx-auto" style="text-align: center;">
  <a class="btn btn-primary d-flex align-items-center" type="button" style="height: 200px; margin-bottom: 150px; font-size: 3rem; text-align: center; font-weight: bold;" href="all-listings.html">浏览所有列表</a>
</div>
英文:

Add style=&quot;text-align: center;&quot; to the container div.

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

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

&lt;div class=&quot;d-grid gap-2 col-6 mx-auto&quot; style=&quot;text-align: center;&quot;&gt;

  &lt;a class=&quot;btn btn-primary d-flex align-items-center&quot; type=&quot;button&quot; style=&quot;height: 200px;margin-bottom: 150px;font-size: 3rem; text-align: center; font-weight: bold;&quot; href=&quot;all-listings.html&quot;&gt; Browse All Listings&lt;/a&gt;

&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

你可以使用一个充当链接的按钮。

&lt;button class=&quot;btn btn-primary&quot; 
style=&quot;height: 200px; margin-bottom: 150px; font-size: 3rem; font-weight: bold;&quot;
onclick=&quot;window.location.href = &#39;all-listings.html&#39;;&quot;&gt;
浏览所有列表
&lt;/button&gt;
英文:

You can use a button that acts as a link instead.

&lt;button class=&quot;btn btn-primary&quot; 
style=&quot;height: 200px; margin-bottom: 150px; font-size: 3rem; font-weight: bold;&quot;
onclick=&quot;window.location.href = &#39;all-listings.html&#39;;&quot;&gt;
Browse All Listings
&lt;/button&gt;

答案3

得分: 0

Bootstrap按钮默认情况下已经将它们的文本居中对齐,所以你只需应用btn btn-primary类,就可以了。不需要添加任何其他内容,只需确保你没有用自己的样式覆盖Bootstrap的样式。

有关更多信息,请查阅Bootstrap官方网站上的文档(https://getbootstrap.com/docs/4.3/components/buttons/)。

英文:

Bootstrap buttons already have their text centered by default so you should be able to just apply the class btn btn-primary and it should do. No need to add anything else, just check that you're not overwriting the bootstrap styles with your own styles.

For more info check the documentation on the bootstrap website.

答案4

得分: 0

&lt;a class=&quot;btn btn-primary text-center&quot; type=&quot;button&quot; style=&quot;height: 200px;margin-bottom: 150px;font-size: 3rem; font-weight: bold;&quot; href=&quot;all-listings.html&quot;&gt; 浏览所有列表&lt;/a&gt;

[您可以在Bootstrap内使用文本实用工具][1]

[1]: https://getbootstrap.com/docs/5.3/utilities/text/
英文:
&lt;a class=&quot;btn btn-primary text-center&quot; type=&quot;button&quot; style=&quot;height: 200px;margin-bottom: 150px;font-size: 3rem; font-weight: bold;&quot; href=&quot;all-listings.html&quot;&gt; Browse All Listings&lt;/a&gt;

You can use text utility within bootstrap

huangapple
  • 本文由 发表于 2023年2月9日 02:42:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390359.html
匿名

发表评论

匿名网友

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

确定