CSS Layout How to - Center content on a page vertically and horizontally








Question

We would like to know how to center content on a page vertically and horizontally.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
html, body {<!--   w  w w . j a  va 2s  .  c  o m-->
  margin: 0;
  padding: 0;
  border: 0;
  height: 100%;
}

body {
  background-color: transparent;
  overflow: hidden;
  font-family: "Helvetica";
  color: #359115;
  font-weight: bold;
}

#container {
  display: table;
  height: 100%;
  width: 100%;
  background-color: #dddddd;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

#container .wrapper {
  display: table-cell;
  height: 100%;
  vertical-align: middle;
  text-align: center;
}
</style>
</head>
<body>
  <div id="container">
    <div class="wrapper">
      This is a test
    </div>
  </div>
</body>
</html>

The code above is rendered as follows: