19. CSS Position
What you will learn
- The five position values
- How
top,right,bottom, andleftwork - How to place a badge on a card
- How to make a sticky header
- How
z-indexstacks overlapping elements
The position property
| Value | Behaviour |
|---|---|
static |
The default. Normal flow, and top and left have no effect |
relative |
Stays in flow, but can be nudged from its normal place |
absolute |
Removed from flow and placed relative to the nearest positioned ancestor |
fixed |
Placed relative to the browser window and stays there when scrolling |
sticky |
Scrolls normally, then sticks at a set position |
Relative
A relative element keeps its space in the page, but top, left, right, and bottom shift it visually from where it would normally be.
Example
<style>
.shifted {
position: relative;
left: 40px;
top: 10px;
background-color: #cfe6ff;
padding: 8px;
}
</style>
<p>Normal paragraph</p>
<p class="shifted">Shifted 40px right and 10px down</p>
<p>Normal paragraph</p>
Output
Normal paragraph
Shifted 40px right and 10px down
Normal paragraph
Absolute
An absolute element leaves the normal flow, so other content closes the gap. It is placed relative to its nearest ancestor that has a position other than static. If there is none, it uses the page.
That is why the usual pattern is: position: relative on the parent, and position: absolute on the child.
Example
<style>
.card {
position: relative;
width: 260px;
padding: 20px;
background-color: #eaf5ff;
border: 1px solid #cfe6ff;
border-radius: 8px;
}
.badge {
position: absolute;
top: 8px;
right: 8px;
background-color: crimson;
color: white;
font-size: 12px;
padding: 2px 8px;
border-radius: 10px;
}
</style>
<div class="card">
<span class="badge">New</span>
<p>A course card with a badge in the corner.</p>
</div>
Output
A course card with a badge in the corner.
Fixed
A fixed element ignores scrolling. It is perfect for a “back to top” button or a cookie notice.
Syntax
.to-top {
position: fixed;
bottom: 20px;
right: 20px;
}
Take care that fixed elements never hide important content, especially on small screens.
Sticky
A sticky element scrolls like normal until it reaches the offset you set, then it stays in place inside its parent. A sticky site header uses top: 0.
Syntax
.site-header {
position: sticky;
top: 0;
background-color: white;
}
Stacking order with z-index
When positioned elements overlap, z-index decides which is in front. A higher number sits on top. It only works on positioned elements (anything except static).
Example
<style>
.stack { position: relative; height: 110px; }
.stack div {
position: absolute;
width: 120px;
height: 80px;
color: white;
padding: 6px;
}
.one { background-color: #0d6efd; left: 0; top: 0; z-index: 1; }
.two { background-color: crimson; left: 50px; top: 20px; z-index: 2; }
</style>
<div class="stack">
<div class="one">z-index 1</div>
<div class="two">z-index 2</div>
</div>
Output
Positioning is powerful but easy to overuse. For page layouts, prefer Flexbox and Grid. Use position for small details such as badges, overlays, and sticky bars.
Common mistakes
- Using
absolutewithout a positioned parent, so the element jumps to the corner of the page. - Setting
toporlefton astaticelement, which does nothing. - Huge
z-indexnumbers such as 99999. Keep a small, organised scale. stickyfailing because a parent hasoverflow: hidden.
Exercise
Place .tag in the bottom-right corner of .box, 6px from each edge. Remember to give the parent a position.
Show solution
<style>
.box {
position: relative;
width: 200px;
height: 100px;
background-color: #eaf5ff;
border: 1px solid #0d6efd;
}
.tag {
position: absolute;
bottom: 6px;
right: 6px;
}
</style>
<div class="box">
<span class="tag">Tag</span>
</div>
Knowledge check
1. Which position value is the default?
static
2. An absolute element is placed relative to what?
Its nearest ancestor with a position other than static.
3. Which property decides which overlapping element is in front?
z-index
Summary
- The five values are static, relative, absolute, fixed, and sticky.
- Give the parent
position: relativeto anchor an absolute child. - Use
z-indexfor stacking, and use positioning for details rather than whole layouts.