Sub topic 4
Relative positioning: shifting without leaving the flow
This chapter assumes you know basic HTML and CSS selectors, the box model (content, padding, border, margin), and length units such as px, %, and rem. It also assumes you have read the earlier chapters on normal flow, the position property, and the offset properties.
What relative positioning does
Setting position: relative on an element changes two things:
- The element becomes a positioned element. Its offset properties (
top,right,bottom,left,inset) now take effect. - The element can become a reference point for absolutely positioned children. The next chapter covers this.
The browser first lays the element out in normal flow, as if it were position: static. Then it paints the box shifted by the offsets. The space it reserved in the flow does not move.
.box {
position: relative;
top: 10px;
left: 20px;
}This box is drawn 10px lower and 20px further right than its normal position. Its siblings are laid out as if the box had never moved.
How offsets are read
For a relatively positioned element, offsets mean "move away from this edge of my normal position."
| Declaration | Visual effect |
|---|---|
| `top: 10px` | Moves the box down 10px |
| `bottom: 10px` | Moves the box up 10px |
| `left: 20px` | Moves the box right 20px |
| `right: 20px` | Moves the box left 20px |
| `top: -10px` | Moves the box up 10px |
The direction feels reversed at first. Read left: 20px as "push away from the left edge," which moves the box right.
If you set both top and bottom, top wins. If you set both left and right, left wins when the text direction is left to right. Percentage offsets are relative to the containing block's height (for top and bottom) or width (for left and right).
A worked example
This page has three boxes in a column. The middle one is shifted.
<div class="box one">One</div>
<div class="box two">Two</div>
<div class="box three">Three</div>
<style>
.box {
width: 120px;
height: 60px;
margin-bottom: 10px;
color: white;
}
.one { background: tomato; }
.two {
background: steelblue;
position: relative;
top: 20px;
left: 40px;
}
.three { background: seagreen; }
</style>Box Two is painted 20px lower and 40px to the right. Box Three does not close the gap or move out of the way. It stays directly below the space Box Two originally occupied. As a result, Box Two now overlaps the top of Box Three.
Elements that come later in the HTML are painted over earlier ones when they overlap, as long as none of them sets z-index. Positioned elements are painted after non-positioned ones, so Box Two appears on top of Box Three even though Box Three comes later in the source.
Common uses
- Small visual adjustments, such as aligning an icon a pixel or two with adjacent text.
- Animating or transitioning an element's offset without reflowing the page. Prefer
transform: translate()for animation, since it avoids layout work. - Creating a containing block for absolutely positioned children, with no offsets at all. Writing only
position: relativeis common and valid.
Common errors
- Offsets do nothing. The element is still
position: static. Check that thepositiondeclaration is applied, not overridden by a more specific selector. - Expecting siblings to move. They do not. The original space is preserved.
- Unexpected overlap. Shifting a box into its neighbors causes overlap, since the neighbors do not make room.
- Confusing direction.
right: 10pxmoves the box left, not right. - Applying relative to table parts. Support for
position: relativeon some table elements, such as<tr>and<td>, has historically been inconsistent. Test it, or wrap the content in adiv.
Exercise
A paragraph has position: relative; right: 15px; top: -5px;. In which direction is it painted relative to its normal position, and does the paragraph after it move?
Show answer
It is painted 15px to the left and 5px higher than its normal position. The next paragraph does not move, because the original space stays reserved in the flow.
Exercise
You add top: 30px to an element, but nothing changes. The element has no position declaration. Why, and what is the smallest fix?
Show answer
The element is position: static by default, and static elements ignore offset properties. Adding position: relative makes the top: 30px offset take effect.