HTML Element Style How to - Vertical-center images








Question

We would like to know how to vertical-center images.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.photo {<!--  www .  j ava2  s .c  o  m-->
  width: 300px;
  height: 300px;
  border: 1px solid #000;
}

.middle-photo {
  display: table;
  width: 100%;
  height: 100%;
}

.center-photo {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
</style>
</head>
<body>
  <div class="photo">
    <div class="middle-photo">
      <div class="center-photo">
        <img src="http://www.java2s.com/style/download.png" />
      </div>
    </div>
  </div>
</body>
</html>

The code above is rendered as follows: