Sub topic 2
The position property at a glance
This chapter assumes you know basic HTML structure and CSS selectors, the box model (content, padding, border, margin), and length units such as px, %, and rem. It gives you a map of the position property. Later chapters cover offsets, containing blocks, and each value in detail. By the end you should be able to predict, for each value, whether an element keeps its space in the flow, what it is placed against, and whether it can overlap its neighbors.
What position controls
In normal flow, the browser places each element by its display type and its source order. Block boxes stack vertically. Inline content runs along a line and wraps. You never say where an element goes. The layout algorithm works it out from the elements before it.
The position property changes how one element takes part in that algorithm. It answers two questions:
- Does the element still take up space in the flow, so that its siblings arrange themselves around it?
- Which reference rectangle do its offset properties (
top,right,bottom,left,inset) measure from?
Each of the five values answers these two questions differently.
.selector {
position: static; /* static | relative | absolute | fixed | sticky */
}The initial value is static. The property is not inherited, so setting position: absolute on a parent does not affect its children. Every element you want to position needs its own declaration. You can still write position: inherit to copy the parent's value explicitly.
The five values
| Value | Keeps its space in the flow? | Offsets measure from | Typical use |
|---|---|---|---|
| `static` | Yes | Not applicable, offsets are ignored | Everything by default |
| `relative` | Yes | The element's own normal position | Small nudges, anchoring absolute children |
| `absolute` | No | The padding box of the nearest positioned ancestor | Badges, tooltips, overlays inside a component |
| `fixed` | No | The viewport (usually) | Chat buttons, modal backdrops |
| `sticky` | Yes | The nearest scrolling ancestor's visible area | Headers and table headings that stay in view |
An element is called positioned when its computed position is anything other than static. This term matters later, because absolute positioning looks for a positioned ancestor.
static
static is the default. The element sits where normal flow puts it. If you set top: 20px on a static element, nothing happens. The browser ignores offsets on it without an error. That silent failure is the most common beginner problem: a developer writes top and left but forgets position.
Static elements also ignore z-index, with one exception. Children of a flex or grid container accept z-index even when they are static. Stacking order is covered in its own chapter.
relative
A relative element is laid out exactly as it would be if it were static. The browser reserves the same space for it. Then it paints the element shifted by the offsets. Siblings do not react to the shift, so a shifted element can overlap them or leave a visible gap where it used to be.
.box {
position: relative;
top: 10px;
left: 20px;
}This draws the box 10px lower and 20px further right than its normal position. The space it occupies in the flow stays where it was. A relative element with no offsets looks identical to a static one. Developers often use it for exactly that reason. It turns the element into a positioned element without moving it, so absolute children have something to anchor to.
absolute
An absolute element is removed from the flow. Its siblings behave as if it were not there, and its parent does not grow to contain it. The browser then places it using its offsets, measured from the padding box of the nearest positioned ancestor. If there is no positioned ancestor, it uses the initial containing block, which is roughly the size of the viewport and anchored at the top of the document.
fixed
A fixed element is also removed from the flow. Its reference rectangle is the viewport, so it stays in the same place on screen while the page scrolls. One edge case: if an ancestor has a transform, filter, perspective, or similar property, that ancestor becomes the reference instead, and the element no longer pins to the viewport. The pitfalls chapter returns to this.
sticky
A sticky element is the one value that behaves as a hybrid. It stays in the flow and keeps its space. It scrolls normally until it would cross the threshold set by an offset, such as top: 0. Then it holds that position while its parent is still in view. When the parent scrolls away, the element goes with it. Sticky needs at least one offset to do anything, and it only works within the bounds of its parent.
Two groups of values
The values fall into two groups that are easier to remember than five separate rules.
- In-flow values:
static,relative,sticky. The element keeps its space. Siblings never move because of it. - Out-of-flow values:
absolute,fixed. The element's space is released. Siblings close up as if it were absent.
The diagram below shows the decision as a sequence of questions.
A runnable comparison
The page below puts one of each value in a container. Save it as an .html file and open it in a browser. Remove the position line from one box at a time and watch what changes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>position values</title>
<style>
body { margin: 0; font-family: sans-serif; height: 200vh; }
.stage {
position: relative;
margin: 2rem;
padding: 1rem;
border: 2px dashed gray;
height: 300px;
}
.item {
width: 120px;
height: 50px;
margin-bottom: 8px;
color: white;
line-height: 50px;
text-align: center;
}
.s { background: gray; position: static; top: 40px; }
.r { background: tomato; position: relative; top: 15px; left: 30px; }
.a { background: steelblue; position: absolute; top: 0; right: 0; }
.f { background: seagreen; position: fixed; bottom: 10px; right: 10px; }
.k { background: darkorchid; position: sticky; top: 0; }
</style>
</head>
<body>
<div class="stage">
<div class="item s">static</div>
<div class="item r">relative</div>
<div class="item a">absolute</div>
<div class="item k">sticky</div>
<div class="item">normal</div>
</div>
<div class="item f">fixed</div>
</body>
</html>Here is what you should observe:
- The gray
staticbox ignorestop: 40px. It sits at the start of the stage. - The red
relativebox is shifted down and right, but the space it reserved remains. The boxes after it do not move up or down because of the shift. - The blue
absolutebox jumps to the top-right corner of the dashed stage, not the page. The stage is its reference because the stage hasposition: relative. The boxes after it close up as if it were not there. - The green
fixedbox stays in the bottom-right corner of the window as you scroll the tall page. - The purple
stickybox scrolls until it reaches the top of the window, then holds there until the stage ends. Because the stage is only 300px tall, the box stays stuck for a short stretch before leaving with its parent.
Side effects of leaving the flow
Switching to absolute or fixed changes more than placement. Four side effects explain many surprises.
- Shrink-to-fit width. A normal block fills its parent's width. An out-of-flow box with
width: autoshrinks to fit its content. If you expected a full-width bar, setwidthor use bothleftandright. - Blockification. An absolute or fixed element gets a computed
displayof block-level. An inlinespanthat you make absolute behaves as a block, so you can give it width and height. - Parent collapse. If every child is out of flow, the parent has no in-flow content to size itself from, so its height can drop to zero. Give the parent an explicit height, or keep at least one child in the flow.
- Default location. If you set
position: absolutebut give no offsets, the element stays at the place it would have occupied in the flow (its static position). It does not jump to the corner. It only stops taking up space.
Positioned elements also paint above non-positioned, in-flow content by default. That is why a positioned badge appears over text without any z-index. The details of paint order are covered later.
Choosing a value quickly
Ask these questions in order:
- Should the element look and behave as it normally would? Leave it
static. - Do you only need to shift it slightly, or make it an anchor for children? Use
relative. - Should it sit on top of something else, inside a component? Use
absoluteand anchor it. - Should it stay fixed on the screen, whatever the scroll? Use
fixed. - Should it scroll with the page and then hold at an edge? Use
sticky.
Summary of reading order
You now have the vocabulary used for the rest of this tutorial: positioned element, in flow, out of flow, and reference rectangle. The next chapters take the pieces in turn. They cover how offsets work, then relative positioning, then containing blocks, which decide where absolute and fixed elements anchor.
Exercise
A page has a .card with no position declaration. Inside it, a .badge has position: absolute; top: 0; right: 0;. The badge appears in the top-right corner of the browser window, not the card. Why, and what is the smallest change that fixes it?
Show answer
With no positioned ancestor, the badge's reference rectangle is the initial containing block (roughly the viewport at the top of the document). Add position: relative to .card. This makes the card a positioned element without moving it, and the badge now measures its offsets from the card's padding box.
Exercise
Which of the five position values keep the element's space in the flow, and which release it? What happens to a parent whose only child is position: absolute?
Show answer
static, relative, and sticky keep the space. absolute and fixed release it. If the only child is absolute, the parent has no in-flow content to size from, so its height can collapse to zero unless you set an explicit height.