【问题标题】:CSS Button - Vertically centre textCSS 按钮 - 垂直居中文本
【发布时间】:2012-01-05 17:07:58
【问题描述】:

我有一个按钮,它是一个简单的锚标记,样式如下 -

.buyBtn{ 
   background:url(../images/buyBtn.png) no-repeat; 
   padding-top:4px; 
   width:97px;     
   height:28px; 
   margin-top:14px;
}
.buyBtn a{
   color:#fff!important; 
   font-weight:normal!important; 
   text-decoration:none;
   padding-left:27px;  
   padding-top:12px; 
   text-shadow:none!important;
}

我在将按钮内的文本垂直居中时遇到问题,它在某些设备上看起来不错,但在其他设备上却偏离了中心。

谁能推荐一种解决此问题的方法或更好的解决方案以达到相同的结果?

干杯

【问题讨论】:

    标签: css cross-browser


    【解决方案1】:

    使用 line-height 使其垂直居中。我通常使用与其高度相同的值。

    【讨论】:

    • 应该是国际海事组织接受的答案。晶莹剔透,没有破解
    • 只有在知道高度时才有效。
    • 我将height 设置为0line-height 设置为零,这有效!
    • 我使用了height: <some_value>,然后设置了line-height:0,它已经奏效了。
    • 我相信这只有在只有一行文本时才有效 - 如果我错了,请纠正我。
    【解决方案2】:

    您是否尝试为链接设置字体大小?每个浏览器可能都有自己的默认大小,所以这可能是一个提示。也要小心填充和宽度/高度,如果添加填充,则需要减小块大小,因为填充包含在宽度中。例如,一个没有填充的 100px 宽度的简单块将具有 100px 的大小:好的。添加padding-left: 10px;,您的块现在的宽度为 110 像素。 ;)

    【讨论】:

      【解决方案3】:

      我会使用line-height 作为上面提到的 bchhun。另外,padding-toppadding-bottom 可以提供帮助。

      【讨论】:

        【解决方案4】:

        HTML:

        <div class="buyBtn"><a href="#">Button</a></div>
        

        CSS:

        .buyBtn{ 
            background:url(../images/buyBtn.png) no-repeat; 
            width:97px;     
            height:28px; 
            display: table-cell;
            vertical-align: middle;
        }
        
        .buyBtn a{
            color:#fff!important; 
            font-weight:normal!important; 
            text-decoration:none;
            padding-left:27px;
            text-shadow:none!important;
        }
        

        无需使用padding-topmargin-top 进行垂直对齐。只需使用display: table-cell;vertical-align: middle;。就是这样。

        【讨论】:

        • 您不能添加边距来定位表格单元格元素,因此如果您需要使用边距定位 .buyBtn,此技术的应用会受到限制。
        【解决方案5】:

        您可以复制此代码并将其作为 CSS 并根据需要进行调整

        .btn-alignment{
            width: 88px;
            height: 40px;
            margin: 0 auto;
            padding: 0;
            display: inline-block;
            line-height: 40px;
            margin-top: 9px;
            text-align: center;
        }
        

        【讨论】:

          【解决方案6】:

          Flexbox 帮我搞定了。假设您有一个 excel 按钮(实际上是一个锚标记)。

          HTML

          <a class="btn-excel" href="/Export/ExportListToExcel">
              Export To Excel
          </a>
          

          CSS

          .btn-excel {
              background-color: #32CD32; /*lime green*/
              color: #fff;
              outline: none;
              border-radius: 0.3rem;
              text-decoration: none;
              width: 5rem;
              height: 2.5rem;
              /* Flex rules 'em all */
              display: flex;
              justify-content: center;
              align-items: center;
          }
          .btn-excel:hover {
              background-color: #808000; /*olive*/
              cursor: pointer;
              text-decoration: none;
          }
          

          【讨论】:

          • 如果您希望按钮并排放置,可以使用 display: inline-flex。
          猜你喜欢
          • 2013-06-04
          • 2012-02-05
          • 2014-10-22
          • 2011-12-16
          • 2013-07-31
          • 2021-10-06
          • 2023-03-05
          • 1970-01-01
          • 2013-05-25
          相关资源
          最近更新 更多