HTML Element Style How to - Vertically center a header on top of an image








Question

We would like to know how to vertically center a header on top of an image.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.image {<!--from   ww w . j  a v  a 2 s. c  om-->
  position: relative;
}

.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
}

h2 {
  margin: 0;
  padding: 0;
}
</style>
</head>
<body>
  <div class="image">
    <img src="http://www.java2s.com/style/download.png" />
    <div class="wrapper">
      <h2>Title</h2>
    </div>
  </div>
</body>
</html>

The code above is rendered as follows: