Positioning is how you take control of exactly where an element sits on a page. By default, elements simply flow one after another in the order they appear in the HTML, but the position property lets you break out of that flow, place an element relative to its container, pin it to the screen, or make it stick as the user scrolls. Understanding the five positioning modes, and how they interact with top, left, right, bottom, and z-index, unlocks most of the layout effects you see on the modern web.
The Five Positioning Modes
The position property has five values, and each changes how an element is placed and how it relates to everything around it.
Static: the default
Every element starts as static, which simply means it follows the normal document flow, sitting wherever its place in the HTML puts it. No special positioning is applied, and crucially, a static element ignores the top, left, right, and bottomproperties entirely.
.element { position: static;}
You rarely set static explicitly, since it is already the default. It is worth knowing mainly so you understand what you are overriding when you choose any other value.
Relative: nudged from its normal position
A relative element stays in the document flow but can be shifted from where it would normally sit, using the offset properties.
.element { position: relative; top: 20px; left: 10px;}
This moves the element 20 pixels down and 10 pixels to the right of its original position. The important detail is that the space it would have occupied is still reserved, so other elements do not move to fill the gap. This makes relative ideal for small visual adjustments. It also has a second, equally important job: a relatively positioned element becomes the reference point for any absolutely positioned children, which is the key to the next mode.
Absolute: placed relative to a positioned ancestor
An absolute element is removed from the normal flow entirely, so it no longer takes up space, and other elements behave as if it were not there. It is then positioned relative to its nearest positioned ancestor, meaning the closest parent with a position of relative, absolute, fixed, or sticky. If no such ancestor exists, it positions relative to the page itself.
.parent { position: relative;}.child { position: absolute; top: 20px; left: 50px;}
Here the child is placed 20 pixels from the top and 50 pixels from the left of .parent, because the parent is positioned. This parent-child pairing is the standard pattern for dropdowns, tooltips, overlays, and badges, anything you want to place precisely inside a container. The position: relative on the parent often defines no movement of its own; it exists purely to give the absolute child something to anchor to.
Fixed: pinned to the viewport
A fixed element is positioned relative to the viewport, the visible area of the browser window, and it stays put even as the page scrolls.
.element { position: fixed; top: 0; right: 0; width: 100px; background: red;}
Because it ignores scrolling, fixed is the tool for anything that should always remain visible: a navigation bar pinned to the top, a floating action button in the corner, a cookie banner. Like absolute, it is removed from the normal flow, so it does not reserve space where it originally sat.
Sticky: a hybrid of relative and fixed
A sticky element is the most interesting of the five. It behaves like a relative element while it is in its normal position, then switches to behaving like a fixed element once you scroll past a threshold you define.
.element { position: sticky; top: 50px;}
This element scrolls along with the page normally, but as soon as it would scroll past 50 pixels from the top, it sticks there and stays visible. This is exactly the behaviour you want for section headings that remain on screen while you read their content, or a sub-navigation bar that pins itself once you scroll down to it. Sticky gives you the always-visible quality of fixed without permanently removing the element from the flow.
Moving Elements with the Offset Properties
For any element that is relative, absolute, fixed, or sticky, the four offset properties control where it goes.
.element { top: 20px; left: 10px; right: 5px; bottom: 15px;}
Each one pushes the element a set distance from the corresponding edge of its reference point. What that reference point is depends on the positioning mode: for relative it is the element’s own original position, for absolute it is the positioned ancestor, and for fixed and sticky it is the viewport. These properties have no effect at all on a static element, which is the most common reason offsets sometimes seem to do nothing.
Controlling Overlap with z-index
Once elements start overlapping, you need a way to decide which one sits on top. That is the job of z-index. Higher values appear in front of lower ones.
.box1 { position: absolute; top: 50px; left: 50px; z-index: 1;}.box2 { position: absolute; top: 70px; left: 70px; z-index: 2;}
Here .box2 sits in front of .box1 because its z-index is higher. By default, z-index is auto, and elements stack in the order they appear in the document, with later elements on top. One thing to remember is that z-index only works on positioned elements, so an element must have a position other than static for it to take effect.
Putting It Together
A simple example shows the absolute-inside-relative pattern that comes up constantly:
.container { position: relative; width: 300px; height: 200px; background: lightgray;}.box { position: absolute; top: 50px; left: 50px; width: 100px; height: 100px; background: blue;}
<div class="container"> <div class="box"></div></div>
Because the container is relative and the box is absolute, the blue box is placed 50 pixels from the top and left of the container, not the page. Change the container to static and the box would jump to position itself against the whole page instead. This relationship between a positioned parent and an absolute child is one of the most useful patterns in CSS layout.
Choosing the Right Mode
Each value has a natural home. Use static for ordinary elements that should follow the flow, which is the vast majority. Use relative for minor shifts, and as the anchor for absolutely positioned children. Use absolute for dropdowns, tooltips, and popups that need to sit precisely inside a container. Use fixed for navigation, floating buttons, and anything that must stay visible while scrolling. And use sticky for headers and section highlights that should follow you down the page after a certain point.
The Takeaway
CSS positioning gives you five ways to place an element. static follows the normal flow, relative shifts an element while keeping its space and anchoring its children, absolute detaches an element and places it against a positioned ancestor, fixedpins it to the viewport, and sticky switches from relative to fixed as you scroll. Combine these with the offset properties to control placement and z-index to control overlap, and you have the full toolkit for arranging elements exactly where you want them. The single most common pattern to remember is a relative parent with an absolute child, because once that clicks, a huge amount of layout work falls into place.
See you soon.
[…] Understanding CSS Positioning […]
[…] CSS background properties give you full control over the space behind your content. Set a colour with background-color, add an image with background-image, and shape that image with background-repeat, background-position, background-size, and background-attachment, while background-origin and background-clip fine-tune where it sits and how far it shows within the box model. The shorthand rolls all of this into one clean line, and comma-separating values lets you layer backgrounds for richer effects. Provide a fallback colour, keep your images optimised, and adjust for different screens, and your backgrounds will look polished without becoming a performance or maintenance burden. […]
[…] Positioning: https://datalad.co.uk/understanding-css-positioning/ […]
[…] positioning article covers the five values of the position property, the offset properties that move elements around, […]
[…] the full background, read the guide to understanding CSS positioning. To practise, work through the 10 code-along […]