A Detail guide CSS Position.

Hi folks looking for CSS position check it here.

A Detail guide CSS Position.

CSS Position

Web applications are always growing, and users expect so much information on a single page with less click or scroll. Placing/Positioning HTML elements at correct places are the key to hold so many controls on a single page. CSS position is one of the key topics in CSS which helps to understand how to place HTML elements at various locations on a page.

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

Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.

What is document normal flow?

Rendering elements normally one after another is considered as normal flow. Each normal flow element position affects or affected by other normal flow elements. There are five different position values:

  • static
  • relative
  • fixed
  • absolute
  • sticky

i. Static position

This is the default CSS position applicable to all HTML elements. This position place the HTML elements based on normal document flow. Using this position, top/right/bottom/left properties can not be applicable to elements (ie., static position elements don’t obey top/right/bottom/left properties).

 position:static;

ii. Relative position.

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;

iii. Fixed position.

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;

iv. Absolute position:

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.

  • Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
    position:absolute;
    

v. Sticky position

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

  • Note: Internet Explorer does not support sticky positioning. Safari requires a -webkit- prefix (see example below). You must also specify at least one of top, right, bottom or left for sticky positioning to work.

    position:sticky;