CSS Layout How to - Center div inside parent








Question

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

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#outerContainer {<!--from  w  ww . ja  v  a 2 s  . c  o  m-->
  border: 1px solid red;
}

#margin {
  height: 25px;
  width: 30%;
  margin: 0 auto;
  border: 1px solid green;
}
</style>
</head>
<body>
  <div id='outerContainer'>
    <div id='margin'></div>
  </div>
</body>
</html>

The code above is rendered as follows: