CSS Layout How to - Shrink image to fit header








Question

We would like to know how to shrink image to fit header.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.parent {<!--   w  w w  . j  a  va2  s  .  c  om-->
  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: