HTML Element Style How to - relatively position a div within a div








Question

We would like to know how to relatively position a div within a div.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.outside {<!--   www.j a  va2  s.  c o  m-->
  position: relative;
  width: 200px;
  height: 200px;
  border: 2px solid black
}

.inside {
  position: absolute;
  width: 50px;
  height: 50px;
  border: 2px solid blue
}

.top_right {
  top: 0;
  right: 0;
}

.center {
  top: 50%;
  left: 50%;
  margin: -25px 0 0 -25px
}
</style>
</head>
<body>
  <div class="outside">
    <div class="inside top_right"></div>
  </div>
  <br>
  <br>
  <div class="outside">
    <div class="inside center"></div>
  </div>
  <br>
  <br>
  <a
    href="http://stackoverflow.com/questions/9877047/how-to-relatively-position-a-div-within-a-div/9877139#9877139">How
    to relatively position a div within a div?</a>
</body>
</html>

The code above is rendered as follows: