top of page

CSS Position Property



CSS is how we determine the layout and design of a webpage. The CSS position is how we position each element in a document. This property is a single keyword, and we attach a value to it to set the specific position of an element.


The position property specifies the type of positioning method used for an element.


There are five different position values:

  • static

  • relative

  • fixed

  • absolute

  • sticky


We can also set the position property. Then, the coordinates of an element is positioned using helper properties:

1. top: It is used to set the top margin edge for positioned box.

2. bottom: It is used to set the bottom margin edge for a positioned box.

3. left: It sets a left margin edge for a positioned box.

4. right: It is used to set the right margin edge for a positioned box.

5. z-index: It is used to set stack order of an element.

6. Clip: It is used to clip an absolutely positioned element.

7. Cursor: It is sued to specify the type of cursors to be displayed.

8. Overflow: This property is used to define what happens if content overflow an element's box


1. Static

HTML elements are positioned static by default.. Static positioned elements are not affected by the top, bottom, left, and right properties. An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page.

<!DOCTYPE html>
  <html>
  <head>
    <style>
      div.static 
      {
        position: static;
        border: 3px solid green;
      }
    </style>
  </head>

  <body>
    <h2>CSS Position static</h2>
      <div class="static">
        Static 
      </div>

  </body>
</html>

Output:



2. Relative

An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

<!DOCTYPE html>
  <html>
  <head>
    <style>
      div.relative1 {
        position: relative;
        left: 50px;
        border: 3px solid blue;
      }

      div.relative2 {
        position: relative;
        right: 10px;
        border: 3px solid red;
      }

    </style>
  </head>

  <body>
  <h2>CSS Position relative</h2>
    <div class="relative1">
      Relative Left
    </div>
   
     <br>
    
    <div class="relative2">
      Relative Right
    </div>
    <br>

  </body>
</html>

Output:




3. Fixed

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located.

<!DOCTYPE html>
  <html>
    <head>
    <style>
      div.fixed {
        position: fixed;
        bottom: 100px;
        right: 20px;
        width: 400px;
        border: 3px solid orange;
      }
    </style>
    </head>
  <body>
  <h2>CSS Position fixed</h2>
    <div class="fixed">
      position: fixed;
    </div>
  </body>
</html>

Output:



4. Absolute

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.

<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid Blue;
} 

div.absolute {
  position: absolute;
  top: 85px;
  right: 10px;
  width: 200px;
  height: 100px;
  border: 3px solid Red;
}
</style>
</head>
<body>

<h2>CSS Position absolute</h2>


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

</body>
</html>

Output:




5. Sticky

An element with position: sticky; is positioned based on the user's scroll position. A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position: fixed).


<!DOCTYPE html>
<html>
<head>
<style>
div.sticky {
  position: sticky;
  top: 10px;
  padding: 5px;
  background-color: yellow;
  border: 3px solid Red;
}
</style>
</head>
<body>

<p>Scroll the Page to understand the Sticky Position</p>

<div class="sticky">The Tech Platform</div>

<div style="padding-bottom:2000px">
  <p>The Tech Platform is your own platform, managed by you as your blog/website where you share your expertise, knowledge through your content (articles/videos/blogs and more) on your own platform and will not pay any expenses for your own site hosting for a life time.</p>
  
</div>

</body>
</html>

Output:



The Tech Platform

0 comments

Recent Posts

See All
bottom of page