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 common length units such as px, %, and rem. You should also know how normal flow lays out block and inline boxes.
By the end, you will be able to shift an element with position: relative, predict what happens to the space it occupied, and use it to create a containing block for later chapters.
What relative positioning does
An element with position: static (the default) sits in normal flow and ignores top, right, bottom, left, and inset. Setting position: relative changes two things:
- The offset properties now take effect. They move the box away from where normal flow would have put it.
- The element becomes a positioned element. This makes it the containing block for any absolutely positioned descendants. Later chapters rely on this.
The key behavior is this: the browser first lays out the element as if it were static. It reserves that space in the flow. Only then does it paint the box shifted by the offsets. Siblings do not react to the shift.
.box {
position: relative;
top: 10px;
left: 20px;
}This 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 the elements after it do not move.
Reading the offsets
Offsets describe a push away from an edge. They do not describe a destination. This trips up many learners.
| Declaration | Effect on a relatively positioned box |
|---|---|
| `top: 10px` | Moves the box 10px down |
| `top: -10px` | Moves the box 10px up |
| `bottom: 10px` | Moves the box 10px up |
| `left: 20px` | Moves the box 20px right |
| `right: 20px` | Moves the box 20px left |
| `left: -20px` | Moves the box 20px left |
So top: 10px means "move away from the top edge by 10px". bottom: 10px means "move away from the bottom edge by 10px", which is upward.
Conflicting offsets
If you set opposite offsets on the same axis, only one wins. For top and bottom, top wins. For left and right, the winner depends on the writing direction: left wins in left-to-right text, and right wins in right-to-left text. The losing value is ignored, not added.
.conflict {
position: relative;
top: 10px;
bottom: 50px; /* ignored, because top wins */
}The inset shorthand
inset sets all four offsets at once. It follows the same order as margin: top, right, bottom, left.
.nudge {
position: relative;
inset: 4px 0 0 8px; /* top 4px, right 0, bottom 0, left 8px */
}Because of the conflict rule above, right: 0 and bottom: 0 here do not cancel the shift. top and left win on their axes, so the box moves 4px down and 8px right.
Percentages and units
Offset percentages on a relatively positioned box resolve against the containing block, which is the nearest block-level ancestor's content box. The two axes use different references:
leftandrightpercentages use the containing block's width.topandbottompercentages use the containing block's height, but only if that height is set explicitly. If the parent's height depends on its content, a percentagetoporbottomcomputes toautoand has no effect on a relatively positioned box.
This is a common source of "my top: 50% does nothing" reports. Use a length such as rem or px when you cannot be sure the parent has a definite height.
What happens to the space
Because the original space is kept, a shifted box can overlap its neighbors or leave a visible gap. The browser does not push anything out of the way.
<div class="row">
<div class="item">One</div>
<div class="item shifted">Two</div>
<div class="item">Three</div>
</div>
<style>
.item {
display: inline-block;
width: 80px;
padding: 8px;
background: lightgray;
}
.shifted {
position: relative;
top: 20px;
left: 30px;
background: gold;
}
</style>Box "Two" is drawn 20px lower and 30px to the right. Box "Three" stays exactly where it would have been if "Two" had never moved. As a result, "Two" overlaps part of "Three". The layout is unaware of the overlap.
Since the shifted box is painted later in the document order than "One", and positioned elements paint above non-positioned ones, "Two" is drawn on top of "Three" even though "Three" comes later in the HTML. Paint order is covered in the stacking chapters.
Relative positioning and other elements
Inline elements
position: relative works on inline elements too. An inline <span> can be shifted vertically, which is handy for nudging an icon or a superscript-like piece of text. The line box height does not change, so the line spacing is unaffected.
.icon {
position: relative;
top: 2px; /* align the icon with the text baseline */
}Table parts
Support for position: relative on table cells, rows, and similar table internals has historically differed between browsers. Current browsers handle common cases, but if you need to shift table content, apply the positioning to an element inside the cell instead. This is more predictable.
Effect on size and scrolling
A relative shift does not change the element's width or height. It also does not change the size of the parent. However, a shifted box can extend past the parent's edge. If that parent has overflow: hidden, the shifted part is clipped. If the parent allows scrolling, the shifted box can add to the scrollable overflow area, so scrollbars may appear.
Relative positioning as a containing block
Often you do not want to move an element at all. You set position: relative with no offsets only to make the element an anchor for its absolutely positioned children.
<div class="card">
<span class="badge">New</span>
<p>Card content</p>
</div>
<style>
.card {
position: relative;
padding: 1rem;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 0;
right: 0;
}
</style>The .card has no offsets, so it does not move. But because it is positioned, the absolutely positioned .badge measures its top and right from the card's padding box. The badge lands in the card's top-right corner. Without position: relative on the card, the badge would look for a higher positioned ancestor, and might end up in the corner of the page.
The next chapters cover containing blocks and absolute positioning in detail. For now, remember that position: relative with no offsets is a normal and common pattern, and it has no visual effect on the element itself.
Relative positioning and stacking
A positioned element with z-index: auto does not create a stacking context. It still paints in the positioned layer, above normal flow content. If you set an integer z-index on a relatively positioned element, it does create a stacking context, and z-index starts to work. On a static element, z-index is ignored (flex and grid items are the exception).
.lifted {
position: relative;
z-index: 1; /* works because the element is positioned */
}This is a frequent reason to add position: relative to an element: it lets z-index apply without moving anything.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Offsets do nothing | Element is still `position: static` | Add `position: relative` |
| Siblings did not move when the box moved | This is expected; the original space is kept | Use `margin` or flow changes if you need siblings to react |
| `top: 50%` has no effect | Parent height is `auto`, so the percentage computes to `auto` | Use a length, or give the parent an explicit height |
| `bottom` seems to move the box the wrong way | Offsets push away from an edge | Use a negative value or `top` instead |
| Box is clipped after shifting | Ancestor has `overflow: hidden` | Reduce the shift or change the overflow |
| Animation of `top` and `left` feels janky | Offsets trigger layout and paint work | Use `transform: translate()` for animation |
Relative offsets versus margins and transforms
Three tools can move an element a few pixels, and they behave differently.
| Tool | Affects siblings | Affects layout size | Creates positioned context | Good for |
|---|---|---|---|---|
| `margin` | Yes | Yes | No | Real spacing |
| `position: relative` with offsets | No | No | Yes | Visual nudges, anchoring children |
| `transform: translate()` | No | No | No (but creates a containing block for fixed and absolute descendants) | Animation, centering tricks |
Use margin when the space is part of the design. Use relative offsets for a small visual correction. Use transform for motion, since changes to it can usually be handled without recalculating layout.
Worked example: a nudged label
Suppose you have a form label that looks slightly too high next to its input. You want to move it down 3px without changing the form's height.
<label class="field">
<span class="field-name">Email</span>
<input type="email">
</label>
<style>
.field {
display: flex;
gap: 0.5rem;
align-items: center;
}
.field-name {
position: relative;
top: 3px;
}
</style>The flex container still calculates its height from the original, unshifted label. The label is painted 3px lower. Nothing else in the form moves. If you had used margin-top: 3px instead, the label's flow size would grow, and the row could get taller.
Summary
position: relativemakes offsets work and leaves the element's original space in the flow.- Offsets push away from an edge, so
bottom: 10pxmoves the box up. - Siblings never react to the shift, so overlaps and gaps are possible.
- Percentage
topandbottomneed a parent with a definite height. - With no offsets,
position: relativeis the standard way to create a containing block for absolute children. - Prefer
transformfor animation andmarginfor real spacing.
Exercise
A box has position: relative; top: 30px; bottom: 10px;. The text direction is left-to-right. Where is the box drawn compared to its normal position, and what happens to the space that the box occupies in the flow?
Show answer
The box is drawn 30px lower. When top and bottom are both set, top wins and bottom is ignored. The space in the flow does not change, so siblings stay where they were and the shifted box may overlap them.
Exercise
You add position: relative; top: 50%; to a child of a parent with no explicit height, hoping to move the child halfway down the parent. Nothing happens. Why, and what could you do instead?
Show answer
The parent's height depends on its content, so the percentage for top computes to auto and has no effect. Give the parent an explicit height, or use a length value such as top: 2rem.
Exercise
A card needs a small "Sale" tag in its top-right corner. You write .tag { position: absolute; top: 0; right: 0; }, but the tag appears at the top-right corner of the page. What one-line change fixes it, and does that change move the card?
Show answer
Add position: relative to the card. The card becomes the containing block for the tag, so the tag is placed against the card's corner. The card does not move because no offsets are set on it.