Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

CSS Positioning Explained: Relative, Absolute, Fixed, and Sticky

A practical guide to CSS positioning: compare flow behavior, understand containing blocks, learn sticky’s scroll limits, and debug common placement problems.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a CSS positioning value by asking three questions: does the element keep its space in normal layout, what determines its offsets, and how does it behave while scrolling? relative keeps its place in the flow; absolute and fixed do not; sticky keeps its place and can become offset while scrolling. The details of the containing block and scroll ancestors determine where each one goes.

Start with normal flow

Before positioning, an element’s default is position: static. It takes part in normal document layout, and inset properties such as top and left do not reposition it. Normal flow determines where the element sits and how neighboring content lays out around it.

The four positioning values below change that baseline in different ways. An element can be visually displaced without its original space moving, or removed from the flow altogether. Its offsets also need a reference: that may be its original position, a containing block, a scrolling ancestor, or usually the viewport.

Compare the four positioning values

Value Keeps normal-flow space? Typical positioning reference Teaching example
relative Yes Its normal position for offsets; can establish a local containing block Nudge a label or anchor an overlay within a component
absolute No Nearest applicable containing block Badge, icon, or overlay inside a card with a positioned parent
fixed No Usually the viewport in visual media; ancestor properties can change the containing block Persistent help button or viewport-level control
sticky Yes Nearest scrolling ancestor and containing block Section heading or table header that sticks during a scroll interval

These are patterns for understanding layout, not rules that every design should follow. The right choice depends on flow participation, positioning reference, and scrolling behavior together.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When should you use position: relative?

Use relative when an element should retain its original place in layout but appear offset from that position. For example, a label can be nudged slightly without causing the content after it to move into the vacated space. The element’s original flow space remains reserved; its painted position moves.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

relative is also commonly used to make an element the local containing block for an absolutely positioned descendant. That gives a component such as a card a deliberate reference for an internal badge or icon, rather than leaving that descendant to be positioned against a more distant ancestor.

When should you use position: absolute?

Use absolute for an element that should not take up its usual space in normal flow and whose offsets should be measured against a containing block. A common teaching example is a badge positioned inside a card: make the card a positioned parent, then place the badge within that card’s bounds.

An absolutely positioned element uses the nearest applicable containing block, commonly the nearest ancestor whose position is not static. If no applicable ancestor establishes one, its offsets are resolved against the initial containing block. If an overlay appears in an unexpected location, inspect its ancestors to find the reference it is actually using.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When should you use position: fixed?

Use fixed when an element should be removed from normal flow and typically stay in a viewport-relative position as the page scrolls, such as a persistent help control. In visual media, the viewport is the usual reference, but it is not guaranteed: an ancestor with a non-none transform, perspective, or filter can establish a different containing block.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

If fixed content moves with a region or seems attached to a component, inspect those ancestor properties before changing its offsets. “Fixed” does not unconditionally mean “fixed to the viewport.”

How does position: sticky work?

sticky keeps its normal-flow space. As scrolling reaches the relevant inset threshold, the element is offset relative to its nearest scrolling ancestor, while remaining constrained by its containing block. It therefore sticks only over the portion of scrolling allowed by that containing block; it is not simply fixed to the viewport for the whole page.

Set a non-auto inset on the axis where the element should stick. For example, a top-sticking heading needs a non-auto top inset. MDN notes that “At least one inset property (top, inset-block-start, right, inset-inline-end, etc.) needs to be set to a non-auto value for the axis on which the element needs to be made sticky.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a useful mental model, imagine a heading inside a bounded section on a tall scrolling page. The heading can stick as the page scrolls through that section, but the section’s bounds limit how long it can remain offset. An ancestor with overflow: hidden, auto, scroll, or overlay may determine the sticky scrolling mechanism—even if a different ancestor is the one visibly scrolling.

Why is my positioned element in the wrong place or behind something?

  • An absolute element is misplaced: Find its nearest applicable containing block by inspecting ancestors. If the design calls for positioning inside a component, establish an intentional local context there.
  • A fixed element scrolls with a region: Check ancestors for a non-none transform, perspective, or filter that may change its containing block.
  • A sticky element never sticks: Check for a non-auto inset on the intended axis, then inspect ancestors with overflow: hidden, auto, scroll, or overlay. Also check whether its containing block provides enough room for a sticky interval.
  • An element overlaps content unexpectedly: Check stacking contexts before increasing z-index. Fixed and sticky elements always create stacking contexts; relative and absolute elements create one when z-index is not auto. A child’s high z-index is interpreted inside its own stacking context, so it cannot necessarily rise above content in another context.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose by behavior, not by a slogan

  • Choose relative to preserve flow space while offsetting an element, or to give an absolute descendant a local reference.
  • Choose absolute when the element should not reserve normal-flow space and should be positioned against a containing block.
  • Choose fixed when the element should leave normal flow and usually remain viewport-relative during scrolling, after checking ancestor effects.
  • Choose sticky when the element should remain in flow but become offset during a bounded scrolling interval.

For further CSS study, the publisher page for CSS in Depth, Second Edition describes a broader CSS reference. Manning’s CSS in Depth catalog entry lists positioning and stacking contexts among its topics.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.