# Positioning Schemes

> Break elements out of the normal document flow to create overlays, sticky headers, and fixed floating buttons.

Subject: CSS | Lesson 17 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/position-schemes
Previous: [Borders, Radius & Shadows](https://codewithanimation.in/css/borders-radius-and-shadows.md) | Next: [Z-index & Stacking Context](https://codewithanimation.in/css/z-index-and-stacking.md)

## What it is

By default, browsers place HTML elements onto the page one after another, from top to bottom. This is called the 'normal document flow'.

The `position` property allows you to hijack this behavior. You can nudge an element away from its original spot, remove it from the flow entirely so other elements ignore it, or lock it to the screen so it never scrolls away.

## Why it matters

Complex UI patterns are impossible with normal flow alone. If you want a navigation bar that stays visible as the user scrolls, a modal dialog that hovers over the page, or a notification badge that sits exactly on the top-right corner of an icon, you must use CSS positioning.

## How it works

There are five main values for the `position` property:

1. **static**: The default. The element stays in normal flow. The `top`, `right`, `bottom`, and `left` properties have absolutely no effect.

2. **relative**: Stays in normal flow, but you can visually nudge it using `top`/`left`. Importantly, it acts as an 'anchor' for any absolutely positioned children inside it.

3. **absolute**: Completely removed from the normal flow. It positions itself relative to its closest *positioned* ancestor (any parent that isn't `static`).

4. **fixed**: Removed from the flow and locked to the browser viewport. It stays on screen even when the user scrolls.

5. **sticky**: A hybrid. It acts like `static` (scrolling normally) until it hits a specified threshold (like `top: 0`), at which point it 'sticks' to the screen until its parent container scrolls away.

## Common mistake: The absolute runaway child

The most common mistake beginners make is applying `position: absolute` to a child element and watching it fly across the screen, completely breaking the layout.

This happens because `absolute` elements look up the HTML tree for an anchor. If they don't find a parent with `position: relative`, they keep going until they hit the main `<body>` tag and anchor to the entire page instead of their container.

## Tip: Stacking order

Once you change an element's position from `static` to anything else (relative, absolute, fixed, or sticky), it immediately gets rendered *on top* of all static elements.

If multiple positioned elements overlap, the one that appears later in the HTML will be on top, unless you explicitly change it using `z-index`.

## Try it: Try it

Select different positioning schemes for the highlighted box. Use the scroll area to see how 'fixed' and 'sticky' behave when the page moves.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/position-schemes

## Check yourself

### Question 1

Which position value completely removes an element from the normal document flow and locks it to the browser viewport, so it never scrolls?

- A. fixed
- B. absolute
- C. sticky

Correct answer: A

Fixed positioning anchors the element to the screen (the viewport) itself. This is commonly used for persistent 'Back to top' floating buttons.

- Why not B: Absolute removes the element from the flow, but it scrolls away with the page because it anchors to a parent container, not the viewport.
- Why not C: Sticky only locks to the screen temporarily while its parent container is in view.

### Question 2

If a child element is set to 'position: absolute; top: 0;', what determines which ancestor it anchors to?

- A. The closest ancestor with a position other than 'static'
- B. The direct parent element
- C. The <body> tag

Correct answer: A

An absolutely positioned element searches up the DOM tree for a 'containing block'. It anchors to the first ancestor it finds that has a position of relative, absolute, fixed, or sticky.

- Why not B: It will ignore the direct parent if the parent is position: static.
- Why not C: It only falls back to the <body> tag if no intermediate ancestors are positioned.

### Question 3

Which position value behaves like 'static' until the user scrolls past a threshold, at which point it temporarily acts like 'fixed'?

- A. sticky
- B. relative
- C. absolute

Correct answer: A

Sticky positioning is a hybrid. It scrolls normally until its specified threshold (e.g., top: 0) is met, then it pins itself to the viewport until its parent container scrolls out of view.

- Why not B: Relative just offsets the element visually; it always scrolls exactly like normal flow.
- Why not C: Absolute elements do not change behavior based on scroll thresholds.

## Remember this

- `static` is the default. `top` and `left` do nothing.
- `relative` keeps the element in flow but allows nudging, and acts as an anchor for absolute children.
- `absolute` removes the element from flow and anchors it to the nearest positioned parent.
- `fixed` locks the element to the browser screen.
- `sticky` scrolls normally until a threshold, then sticks to the screen.
