CSS Layout How to - Position elements at top right of its parent








Question

We would like to know how to position elements at top right of its parent.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#myDiv {<!--  w  ww. j av  a2 s  .  c  o  m-->
  height: 50px;
  position: absolute;
  width: 300px;
  background-color: #DDD;
}

#myButton {
  cursor: pointer;
  margin-right: 10px;
  position: absolute;
  top: 0;
  right: 0;
}
</style>
</head>
<body>
  <div id="myDiv">
    <p>text</p>
    <span id="myButton">B</span>
  </div>
</body>
</html>

The code above is rendered as follows: