我有这个布局:
我的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。
答案 0 :(得分:12)
另一种方法是使用flexible boxes:
a {
display: inline-flex;
align-items: center; /* cross axis */
justify-content: center; /* main axis */
line-height: 1; /* reset */
}
您可能需要添加前缀,请参阅browser support和fiddle。
答案 1 :(得分:6)
垂直对齐仅影响显示为表格单元格的元素(或内联块,但后面的效果不同)。这些元素不得浮动。
a {
display: table-cell;
vertical-align: middle;
/* Reset */
float: none;
line-height: normal;
}