HTML Element Style How to - Vertically center an image with an anchor inside a div








Question

We would like to know how to vertically center an image with an anchor inside a div.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.linkgroup {<!--  w  w w .  jav a 2  s  .  c  o m-->
  height: 100px;
  width: 500px;
  border: 1px solid red;
}

span {
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

img {
  background: #3A6F9A;
  border: 1px solid red;
  vertical-align: middle;
  height: 40px;
}

a {
  vertical-align: middle;
  display: inline-block;
  border: 1px solid red;
}
</style>
</head>
<body>
  <ul>
    <div class="linkgroup">
      <span class="helper"></span> 
      <img src="http://www.java2s.com/style/download.png" /> 
      <a href="">test</a>
    </div>
  </ul>
</body>
</html>

The code above is rendered as follows: