Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Positioning | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 11 of 50

·css

Positioning

11 mins

Course progress22%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `relative` is useful for small tweaks; it does **not** affect surrounding elements.
  • `absolute` needs a positioned ancestor (`position:relative|absolute|sticky`).
  • `sticky` allows a header or nav to stick after scrolling past its original spot.

CSS positioning moves an element outside or inside the normal document flow.

ValueDescription
staticDefault – element follows normal flow.
relativeOffset from its static position; space is still reserved. Good for slight nudges or for establishing a positioned ancestor for absolute children.
absoluteRemoved from flow; positioned relative to the nearest positioned ancestor (relative, absolute, fixed, sticky).
fixedRemoved from flow; anchored to the viewport (doesn't move on scroll).
stickyActs like relative until a scroll threshold is met, then behaves like fixed. Perfect for sticky headers or table‑of‑contents.

Example – Sticky Header

header {
  position:sticky;
  top:0;               /* sticks when its top reaches the viewport top */
  background:#fff;
  z-index:10;          /* stay on top of other content */
  box-shadow:0 2px 4px rgba(0,0,0,0.1);
}

Visual Aid – Positioning Context

+-------------------+   <-- .parent (position:relative)
| .child (absolute) |
+-------------------+

Because the parent is positioned, the child’s top/left/right/bottom are calculated relative to the parent, not the document.

Common Pitfall

If you forget to give the ancestor a position value, an absolute child will jump to the document root, breaking your layout. Always double‑check the ancestor chain.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonDisplay PropertyContinue to lesson 12Floating & ClearingBack to CSS Basics to Advanced curriculum