CHAPTER 22
CSS Position
Learn how to control the position of HTML elements using static, relative, absolute, fixed and sticky positioning.
01. What is CSS Position?
The position property controls how an element is positioned in the document.
Common values are static, relative, absolute, fixed and sticky.
Positioning is useful for menus, badges, overlays, floating buttons and special layouts.
02. Syntax
selector {
position: value;
top: value;
right: value;
bottom: value;
left: value;
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
.box {
position: relative;
width: 300px;
height: 180px;
background: #2563eb;
}
.badge {
position: absolute;
top: 15px;
right: 15px;
background: #facc15;
padding: 8px 12px;
}
</style>
</head>
<body>
<div class="box">
<div class="badge">
New
</div>
</div>
</body>
</html>
04. Important Points
✓
static is the default position.
✓
relative keeps the element in normal flow while allowing offsets.
✓
absolute positions an element relative to a positioned ancestor.
✓
fixed keeps an element attached to the viewport.
✓
sticky combines normal scrolling with a sticky position.
05. Common Mistakes
⚠
Using absolute positioning for the entire website layout.
⚠
Forgetting to give the parent position: relative when using absolute children.
⚠
Creating fixed elements that cover important mobile content.
06. Real World Use
Position is used for notification badges, dropdowns, floating buttons, overlays, sticky headers and image labels.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Create a Badge
Create a card using position: relative and place a small 'New' badge in its top-right corner using position: absolute.