CSS Positions & Properties

CSS Positions & Properties

Photo by Ilya Pavlov on Unsplash

#CSS Position The CSS position property is used to set position for an element in CSS. it is also used to place an element behind another with the help of z-index properties.

An element can be positioned using the top, bottom, left and right properties. These properties can be used only after position property is set or defined first.

Now, Let's have a look at the following CSS positioning:

  • CSS Static Positioning
  • CSS Relative Positioning
  • CSS Fixed Positioning
  • CSS Absolute Positioning
  • CSS Sticky Positioning

Let's discuss in brief with examples -

CSS Static Positioning

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:

div {
  position: static;
  border: 3px solid #73AD21;
}

CSS Relative Positioning

Using CSS Relative positioning an element can be positioned according to the normal flow of the document, and then offset relative to itself based on the values of top, right, bottom, and left. The offset does not affect the position of any other elements, thus it remain same as static.

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.

div {
  position: relative;
  left: 30px;
  border: 3px solid #73AD21;
}

CSS Fixed Positioning

An element with position: fixed; is positioned relative to the Browser/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.

div{
  position: fixed;
  bottom: 0;
  right: 0;
  width: 300px;
  border: 3px solid #73AD21;
}

CSS Absolute Positioning

An element with position: absolute; is positioned relative to the nearest positioned ancestors. However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

The absolute positioning is used to position an element relative to the first parent element that has a position other than static. If no such element is found, the containing block is HTML.

With the absolute positioning, you can place an element anywhere on a page also it can overlap other elements.

div.relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid #73AD21;
}

div.absolute {
  position: absolute;
  top: 80px;
  right: 0;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
}

CSS Sticky Positioning

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/browser - then it "sticks" in place (like position:fixed).

div{
  position: sticky;
  top: 0;
  background-color: green;
  border: 2px solid #4CAF50;
}

Cheat sheets for using CSS Position Properties :

PropertyDescriptionValues
positionIt is used to specify the type of positioning for an element.absolute, fixed, relative, static, inherit, sticky
topIt is used to set a top margin edge for a positioned box.auto, length, %, inherit
bottomIt is used to set the bottom margin edge for a positioned box.auto, length, %, inherit
leftIt sets a left margin edge for a positioned box.auto, length, %, inherit
rightIt is used to set a right margin edge for a positioned box.auto, length, %, inherit
clipIt is used to clip an absolutely positioned element.shape, auto, inherit
cursorIt is used to specify the type of cursors to be displayed.
overflowThis property is used to define what happens if content overflow an element's box.auto, hidden, scroll, visible, inherit
z-indexIt is used to set stack order of an element.number, auto, inherit