html - 如何以相同的方式垂直对齐标签和按钮内的图标

标签 html css vertical-alignment

我正在尝试使 ul/li 标签内的图标垂直居中。 根据是否将图标放在按钮标签和标签标签内,我会得到两个不同的结果,并且我无法在标签中对齐。

<ul>
    <li><label class="btn"><i class="fa fa-anchor"></i></label></li>
    <li><button class="btn"><i class="fa fa-anchor"></i></button></li>
    <li><button class="btn"><i class="fa fa-anchor"></i></button></li>
</ul>

CSS:

li{
  display: inline-block;
  text-align: center;
  vertical-align: middle;
  box-sizing: border-box;
  margin: 0;
  border: 1px solid;
}

button,
label{
  position: relative;
  cursor: pointer;
  padding: 0;
  margin: 0;
  background: transparent;
  color: inherit;
  font-size: 16px;
  border: none;
 outline: none;
  width: 41px;
  height: 41px;
  padding: 0 5px;
  margin: 0 2px;
  text-align: center;
  display: inline-block;
  box-sizing: border-box;
}

  button:hover,
  label:hover{
  background-color: #4E84BB;
  color: white;
}

请看 fiddle :http://jsfiddle.net/zbhjco0j/

如何在这两种情况下使图标居中?

谢谢

最佳答案

添加 line-height: 41px;button, label Demo

CSS:

button,
label{
    position: relative;
    cursor: pointer;
    padding: 0;
    margin: 0;
    background: transparent;
    color: inherit;
    font-size: 16px;
    border: none;
    outline: none;
    width: 41px;
    height: 41px;
    line-height: 41px;   /* newly added */
    padding: 0 5px;
    margin: 0 2px;
    text-align: center;
    display: inline-block;
    box-sizing: border-box;
}

关于html - 如何以相同的方式垂直对齐标签和按钮内的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435766/

相关文章:

ios - 为每个 View Controller 分别垂直定位 UINavigationBar 标题

css - 在 Firefox 中垂直对齐大于容器的绝对元素

html - 同一 <div> 的 x 轴和 y 轴滚动条的不同样式

CSS 容器最小高度最大高度声明

html - 链接中的中心文本作为 block

html - 向输入元素添加值会导致它垂直移动

html - 定位表格和多个图像

JavaScript 不会触发 CSS 转换

html - 单击带有 z-index 的定位 div 以在下方提交按钮

jquery - 单击关闭嵌套的 div ul li