Learn how to control the position of elements on a webpage using CSS positioning properties
position
Propertyposition
property specifies the positioning method used for an element. There are five different position values:
static
(default)relative
absolute
fixed
sticky
position: static
is the default positioning for all HTML elements. Elements with static positioning are positioned according to the normal document flow.
position: static
, the top
, right
, bottom
, and left
properties have no effect.position: relative
positions an element relative to its normal position in the document flow. Other elements on the page are not affected by a relatively positioned element.
Relative Positioning
top
, right
, bottom
, left
) move the element relative to its original positionposition: absolute
positions an element relative to its nearest positioned ancestor (an ancestor with a position value other than static
). If no positioned ancestor exists, it positions relative to the initial containing block (usually the viewport).
Absolute Positioning
transform
property (modern approach):
position: fixed
positions an element relative to the viewport. The element stays in the same position even when the page is scrolled.
Fixed Positioning
position: sticky
is a hybrid of relative and fixed positioning. The element is treated as relative until it crosses a specified threshold, then it’s treated as fixed.
Sticky Positioning
top
, right
, bottom
, or left
must be specifiedoverflow
set to hidden
, scroll
, or auto
.z-index
Propertyz-index
property specifies the stack order of positioned elements (which element should be placed in front of or behind other elements).
Z-Index Stacking
z-index
:
position
value other than static
)z-index
are stacked in the order they appear in the HTML (later elements on top)z-index
can be negative, placing elements behind non-positioned elementsz-index
creates stacking contexts, which can limit the effect of z-index
values in nested elementstop
, right
, bottom
, and left
) specify the distance between a positioned element and its reference point.
relative
positioning, they offset the element from its normal positionabsolute
positioning, they offset the element from its positioned ancestorfixed
positioning, they offset the element from the viewportsticky
positioning, they define the threshold at which the element becomes fixedtop
and bottom
are specified, top
takes precedence for relative
positioning, while for absolute
and fixed
positioning, the element is stretched if its height is not set.Similarly, when both left
and right
are specified, left
takes precedence for relative
positioning, while for absolute
and fixed
positioning, the element is stretched if its width is not set.position: absolute
, remember to set position: relative
on a parent element to create a positioning context.
overflow
properties appropriately.
position
property determines how an element is positioned in the documentstatic
is the default positioning method, following the normal document flowrelative
positions an element relative to its normal positionabsolute
positions an element relative to its nearest positioned ancestorfixed
positions an element relative to the viewportsticky
is a hybrid of relative and fixed positioningz-index
property controls the stacking order of positioned elementstop
, right
, bottom
, left
) specify the distance from reference points