HTML Element Style How to - Place image in middle of div with overflow hidden








Question

We would like to know how to place image in middle of div with overflow hidden.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.main {<!--  www.  ja va 2s  .co  m-->
  width: 150px;
  margin: 0 auto;
  overflow: visible;
  position: relative;
  height: 200px;
  outline: 1px solid blue;
}

img.absolute {
  width: 200px;
  left: 50%;
  margin-left: -100px;
  margin-top: 0;
  position: relative;
  outline: 1px solid red;
}
</style>
</head>
<body>
  <div class="main">
    <img class="absolute" src="http://www.java2s.com/style/download.png" alt="" />
  </div>
</body>
</html>

The code above is rendered as follows: