CSS Layout How to - Center box over a full background








Question

We would like to know how to center box over a full background.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#outerContainer {<!--from  w w  w  .jav  a  2  s  .  c o  m-->
  display: table;
  margin: auto;
}

#mainContainer {
  width: 100%;
  display: table-cell;
  vertical-align: middle;
}

#background {
  width: 100%;
  height: 100%;
  background-image:url('http://placehold.it/1000x800');
  position: absolute;
  background-repeat: no-repeat;
  background-size: cover;
}

#box {
  text-align: center;
  width: 100px;
  position: fixed;
  height: 100px;
  background: #fff;
  border: 1px #000 solid;
}
</style>
</head>
<body>
  <div id="background"></div>
  <div id="outerContainer">
    <div id="mainContainer">
      <div id="box">
        <p>This is some content</p>
      </div>
    </div>
  </div>
</body>
</html>

The code above is rendered as follows: