【问题标题】:vertical-align:middle for text in button垂直对齐:按钮中的文本中间
【发布时间】:2012-12-03 19:20:10
【问题描述】:

我有这个布局:

我的 CSS

body {
     background: #e2eaed;
}
a {
     text-decoration: none;
     color: #fff;
     font-size: 30px;
     height: 62px;
     line-height: 62px;
     /* vertical-align: middle is not works  */
     background: #8dc73f;
     width: 132px;
     padding: 0 25px;
     font-size: 16px;
     text-align: center;
     font-weight: bold;
     margin-left: 4px;
     display: block;
     float: left;
}

​当按钮有 1 行文本时,我的代码运行良好。但是当按钮有 2 行文本时,如上图所示。代码文本的高度很大,因为我使用了line-height 属性。我试过vertical-align,但它不起作用。

请参阅jsfiddle

【问题讨论】:

    标签: button vertical-alignment css


    【解决方案1】:

    另一种方法是使用flexible boxes:

    a {
      display: inline-flex;
      align-items: center; /* cross axis */
      justify-content: center; /* main axis */
    
      line-height: 1; /* reset */
    }
    

    您可能需要添加前缀,请参阅browser supportfiddle

    【讨论】:

      【解决方案2】:

      垂直对齐仅影响显示为表格单元格(或内联块,但对后面的效果不同)的元素。这些元素不能浮动。

      a {
        display: table-cell;
        vertical-align: middle;
      
        /* Reset */
        float: none;
        line-height: normal;
      }
      

      【讨论】:

      • @Ozerich 表格单元格没有边距。您可以改用透明边框。
      猜你喜欢
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2019-07-01
      • 2017-01-17
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 2017-06-29
      相关资源
      最近更新 更多