CSS Layout How to - Vertically and Horizontally centered image








Question

We would like to know how to vertically and Horizontally centered image.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
a {<!-- w  ww  .j av  a 2 s . c om-->
  height: 180px;
  width: 150px;
  display: block;
  text-align: center;
  border: 1px solid black;
  margin: 10px;
  line-height: 180px;
}

a img {
  vertical-align: middle;
}
</style>
</head>
<body>
  <a> <img
    src="http://www.java2s.com/style/download.png"
    alt="Centre Me!" />
  </a>
</body>
</html>

The code above is rendered as follows: