HTML Element Style How to - Place a div over another div and center it








Question

We would like to know how to place a div over another div and center it.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.main {<!-- w ww. j a va2s.c om-->
  width: 150px;
  height: 100px;
  background: yellow;
}

.over {
  position: absolute;
  width: 50px;
  height: 50px;
  background: black;
  margin: 25px 0 0 50px;
}
</style>
</head>
<body>
  <div class="main">
    <div class="over">test</div>
  </div>
</body>
</html>

The code above is rendered as follows: