【问题标题】:How to create a bootstrap circled button with the text centered中心数字的引导圆形按钮[重复]
【发布时间】:2021-04-03 00:59:45
【问题描述】:

我正在尝试为圆形按钮添加一个数字按钮。我可以做到,但文本(数字不居中)。理想情况下,如果我有 1 到 15 个圆形 15 个按钮,从 1 到 15

代码如下

body {
  padding: 1em;
}
<link
  href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"
  rel="stylesheet"
  id="bootstrap-css"
>


<button type="button" class="btn btn-info btn-circle btn-lg">100</button>

以上代码来源于此link

【问题讨论】:

    标签: css bootstrap-4


    【解决方案1】:

    一个非常简单的类将解决你的问题-

    <button type="button" class="btn btn-info p-5 rounded-circle btn-lg">100</button>
    

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
      <style>
        body {
      padding: 1em;
    }
      </style>
      
    </head>
    <body>
    
    <button type="button" class="btn btn-info p-5 rounded-circle btn-lg">100</button>
    
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      您必须包含 .btn-circle 的 CSS 才能获得圆角。我从您提供的链接中复制了 CSS 并删除了所有填充,因此文本居中。这将导致:

      .btn-circle {
        width: 30px;
        height: 30px;
        text-align: center;
        padding: 0; // changed
        font-size: 12px;
        line-height: 1.428571429;
        border-radius: 15px;
      }
      .btn-circle.btn-lg {
        width: 50px;
        height: 50px;
        padding: 0; // changed
        font-size: 18px;
        line-height: 1.33;
        border-radius: 25px;
      }
      .btn-circle.btn-xl {
        width: 70px;
        height: 70px;
        padding: 0; // changed
        font-size: 24px;
        line-height: 1.33;
        border-radius: 35px;
      }
      <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
      <button type="button" class="btn btn-info btn-circle btn-lg">100</button>

      【讨论】:

        【解决方案3】:

        这些更改将产生带有居中文本的圆形按钮:

        1. 包括您提供的链接中的.btn-circle css 规则,

        2. 将按钮文本包装在 span 中,并给 span 负边距。

        body {
          padding: 1em;
        }
        
        .btn-circle {
          width: 30px;
          height: 30px;
          text-align: center;
          padding: 6px 0;
          font-size: 12px;
          line-height: 1.428571429;
          border-radius: 15px;
        }
        
        .btn-circle.btn-lg {
          width: 50px;
          height: 50px;
          padding: 10px 16px;
          font-size: 18px;
          line-height: 1.33;
          border-radius: 25px;
        }
        
        .btn-circle.btn-xl {
          width: 70px;
          height: 70px;
          padding: 10px 16px;
          font-size: 24px;
          line-height: 1.33;
          border-radius: 35px;
        }
        
        
        /* use negative margins to accommodate wider button text */
        
        .btn-circle span {
          margin: 0 -2rem;
        }
        <link
          href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"
          rel="stylesheet"
          id="bootstrap-css"
        >
        
        <button
          type="button"
          class="btn btn-info btn-circle btn-lg"
        ><span>100</span></button>

        【讨论】:

          猜你喜欢
          • 2016-12-30
          • 2021-10-05
          • 1970-01-01
          • 2015-08-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-06
          • 2016-11-16
          相关资源
          最近更新 更多