Tuesday, 13 June 2023

Position absolute in CSS

 An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

Here example source code and output :

<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
  position: relative;
  width: 800px;
  height: 600px;
  border: 3px solid #73AD21;
}

div.absolute {
  position: absolute;
  top: 30px;
  right: 10px;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
}

div.absolute2 {
  position: absolute;
  top: 150px;
  right: 10px;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
}


</style>
</head>
<body>

<h2>position: absolute;</h2>

<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed):</p>

<div class="relative">This div element has position: relative;
  <div class="absolute">This div element has position: absolute;</div>
  <div class="absolute2">This div element has position: absolute;</div>
 
</div>


</body>
</html>




Output :





Source : https://www.w3schools.com/css/css_positioning.asp

No comments:

Post a Comment