CSS Layout How to - Place header on image top








Question

We would like to know how to place header on image top.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.parent {<!--   w w  w .j  a  v  a2 s . co m-->
  display: inline;
  position: relative;
}

.parent img {
  position: absolute;
  top: 0;
  left: 0;
}

.parent h1 {
  display: inline;
  position: relative;
  /* this makes sure the h1 is in front of img in stacking order */
  border: 1px dotted lightgray;
  padding: 0 10px; /* optional, tweak left/right as needed */
  vertical-align: top;
  line-height: 68px;
}
</style>
</head>
<body>
  <div class="parent">
    <img src="http://placehold.it/200x200"
      width="100%" height="68" />
    <h1>Variable length text</h1>
  </div>
</body>
</html>

The code above is rendered as follows: