CSS Layout How to - Horizontal center image in div with absolute position








Question

We would like to know how to horizontal center image in div with absolute position.

Answer


<!DOCTYPE html>
<html>
<head>
<style>
img {<!-- w w w .  j  a v a2 s  . c o m-->
  width: 20%;
  display: block;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}
</style>
</head>
<body>
  <img src="http://www.java2s.com/style/download.png">
</body>
</html>

The code above is rendered as follows: