Reading history

CSS Position: static, relative, absolute, fixed and sticky

5 min read 0 views

Learn CSS positioning with live examples: relative and absolute positioning, containing blocks, fixed and sticky elements, offsets and z-index.

Normally the browser places elements one after another in the normal flow: blocks stack down the page and inline elements run along a line. The CSS position property lets you break out of that flow when you need to: nudging an element, pinning a badge to the corner of a card, keeping a header visible while the page scrolls, or layering pop-ups above everything else. This lesson explains the five position values, the offset properties and z-index.

In this lesson you will learn

  • static, relative, absolute, fixed and sticky
  • Using top, right, bottom and left
  • What the “containing block” of an absolute element is
  • Making a sticky header
  • Controlling overlap with z-index

This lesson builds on the CSS box model.

relative and absolute

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Positioning</title>
  <style>
    body { font-family: sans-serif; margin: 8px; }
    .box { background: #00695c; color: white; padding: 6px 10px; border-radius: 6px; display: inline-block; }
    .relative { position: relative; top: 10px; left: 30px; background: #ef6c00; }
    .card { position: relative; background: #eceff1; height: 110px; margin-top: 24px; border-radius: 8px; }
    .badge { position: absolute; top: 8px; right: 8px; background: #c62828; color: white;
             padding: 2px 8px; border-radius: 10px; font-size: 13px; }
    .corner { position: absolute; bottom: 8px; left: 8px; }
  </style>
</head>
<body>
  <span class="box">static</span>
  <span class="box relative">relative: moved 10px down, 30px right</span>
  <span class="box">static</span>
  <div class="card">
    The card is position: relative
    <span class="badge">NEW</span>
    <span class="box corner">absolute, bottom-left</span>
  </div>
</body>
</html>

Result

  • static is the default: the element sits in the normal flow and ignores top, left and the other offsets.
  • position: relative moves the element from where it would normally be. Its original space is kept, so neighbours do not move up to fill it.
  • position: absolute removes the element from the flow and positions it against its containing block: the nearest ancestor that is itself positioned (anything except static). If there is none, it uses the page.
  • That is why the card has position: relative with no offsets: it becomes the anchor, so top: 8px; right: 8px puts the “NEW” badge in the card’s corner rather than the page’s.

sticky, fixed and z-index

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sticky and z-index</title>
  <style>
    body { font-family: sans-serif; margin: 0; }
    header { position: sticky; top: 0; background: #1565c0; color: white; padding: 10px; z-index: 10; }
    p { margin: 10px; line-height: 1.6; }
    .stack { position: relative; height: 90px; margin: 10px; }
    .stack div { position: absolute; width: 90px; height: 60px; color: white; padding: 4px; border-radius: 6px; }
    .a { background: #6a1b9a; left: 0;    top: 0;    z-index: 1; }
    .b { background: #2e7d32; left: 40px; top: 20px; z-index: 3; }
    .c { background: #ef6c00; left: 80px; top: 10px; z-index: 2; }
  </style>
</head>
<body>
  <header>Sticky header: scroll inside this box</header>
  <div class="stack"><div class="a">z 1</div><div class="b">z 3</div><div class="c">z 2</div></div>
  <p>Scroll down. The header stays at the top of this frame because of position: sticky and top: 0.</p>
  <p>Paragraph two of filler text to make the page long enough to scroll.</p>
  <p>Paragraph three. Sticky elements behave like relative ones until they reach their offset.</p>
  <p>Paragraph four. Then they stick in place while their parent is visible.</p>
  <p>Paragraph five, the end.</p>
</body>
</html>

Result

  • position: sticky; top: 0 makes the header scroll normally until it reaches the top, then stay there while its parent is on screen. Scroll inside the result frame to see it. Sticky needs an offset such as top to work.
  • position: fixed pins an element to the browser window itself, so it never scrolls, as with chat buttons or cookie bars. Like absolute, it leaves the normal flow.
  • When positioned elements overlap, z-index decides which is on top: higher numbers win. Green (z-index 3) covers orange (2), which covers purple (1). z-index only affects positioned elements and flex or grid items.

Position Values

ValueIn normal flow?Offsets relative toTypical use
staticYesNot usedDefault
relativeYes (space kept)Its own normal positionSmall nudges, anchor for absolute children
absoluteNoNearest positioned ancestorBadges, tooltips, dropdowns
fixedNoThe browser windowFloating buttons, fixed navbars
stickyYes, until it sticksIts scrolling containerSticky headers, table headings

To centre an absolute element: top: 50%; left: 50%; transform: translate(-50%, -50%);. For page layout itself, prefer Flexbox and Grid; positioning is for special cases.

Common Mistakes

  1. Forgetting to position the parent, so an absolute child jumps to a corner of the whole page.
  2. Using absolute positioning for whole layouts, which breaks on different screen sizes.
  3. Sticky not sticking because no top is set or an ancestor has overflow: hidden.
  4. Raising z-index to 9999 without understanding stacking contexts; a parent with its own z-index or a transform limits how high a child can go.

Practice Exercises

Exercise 1. Place a “Sale” label in the top-left corner of a product image.

Show answer
.product { position: relative; }
.product .sale { position: absolute; top: 0; left: 0; background: #c62828; color: #fff; padding: 4px 8px; }

Exercise 2. Make a “Back to top” button that always stays 20px from the bottom-right of the window.

Show answer
.to-top { position: fixed; right: 20px; bottom: 20px; z-index: 100; }

Frequently Asked Questions

What is the difference between fixed and sticky?

A fixed element is always pinned to the window and takes no space in the layout. A sticky element behaves normally until you scroll to its offset, then sticks, and only within its parent.

Why does z-index not work on my element?

It needs a position other than static (or to be a flex or grid item). Also check whether a parent creates its own stacking context, which caps the child’s level.

Summary

  • relative nudges an element and anchors absolute children.
  • absolute positions against the nearest positioned ancestor.
  • fixed pins to the window; sticky sticks once scrolled to its offset.
  • z-index orders overlapping positioned elements.

Next: Responsive Web Design.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *