HTML Element Style How to - Centre an image inside a div








Question

We would like to know how to centre an image inside a div.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#footer img.display {<!--from   w  w w.j ava2 s . c  o m-->
  display: block;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
  height: 50px;
  width: 300px;
}
</style>
</head>
<body>
  <div id="footer">
    <img src="http://www.java2s.com/style/download.png" class="display" />
  </div>
</body>
</html>

The code above is rendered as follows: