What is the position property in CSS? What values does it have (static, relative, absolute, fixed, sticky) and how do they work?
sobes.tech AI
Answer from AI
The position property in CSS defines how an element is positioned on the page and affects how it interacts with other elements.
Main position values:
-
static— the default value. The element is positioned in the normal document flow, andtop,left,bottom,rightproperties do not apply. -
relative— the element is positioned relative to its normal position. You can offset the element usingtop,left,bottom,right, while it still occupies its original space in the flow. -
absolute— the element is positioned relative to the nearest ancestor with a position other thanstatic(if none, relative to the browser window). The element is taken out of the normal flow, does not occupy space, and is offset usingtop,left,bottom,right. -
fixed— the element is fixed relative to the browser window and does not move when scrolling. It is also taken out of the flow. -
sticky— a hybrid ofrelativeandfixed. The element behaves asrelativeuntil it reaches a specified scroll threshold (e.g.,top: 0), after which it becomes fixed.
Example of usage:
.element {
position: sticky;
top: 0;
background: white;
}
This element will stick to the top of the window when scrolling.