By the end of this lesson, you will be able to explain CSS static and relative positioning, and use relative offsets to nudge an element without changing the normal document flow.
What it is
Static positioning is the browser default. A static element participates in normal flow: block elements stack vertically, inline elements flow horizontally, and properties like top, right, bottom, and left have no effect.
Relative positioning keeps the element in normal flow but lets you offset it from its original position. The element still reserves its original space, so surrounding content does not reflow around the offset. Related terms include normal flow, offset properties, and containing block. A relatively positioned element is often used as a reference point for absolutely positioned children.
Why it matters
- Nudge badges, icons, or superscripts without moving nearby text.
- Create small visual corrections when flex or grid alignment is almost right.
- Provide a positioned ancestor for absolutely positioned tooltips or dropdowns.
- Avoid layout shift because the original space remains reserved.
Syntax or steps
- Set
position: relative;on the element you want to move. - Add one or more offset properties:
top,right,bottom, orleft. - Use small values such as
top: -4px;for visual nudges. - Remember that offsets are measured from the element’s normal-flow position, not from the viewport.
Example
<!DOCTYPE html>
<html>
<head>
<style>
.card {
width: 260px;
padding: 16px;
border: 1px solid #ccc;
font-family: sans-serif;
}
.badge {
display: inline-block;
padding: 2px 8px;
background: #111;
color: white;
border-radius: 999px;
position: relative;
top: -4px;
}
</style>
</head>
<body>
<div class="card">
<h2>Invoice</h2>
<p>Total: $42.00</p>
<span class="badge">New</span>
</div>
</body>
</html>
.badgeis still in normal flow, so the card layout does not change.position: relativemakestopwork.top: -4pxmoves the badge 4 pixels upward from its static position.- If another element followed the badge, it would stay where the badge originally was.
Common mistakes
- Forgetting
position: relative; offsets do nothing on static elements. - Assuming relative positioning removes the element from normal flow.
- Using large relative offsets for page layout; this can cause overlap because space is not removed.
- Confusing relative with absolute; absolute positioning removes the element from normal flow.
When to use it
| Position | Behavior | Best for |
|---|---|---|
static |
Default flow; offsets ignored. | Normal content. |
relative |
Offset from original position; original space remains. | Small nudges and positioned ancestors. |
absolute |
Removed from flow; positioned relative to nearest positioned ancestor. | Overlays, tooltips, badges pinned to corners. |
Practice
Guided exercise: Create a paragraph with class note. Add .note { position: relative; top: 8px; }. Expected output: the paragraph appears 8 pixels lower, but following content stays in its original position.
Challenge: Nudge the badge up and right using top: -4px; and left: 4px;. Hint: positive left moves an element right.
Quick check
Question: Does a relative offset change the space reserved for the element? Answer: No. The element moves visually, but its original space remains in normal flow.
Summary
Static positioning is normal flow. Relative positioning lets you offset an element while preserving its original space, making it ideal for small visual adjustments and as a reference point for absolutely positioned children.