DevAcademy
LearnCSSPositioning
IntermediateCSS

Positioning

Learn the five CSS position values — static, relative, absolute, fixed, and sticky — and how they affect layout.

Reading Time

18 min

Lesson

Lesson 13 of 30

The position Property

position controls how an element is placed in the document, and works together with top, right, bottom, and left to offset it.

Position Values

ValueBehavior
staticDefault. Normal document flow; top/left/etc. have no effect
relativeStays in normal flow, but can be offset from its normal position
absoluteRemoved from flow, positioned relative to nearest positioned ancestor
fixedRemoved from flow, positioned relative to the viewport, stays put when scrolling
stickyActs relative until a scroll threshold, then sticks like fixed

relative Positioning

<style>
  .shifted {
    position: relative;
    top: 10px;
    left: 20px;
    background: #ffeaa7;
    display: inline-block;
    padding: 10px;
  }
</style>

<div class="shifted">Shifted 10px down, 20px right from its normal spot</div>
Output

absolute Positioning

An absolutely positioned element is removed from normal flow and positioned relative to its nearest ancestor that has a position other than static — commonly used for badges, tooltips, and dropdown menus.

absolute Inside relative

<style>
  .card {
    position: relative;
    width: 200px;
    height: 100px;
    background: #eef4fb;
    border: 1px solid steelblue;
  }
  .badge {
    position: absolute;
    top: 8px;
    right: 8px;
    background: crimson;
    color: white;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 12px;
  }
</style>

<div class="card">
  <span class="badge">New</span>
  Card content
</div>
Output

fixed and sticky

fixed keeps an element pinned to the viewport regardless of scrolling — common for sticky headers or "back to top" buttons. sticky behaves like relative until the page scrolls past a threshold, then behaves like fixed within its parent.

absolute Needs a Positioned Ancestor

If no ancestor has position: relative, absolute, or fixed, an absolutely positioned element is placed relative to the entire page — a very common source of unexpected layout bugs.

Best Practice

Always pair position: absolute with a nearby position: relative parent that acts as its containing box, so the offset behaves predictably.

Interview Questions

Quick Quiz

1. Which position value is the default for every element?

2. What is an absolutely positioned element positioned relative to?

3. How does position: sticky behave?