'Position' property in CSS

'Position' property in CSS

While designing a webpage we all come across problems when we want a certain item at a certain position or position of an image with respect to something else. So in order to overcome such problems, there is a property in CSS known as Position.

The position property sets how an element is positioned in a document. The top, right, bottom, and left properties determine the final location of positioned elements. It helps in manipulating the position of an item.

There are five different position values:

1. Static

Every element has a static position by default, so the element will stick to the normal page flow. So if there is a left/right/top/bottom set then there will be no effect on that element.

position: static;

2. Relative

An element’s original position remains in the flow of the document, just like the static value. But now left/right/top/bottom will work. The positional properties “nudge” the element from the original position in that direction.

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.

position: relative;

3. Fixed

The element is removed from the flow of the document like absolutely positioned elements. In fact they behave almost the same, only fixed positioned elements are always relative to the document, not any particular parent, and are unaffected by scrolling.

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.

position: fixed;

4. Absolute

The element is removed from the flow of the document and other elements will behave as if it’s not even there whilst all the other positional properties will work on it.

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.

position: absolute;

5. Sticky

The element is treated like a relative value until the scroll location of the viewport reaches a specified threshold, at which point the element takes a fixed position where it is told to stick.

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).

position: sticky;

Let us write some code for better understanding.

HTML structure:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>CSS Positioning</title>
  </head>
  <body>
    <div class="parent">
      <div class="child one">One</div>
      <div class="child two">Two</div>
      <div class="child three">Three</div>
      <div class="child four">Four</div>
    </div>
  </body>
</html>

CSS styling:

body {
  margin: 100px auto;
}

.parent {
  width: 500px;
  border: 1px solid red;
  margin: auto;
  text-align: center;
}

.child {
  border-radius: 10%;
  width: 100px;
  height: 100px;
  margin: 20px;
}

.one {
  background-color: powderblue;
}

.two {
  background-color: royalblue;
}

.three {
  background-color: sienna;
}

.four {
  background-color: slateblue;
}

Result:

img1.png

Relative Positioning:

.one {
  background-color: powderblue;
  position: relative;
  right: 50px;
}

Result:

img2.png

Here, the square has moved 50px from the left of where it was supposed to be by default.

Absolute Positioning:

.one {
  background-color: powderblue;
  position: absolute;
  top: 50px;
  left: 0;
}

Result:

img3.png

Well now the square has completely abandoned it's parent. They are not positioned based on their usual place in the document flow, but based on the position of their ancestor. This means it will be positioned relative to the whole page itself, which means relative to the element – the root of the page.

Fixed positioning:

.one {
    background-color: powderblue;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
  }

Result:

vid1-high.gif

Here the item got fixed to its position.

Sticky Position:

  .one {
    background-color: powderblue;
    position:sticky;
    bottom: 100px;
    right: 200px;
  }

Result:

screen-capture-low.gif

In this example, the sticky element sticks to the top of the page (bottom: 100px and right: 200px), when you reach its scroll position.

Happy Learning !!!