Note position: relative keeps the element in the normal flow but enables offset with top/right/bottom/left. Its main use is establishing a positioning context for absolutely positioned children.
position relativeoffset elementpositioning contextnudge element
Position Absolute
syntax
position: absolute;
top | right | bottom | left: value;
Note Absolute positioning removes the element from the document flow and positions it relative to the nearest positioned ancestor (one with position: relative, absolute, fixed, or sticky). If none exists, it positions relative to the viewport.
position absoluteabsolute positioningplace elementtooltip positionoverlay element
Position Fixed
syntax
position: fixed;
top | right | bottom | left: value;
Note Fixed elements are positioned relative to the viewport and stay in place during scrolling. They are removed from the document flow. A transform, filter, or perspective on any ancestor breaks fixed positioning and makes it relative to that ancestor instead.
Note Sticky is a hybrid: it behaves as relative until the scroll position reaches the threshold (top/bottom), then it sticks like fixed. The element must have a scrollable ancestor and the parent must have enough height for it to work. overflow: hidden on an ancestor can prevent sticking.
stickysticky headerstick on scrollsticky elementposition sticky
Note z-index only works on positioned elements (relative, absolute, fixed, sticky) and flex/grid children. Elements with opacity < 1, transform, or filter create new stacking contexts. Use isolation: isolate to explicitly create one without side effects.
z-indexstacking orderlayer orderbring to frontoverlapstacking context
Float & Clear
syntax
float: left | right | none;
clear: left | right | both;
Note Floats were once used for page layouts but flexbox and grid have replaced that use case. Floats are still useful for wrapping text around images. Use the clearfix hack or display: flow-root on the parent to contain floated children.
floattext wrap imageclear floatclearfixfloat left right
Note For most cases, the grid place-items: center approach is the shortest and most versatile. Use margin auto for simple block-level horizontal centering. The absolute method works when you need to center within a positioned parent.
center divcenter elementhow to centercentering csshorizontal centervertical center
Inset Shorthand
syntax
inset: top right bottom left;
inset: all;
example
/* Fill entire parent */
.overlay {
position: absolute;
inset: 0;
background: rgb(000 / 0.5);
}
/* 20px from each edge */
.inset-box {
position: absolute;
inset: 20px;
}
Note inset is shorthand for top, right, bottom, and left. inset: 0 is equivalent to top: 0; right: 0; bottom: 0; left: 0. It follows the same clockwise shorthand pattern as margin and padding.
insetfill parentstretch to edgesposition shorthandoverlay