Coding/Learn/css/positioning
CSS Beginner Course
01.Introduction to CSS
02.Selectors (Element, Class & ID)
03.3 Ways to Add CSS to HTML
04.Colors & Backgrounds (HEX, RGB, HSL)
05.Typography & Fonts (font-size, font-weight, line-height)
06.The Cascade & Specificity Wars
07.The Sacred CSS Box Model
08.Padding vs Margin & Shorthand Notation
09.Borders & Border Radius (Rounded Corners & Circles)
10.The Magic of box-sizing: border-box
11.Sizing & Units (px, rem, em, %, vh, vw)
12.The Display Property (block, inline, inline-block, none)
13.Flexbox Part 1: Container Powers (justify, align, gap)
14.Flexbox Part 2: Direction, Wrapping & Item Powers
15.Positioning (relative, absolute, fixed, sticky)
16.Z-Index & The 3D Stacking Context
17.Pseudo-Classes (:hover, :active, :focus, :first-child)
18.Smooth Transitions & Box Shadows
19.Responsive Design & Media Queries (@media)
20.Capstone: Crafting a Modern UI Profile Card
Course Certificate0%

Complete all lessons to earn

LearnCSS Positioning (relative, absolute, fixed, sticky)
15 / 20 0%
Lesson 15 of 20
5 mins read

CSS Positioning (relative, absolute, fixed, sticky)

PreviousNext Lesson
In Plain English

The position property breaks elements out of normal document flow to place them at exact coordinates (top, right, bottom, left). Master the 4 key modes: relative, absolute, fixed, and sticky.

The Mental Model

"position: relative is marking your current seat with a bookmark. position: absolute is stepping out of the line to stand in the top-right corner of the room. position: fixed is strapping a helmet to your eyes so it stays in your vision as you walk. position: sticky is scrolling until you hit the ceiling, then sticking like a magnet."

Deep Dive: How It Works

position: relative: Element stays in normal flow, but unlocks coordinate offsets (top, left) AND serves as an anchor parent for absolute children.

position: absolute: Breaks completely out of page flow. Positions itself relative to the nearest parent with position: relative (or the whole page if none exists).

position: fixed: Locks itself relative to the browser viewport window. Stays frozen in place as the user scrolls (e.g. chat bubble widget).

position: sticky: The modern hybrid! Scrolls naturally like a normal element until it reaches a threshold (e.g. top: 0;), then sticks to the top of the screen!

Syntax Blueprint

/* Parent Anchor */
.card {
  position: relative;
}

/* Absolute Badge in Top Right Corner */
.badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

/* Sticky Header */
.header {
  position: sticky;
  top: 0;
  z-index: 50;
}

Relative parent anchors absolute child; sticky pins headers during scroll.

PointCore Rules to Remember

Point star
The Relative/Absolute Pair: Always put position: relative on the parent when using position: absolute on a child.
Point star
Sticky Requires a Coordinate: position: sticky does nothing without a threshold like top: 0;.
Point star
Fixed for Overlays: Used for modals, mobile bottom navigation bars, and toast notifications.

Common Beginner Traps & How to Fix Them

Mistake:Using position: absolute without a relative parent

Why it happens: The absolute element flies to the top-right of the entire browser window instead of the card!

How to fix: Always add position: relative; to the parent card container.

Pro Tip: ZenCompiler's top floating navbar uses position: sticky; top: 12px; to stay pinned as you browse lessons!

Live Interactive Example

Hit Run Code to see it live
Corner Notification Badge & Sticky Header
CSS3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: The dot anchors to the bell's top-right corner using relative/absolute positioning.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create an absolute corner badge

  • PointWrite a CSS rule for .corner-badge with position: absolute; top: 0; and right: 0;
1
2
Browser Preview & Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates