Sub topic 2
The position property at a glance
This chapter gives you the working model of the position property. Later chapters cover each value in detail. It assumes you know basic HTML and CSS selectors, the box model (content, padding, border, margin), and length units such as px, %, and rem.
What position changes
Every element starts in normal flow. Block boxes stack down the page, and inline content runs along a line. The position property decides whether an element stays in that flow or is placed some other way.
There are five values:
| Value | In normal flow? | Placed relative to |
|---|---|---|
| `static` | Yes | Its normal place. This is the default. |
| `relative` | Yes (its original space is kept) | Its own normal position |
| `absolute` | No | The nearest positioned ancestor |
| `fixed` | No | The viewport |
| `sticky` | Yes | Its normal position, until a scroll threshold is reached |
A positioned element is any element whose position is not static. This term matters because positioned elements become reference points for their absolutely positioned descendants.
Offsets only work on positioned elements
The properties top, right, bottom, left, and the inset shorthand are offsets. They do nothing on a static element. Setting top: 10px on a default element has no effect until you also set position to another value.
.box {
position: relative;
top: 10px;
left: 20px;
}Here the box is drawn 10px lower and 20px further right than its normal position. The space it occupies in the flow stays where it was, so neighbouring elements do not move.
Flow or no flow
The most useful question to ask about each value is whether the element still takes up space in the flow.
relativeandstickykeep their space. Siblings lay out as if the element had not moved.absoluteandfixedare removed from the flow. Siblings close up as if the element did not exist.
<div class="row">
<div class="item">One</div>
<div class="item floating">Two</div>
<div class="item">Three</div>
</div>
<style>
.item {
height: 40px;
background: #cde;
margin-bottom: 4px;
}
.floating {
position: absolute;
top: 0;
right: 0;
width: 80px;
}
</style>Because .floating leaves the flow, "Three" moves up directly under "One". The floating box also has no containing positioned ancestor here, so it is placed against the initial containing block, which is roughly the page area.
Common errors
- Offsets with no effect. The element is still
static. Add apositionvalue. - Element lands in the wrong place. An
absoluteelement looks for the nearest positioned ancestor. If no ancestor is positioned, it falls back to the page. The fix is usuallyposition: relativeon the intended parent, with no offsets. - Layout collapses around an element. You used
absoluteorfixed, so the parent no longer sees the element's height. Userelativeorstickyif the space should remain. - Conflicting offsets. On a
relativeelement, if bothtopandbottomare set,topwins. Likewiseleftwins overrightin left-to-right writing modes.
Reading the property in DevTools
Browser DevTools show the computed position value for any element. When a box is not where you expect, check three things in order. First, read the computed position. Second, confirm the offsets are being applied and not overridden by another rule. Third, if the element is absolute, walk up the ancestors to find the one with a non-static position.
Exercise
A <span> has top: 20px; left: 20px; but it does not move. The CSS has no position declaration for it. What is wrong, and which value would you choose if you want it shifted while neighbouring elements stay where they are?
Show answer
The span is position: static by default, and offsets are ignored on static elements. Add position: relative. The span is then drawn 20px down and 20px right of its normal place, while its original space stays reserved so neighbouring elements do not move.
Exercise
Which two position values remove an element from normal flow, and what happens to the surrounding siblings?
Show answer
absolute and fixed. The element no longer takes up space, so following siblings lay out as if it were not there.