Search code examples
htmlcssvertical-alignment

How to vertical align icon inside label and button in the same way


I'm trying to center vertically an icon inside an ul/li tag. Depending if placing the icon inside a button tag and label tag I get two different results and I can not get the alignment in the label working.

<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;
}

Please see the fiddle: http://jsfiddle.net/zbhjco0j/

How do I center the icon in both scenario?

thanks


Solution

  • ADD line-height: 41px; to 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;
    }