【问题标题】:How to horizontally center text in bootstrap button?如何在引导按钮中水平居中文本?
【发布时间】:2016-11-02 01:27:00
【问题描述】:

我正在开发一个简单的寻呼机(在 Angular 2 中),但我的样式有问题。我在页面设计中使用引导程序,在寻呼机中使用引导程序按钮。我正在使用line-height 将文本垂直居中,效果很好,但我不知道如何将按钮中的文本水平居中。文本始终显示在右侧。我试过text-align: center,我试过引导类text-center,但文字仍然在右边。

我的html很简单:

<div class="pager">
    <button class="btn btn-sm" *ngFor="let page of pages">{{page}}</button>
</div>

(对于那些不了解 Angular 2 的人:这将遍历 pages 数组并创建与数组具有值的按钮一样多的按钮,并将这些值用作按钮文本)

我的css(sass):

.pager {
    display: inline-block;
    margin-left: 8px;

    button {
        max-height: 18px;
        max-width: 18px;
        margin-left: 2px;
        background: $topBarControlBackground;
        color: $topBarColor;
        line-height: 50%;
        font-size: 80%;
    }
}

结果如下:

.pager {
    display: inline-block;
    margin-left: 8px;

    button {
        max-height: 18px;
        max-width: 18px;
        margin-left: 2px;
        background: $topBarControlBackground;
        color: $topBarColor;
        line-height: 50%;
        font-size: 80%;
    }
}
<div class="pager">
    <button class="btn btn-sm" *ngFor="let page of pages">{{page}}</button>
</div>

【问题讨论】:

  • 更新你的js代码
  • 这里没有任何东西可以工作,因为它是 Angular 2。请删除“运行代码 sn-p”按钮。在这种情况下没有意义。 js 根本不涉及按钮的样式。
  • 你试过 text-align: center !important 吗?
  • 我没有,但我只是做了,没有任何区别。
  • 尝试用 padding 替换 margin-left,我也会创建一个 plnkr 而不是代码 sn-p

标签: css twitter-bootstrap-3 sass


【解决方案1】:

btnbtn-sm 上的填充会弄乱文本对齐。您的按钮太小而无法容纳填充。设置padding:0 5px; 对我有用。见密码笔here

或者(不覆盖填充),您可以将btn-xs 类用于超小按钮。

【讨论】:

  • 谢谢。你非常亲近。将左右填充设置为 0 并使用 btn-xs 修复它。
猜你喜欢
  • 2016-04-23
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 2013-03-02
  • 2017-11-27
  • 2018-09-18
  • 1970-01-01
相关资源
最近更新 更多