TExcelTAD
Home › CSS › CSS Position

CSS Position

The position property takes an element out of normal flow (or not) and lets you place it with the offset properties top, right, bottom and left. It's how you build badges, tooltips, sticky headers, overlays and floating buttons.

The five values

Value In normal flow? Offsets are relative to Typical use
static (default) Yes — offsets ignored Normal content
relative Yes (space kept) Its own normal position Nudging; anchor for absolute children
absolute No Nearest positioned ancestor Badges, overlays, tooltips
fixed No The viewport Floating buttons, fixed headers
sticky Yes, until a threshold Scroll container Sticky headers, table headings

static and relative

A relative element is shifted visually, but the space it originally occupied stays reserved — neighbouring elements don't move.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  div { width: 140px; padding: 12px; margin: 6px; color: white; }
  .s { background: #78909c; }
  .r { position: relative; top: 20px; left: 60px; background: #e91e63; }
</style>
</head>
<body>
  <div class="s">static</div>
  <div class="r">relative: top 20, left 60</div>
  <div class="s">static — I didn't move</div>
</body>
</html>

absolute

An absolute element is removed from flow and positioned against its containing block: the nearest ancestor whose position is not static. If there is none, it uses the page itself. That's why you usually set position: relative on the parent.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 20px; }
  .card { position: relative; width: 220px; height: 130px; background: #e3f2fd; border-radius: 12px; padding: 14px; }
  .badge { position: absolute; top: -10px; right: -10px; background: #f44336; color: white;
           width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; font-weight: bold; }
  .footer { position: absolute; bottom: 10px; left: 14px; right: 14px; background: #1976d2; color: white; padding: 6px; border-radius: 6px; }
</style>
</head>
<body>
  <div class="card">
    Inbox
    <span class="badge">3</span>
    <div class="footer">pinned to the bottom</div>
  </div>
</body>
</html>

Tip: Setting both left and right (or top and bottom) on an absolute element stretches it between them. inset: 0 is a shorthand that covers the whole containing block — perfect for overlays.

fixed

A fixed element is positioned relative to the viewport, so it stays in place when the page scrolls. Scroll inside the frame:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  .content { height: 900px; padding: 16px; background: linear-gradient(#fffde7, #ffe0b2); }
  .fab { position: fixed; right: 16px; bottom: 16px; width: 56px; height: 56px; border-radius: 50%;
         background: #6200ea; color: white; font-size: 30px; display: grid; place-items: center;
         box-shadow: 0 4px 10px rgba(0,0,0,.3); }
</style>
</head>
<body>
  <div class="content">Scroll down — the purple button stays put.</div>
  <div class="fab">+</div>
</body>
</html>

sticky

A sticky element behaves like relative until you scroll past a threshold (e.g. top: 0), then it sticks like fixed — but only within its parent.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  h3 { position: sticky; top: 0; margin: 0; padding: 8px 12px; background: #00897b; color: white; }
  p { margin: 0; padding: 12px; height: 160px; background: #e0f2f1; border-bottom: 1px solid #b2dfdb; }
</style>
</head>
<body>
  <h3>Section A</h3><p>Scroll down. The heading sticks to the top until section B pushes it away.</p>
  <h3>Section B</h3><p>Each sticky header only sticks inside its own parent — here, the body.</p>
  <h3>Section C</h3><p>The end.</p>
</body>
</html>

Note: sticky needs a threshold (top, bottom…) and fails if an ancestor has overflow: hidden.

Test yourself

An absolutely positioned element is placed relative to…

  1. The viewport always
  2. Its own normal position
  3. The nearest ancestor with position other than static
  4. The previous sibling

Which value keeps an element's original space reserved while shifting it visually?

  1. absolute
  2. relative
  3. fixed
  4. static

Which value makes a header stick to the top only after you scroll past it?

  1. fixed
  2. absolute
  3. sticky
  4. relative