Absolute Positioning in CSS

CSSWeb DevelopmentFront End Technology

With absolute positioning, an element is positioned relative to the nearest positioned ancestor.

Example

Let us now see an example −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
div.demo1 {
   position: relative;
   color: white;
   background-color: orange;
   border: 2px dashed blue;
   width: 600px;
   height: 200px;
}
div.demo2 {
   position: absolute;
   color: white;
   background-color: orange;
   border: 2px dashed blue;
   top: 50px;
   right: 0;
   width: 300px;
   height: 100px;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<div class="demo1">position: relative;
<div class="demo2">
position: absolute;
</div>
</div>
<p>This is another demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
</body>
</html>

Output

raja
Published on 30-Dec-2019 11:08:43
Advertisements