Sunday, July 26, 2020

CSS | Position Property

In order to make more complex layouts, we need to discuss the position property. It has a bunch of possible values. The CSS position property is used to set position for an element. it is also used to place an element behind another and also useful for scripted animation effect.

CSS Position & Helper Properties

So there are 5 main values of the Position Property:

position: static | relative | absolute | fixed | sticky

and additional properties for setting the coordinates of an element (I call them helper properties”):

top | right | bottom | left AND the z-index 

Important Note: Helper properties don’t work without a declared position, or with position: static. 

1. Static

position: static is the default value. Whether we declare it or not, elements are positioned in a normal order on the webpage. Let’s give an example:
First, we define our HTML structure:
<body>
    <div class="box-orange"></div>
    <div class="box-blue"></div>
</body>

Then, we create 2 boxes and define their widths, heights & positions:
.box-orange {          // without any position declaration
  backgroundorange;
  height100px;
  width100px;       
}
.box-blue {
  backgroundlightskyblue;
  height100px;
  width100px
  positionstatic;   // Declared as static
}
same result with & without position: static
As we can see in the picture, defining position: static or not doesn't make any difference. The boxes are positioned according to the normal document flow.


2. Relative

position: relative: An element’s new position relative to its normal position. 

Starting with position: relative and for all non-static position values, we are able to change an element’s default position by using the helper properties that I've mentioned above.
Let’s move the orange box next to the blue one.

.box-orange {
  positionrelative;  // We are now ready to move the element
  backgroundorange;
  width100px;
  height100px;
  top100px;         // 100px from top relative to its old position
  left100px;        // 100px from left
}

Orange box is moved 100px to bottom & right, relative to its normal position

NOTE: Using position: relative for an element, doesn’t affect other elements’ positions.


3. Absolute

In position: relative, the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent.

An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element. If it doesn’t have any parent elements, then the initial document <html> will be its parent.

Since position: absolute removes the element from the document flow, other elements are affected and behave as the element is removed completely from the webpage.
Let’s add a container as parent element:
<body>
    <div class="container">
      <div class="box-orange"></div>
      <div class="box-blue"></div>
    </div>
</body>
.box-orange {
  positionabsolute;
  backgroundorange;
  width100px;
  height100px;
}
position: absolute takes the element to the beginning of its parent
Now it looks like the blue box has disappeared, but it hasn’t. The blue box behaves like the orange box is removed, so it shifts up to the orange box’s place.
Let’s move the orange box 5 pixels:
.box-orange {
  positionabsolute;
  backgroundorange;
  width100px;
  height100px;
  left5px;
  top5px;
}
Now we can see the blue box
The coordinates of an absolute positioned element are relative to its parent if the parent also has a non-static position. Otherwise, helper properties position the element relative to the initial <html>.

.container {
  positionrelative;
  backgroundlightgray;
}

.box-orange {
  positionabsolute;
  backgroundorange;
  width100px;
  height100px;
  right5px;    // 5px relative to the most-right of parent
}



4. Fixed


Like position: absolute, fixed positioned elements are also removed from the normal document flow. The differences are:

  • They are only relative to the <html> document, not any other parents.
  • They are not affected by scrolling.
.container {
  positionrelative;
  backgroundlightgray;
}
.box-orange {
  positionfixed;
  backgroundorange;
  width100px;
  height100px;
  right5px;    // 5px relative to the most-right of parent
}

Here in the example, I change the orange box’s position to fixed, and this time it is relative 5px to the right of the <html>, not its parent (container):

See the Pen Position Fixed by PANKAJ (@pankkap) on CodePen.
As we can see, scrolling the page doesn’t affect the fixed positioned box. It is not relative to its parent (container) anymore.

5. Sticky

position: sticky can be explained as a mix of position: relative and position: fixedIt behaves until a declared point like position: relative, after that it changes its behavior to position: fixed . The best way to explain position: sticky is by an example:

See the Pen CSS Position Sticky by PANKAJ (@pankkap) on CodePen.
 
IMPORTANT: Position Sticky is not supported in Internet Explorer and earlier versions of other browsers. 

Z-Index

The z-index property determines the stack level of an HTML element. The “stack level” refers to the element’s position on the Z-axis (as opposed to the X-axis or Y-axis). A higher value means the element will be closer to the top of the stacking order. This stacking order runs perpendicular to the display, or viewport.

3-DIMENSIONAL REPRESENTATION OF THE Z AXIS

In order to clearly demonstrate how z-index works, the image above exaggerates the display of stacked elements in relation to the viewport.

See the Pen CSS Z-Index by PANKAJ (@pankkap) on CodePen.