How to Position Elements in CSS
Use the CSS position property when you need precise control over where an element appears on the page. This helps with sticky headers, badges, overlays, dropdowns, tooltips, and anchored UI elements.
What you’ll build or solve
You’ll learn how to position elements in CSS using the most common positioning modes and directional offsets. You’ll also know when to use relative versus absolute positioning.
Learn CSS on Mimo
When this approach works best
This approach is the right choice when an element needs to stay in a specific spot relative to itself, another container, or the viewport.
Common real-world scenarios include:
- Notification badges
- Sticky navigation bars
- Dropdown menus
- Modal close buttons
- Tooltip labels
This is a bad idea when normal document flow already creates the correct layout. In those cases, Flexbox or Grid is usually easier to maintain.
Prerequisites
You only need:
- A basic HTML file
- A text editor
- Basic HTML and CSS knowledge
Step-by-step instructions
Step 1: Apply position with the correct offset values
Start with position: relative when you want to shift an element from where it normally appears.
HTML
<div class="badge">USA</div>
<style>
.badge {
position: relative;
top: 10px;
left: 20px;
}
</style>
Use position: absolute when the element should be anchored inside a parent container.
HTML
<div class="card">
<button class="close-btn">×</button>
</div>
<style>
.card {
position: relative;
width: 300px;
height: 200px;
}
.close-btn {
position: absolute;
top: 10px;
right: 10px;
}
</style>
What to look for:
relativemoves the element from its normal spotabsolutepositions relative to the nearest positioned parent- Use
top,right,bottom, andleftfor offsets - Set the parent to
position: relativefor anchored child elements - Use Flexbox or Grid first when full layout control is the real goal
Examples you can copy
Notification badge
HTML
<div class="icon">
🔔
<span class="badge">3</span>
</div>
<style>
.icon {
position: relative;
}
.badge {
position: absolute;
top: -8px;
right: -8px;
}
</style>
Sticky header
HTML
<header class="site-header">UK pricing</header>
<style>
.site-header {
position: sticky;
top: 0;
}
</style>
Tooltip label
HTML
<div class="tooltip">France plan</div>
<style>
.tooltip {
position: absolute;
top: 40px;
left: 0;
}
</style>
Common mistakes and how to fix them
Mistake 1: Using absolute without a positioned parent
What the reader might do:
HTML
<div class="card">
<button class="close-btn">×</button>
</div>
<style>
.close-btn {
position: absolute;
top: 10px;
right: 10px;
}
</style>
Why it breaks: the button may position relative to the page instead of the card.
Corrected approach:
HTML
<style>
.card {
position: relative;
}
.close-btn {
position: absolute;
top: 10px;
right: 10px;
}
</style>
Mistake 2: Using positioning for full page layouts
What the reader might do:
HTML
<div class="left-panel"></div>
<div class="right-panel"></div>
<style>
.left-panel {
position: absolute;
left: 0;
}
.right-panel {
position: absolute;
right: 0;
}
</style>
Why it breaks: this becomes hard to maintain and often fails on responsive screens.
Corrected approach:
Use Flexbox or Grid for full layout structures instead of manual positioning.
Mistake 3: Forgetting offset values
What the reader might do:
HTML
.badge {
position: absolute;
}
Why it breaks: without top, right, bottom, or left, the element may stay in an unexpected default spot.
Corrected approach:
HTML
.badge {
position: absolute;
top: 0;
right: 0;
}
Troubleshooting
If the element jumps to the top of the page, set the parent container to position: relative.
If offsets do nothing, confirm the element actually has a position value.
If the layout becomes fragile on mobile, switch large structural layouts to Flexbox or Grid.
If sticky positioning does not work, confirm you set top.
Quick recap
- Use
positionfor precise element placement - Use
relativefor small shifts - Use
absoluteinside positioned parents - Use offset properties like
topandright - Prefer Flexbox or Grid for full layouts
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot