Sobes.tech
Middle

Tell about the CSS property position: what values are available and what are their features?

sobes.tech AI

Answer from AI

The CSS property position determines how an element is positioned on the page. Main values:

  • static — default value. The element is positioned in the normal flow of the document, ignoring top, left, etc.

  • relative — the element is positioned relative to its normal position. You can offset it using top, left, but it still occupies space in the flow.

  • absolute — the element is removed from the normal flow and positioned relative to the nearest positioned ancestor (relative, absolute, fixed). If none exists, relative to the browser window.

  • fixed — the element is fixed relative to the browser window and does not move when scrolling.

  • sticky — a hybrid of relative and fixed. The element behaves as relative until it reaches a specified position during scrolling, then it becomes fixed.

Features:

  • Elements with absolute and fixed do not occupy space in the flow, affecting the layout of neighboring elements.
  • relative allows shifting the element without breaking the flow.
  • sticky is useful for creating fixed headers, menus, etc.

Example:

.header {
  position: sticky;
  top: 0;
  background: white;
}
Tell about the CSS property position: what values are… - sobes.tech