CSS Layout How to - Horizontally center div after the browser is resized








Question

We would like to know how to horizontally center div after the browser is resized.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#someId {<!--from w  ww. j  a v  a  2s  .  c  o  m-->
  width: 100px;
  height: 100px;
  padding: 0;
  border: 0;
  margin-left: -50px; /* 100/2 */
  margin-top: -50px; /* 100/2 */
  position: absolute;
  left: 50%;
  top: 50%;
  background: #000;
  z-index: 2500;
  box-shadow: 0 0 150px #000;
}
</style>
</head>
<body>
  <div id="someId"></div>
</body>
</html>

The code above is rendered as follows: