CSS Layout How to - Vertically align inline elements








Question

We would like to know how to vertically align inline elements.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
p {<!-- w  w w.j  a v  a  2  s.c  om-->
  margin: 0;
  padding: 1em 0;
  font-size: 1.8em;
  line-height: 1.5;
}

.first {
  height: 100%;
  line-height: 0;
}

.first p {
  vertical-align: middle;
  display: inline-block;
  width: 49%;
}

.ipad {
  vertical-align: middle;
  display: inline-block;
  width: 49.2%;
}
</style>
</head>
<body>
  <div class="first">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque
      varius pulvinar imperdiet. Cras quam orci, <br> Duis vulputate
      risus rutrum, elementum purus non,
    </p>
    <img class="ipad"
      src="http://placehold.it/200x200">
  </div>
</body>
</html>

The code above is rendered as follows: