CSS Layout How to - Center div inside parent div








Question

We would like to know how to center div inside parent div.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#bg {<!--   w  w w  .j  a v a2s . co m-->
  background-color: red;
}

#content {
  margin-left: auto;
  margin-right: auto;
  position: relative;
  left: 0px;
  top: 0px;
  width: 200px;
  background-color: #EEE;
}
</style>
</head>
<body>
  <div id="bg">
    <div id="content">Here is my content.</div>
  </div>
</body>
</html>

The code above is rendered as follows: