Open In App

What is the Position of an element relative to its container in CSS ?

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn what is the position of an element relative to its container. The position property in CSS tells about the method of positioning for an element or an HTML entity. 

There are five different types of position properties available in CSS:

  • Fixed: Any HTML element with position: fixed property will be positioned relative to the viewport. An element with fixed positioning allows it to remain at the same position even as we scroll the page. We can set the position of the element using the top, right, bottom, and left.
  • Static: This method of positioning is set by default. If we don’t mention the method of positioning for any element, the element has the position: static method by default. By defining static, the top, right, bottom and left will not have any control over the element. The element will be positioned with the normal flow of the page.
  • Relative: An element with position: relative is positioned relatively with the other elements which are sitting at top of it. If we set its top, right, bottom, or left, other elements will not fill up the gap left by this element. An element with its position set to relative and when adjusted using top, bottom, left, and right will be positioned relative to its original position.
  • Absolute: An element with position: absolute will be positioned with respect to its nearest non-static ancestor. The positioning of this element does not depend upon its siblings or the elements which are at the same level.
  • Sticky: Element with position: sticky and top: 0 played a role between fixed and relative based on the position where it is placed. If the element is placed in the middle of the document, then when the user scrolls the document, the sticky element starts scrolling until it touches the top. When it touches the top, it will be fixed at that place in spite of further scrolling. We can stick the element at the bottom, with the bottom property.

What is the position of an element relative to its container?

In simple words, it means setting the parent element to relative and making the child element to an absolute position. The sum of the relative and absolute positions will set the child relative to its parent or we can also say that position of an element relative to its container.

Syntax: 

position: fixed | static | relative | absolute | sticky;

Example 1: In the below code, we will make use of the relative and absolute properties to demonstrate the solution to this problem.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        What is the Position of an element 
        relative to its container ?
    </title>
      
    <style>
        .h1Class {
            color: green;
        }
  
        .parent {
            position: relative;
            left: 0px;
            top: 0px;
            height: 150px;
            background-color: lightgreen;
        }
  
        .child {
            position: absolute;
            left: 30px;
            top: 40px;
            background-color: green;
            color: white;
        }
    </style>
</head>
  
<body style="text-align:center">
    <h1 class="h1Class">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
  
    <div class="parent">Parent
        <div class="child">Child</div>
    </div>
</body>
  
</html>


Output:

 

Example 2: In the below code, we will make use of the relative and absolute properties to demonstrate the solution to this problem.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Position of an element 
        relative to its container ?
    </title>
      
    <style>
        .h1Class {
            color: green;
        }
  
        .parent {
            position: relative;
            left: 0px;
            top: 0px;
            height: 250px;
            background-color: lightgreen;
        }
  
        .child1 {
            position: absolute;
            left: 30px;
            top: 40px;
            background-color: orange;
            color: white;
        }
  
        .child2 {
            position: absolute;
            left: 60px;
            top: 80px;
            background-color: white;
            color: black;
        }
  
        .child3 {
            position: absolute;
            left: 80px;
            top: 120px;
            background-color: blue;
            color: white;
        }
  
        .child4 {
            position: absolute;
            left: 100px;
            top: 160px;
            background-color: green;
            color: white;
        }
    </style>
</head>
  
<body style="text-align:center">
    <h1 class="h1Class">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
  
    <div class="parent">Parent
        <div class="child1">Child-1</div>
        <div class="child2">Child-2</div>
        <div class="child3">Child-3</div>
        <div class="child4">Child-4</div>
    </div>
</body>
  
</html>


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads