Positioning: relative, absolute, Offsets, inset and the Containing Block
The position property and its offsets: relative nudging, absolute positioning against the nearest positioned ancestor, the static position, inset and its logical forms, stretching with opposite offsets, centering, and common patterns.
kvi5hnuCode Sprout team member8 min readOn this page
Positioning takes an element out of its normal place and puts it somewhere else: a badge in the corner of a card, an icon inside an input, a caption over an image, a dropdown below a button. The position property chooses the scheme, and the offset properties, top, right, bottom, left and the inset shorthands, say where. This article covers relative and absolute; article 57 covers fixed and sticky, and article 58 how positioned elements stack.
The position values
| Value | In flow? | Offsets move it relative to... |
|---|---|---|
static (initial) | yes | offsets don't apply |
relative | yes, keeps its space | its own normal position |
absolute | no, removed from flow | its containing block: the nearest positioned ancestor's padding box |
fixed | no | the viewport (article 57) |
sticky | yes | its scroll container, once a threshold is reached (article 57) |
An element whose position isn't static is called positioned. Positioned elements can use z-index (article 58), and they become the containing block for absolutely positioned descendants.
The offsets
top, right, bottom and left set the distance from the corresponding edge of the reference box, inward. Positive values move toward the center: top: 1rem moves down, left: 1rem moves right. Negative values move outward.
The inset shorthand sets all four, in the same top, right, bottom, left order as margin: inset: 0 puts the element on all four edges. Its logical forms follow the writing mode (article 47):
.badge { position: absolute; inset-block-start: 0.5rem; inset-inline-end: 0.5rem; } /* top right in English */
.overlay { position: absolute; inset: 0; } /* covers the containing block */
Percentages refer to the containing block: top and bottom to its height, left and right to its width.
position: relative
A relatively positioned element is laid out normally, then shifted by its offsets. Its original space stays reserved, so nothing around it moves:
.nudge { position: relative; top: 2px; } /* 2px lower, the layout unchanged */
If you set opposite offsets, one wins: top beats bottom, and in left-to-right text left beats right. Relative positioning can't stretch an element.
Shifting things is rarely the reason to use it. Usually position: relative is set with no offsets at all, just to make the element the containing block for absolutely positioned children, and to let it take a z-index.
position: absolute
An absolutely positioned element is removed from normal flow: the content around it closes up as if it weren't there. It's placed relative to its containing block, the padding box of the nearest ancestor that is positioned (or has a transform, filter or a few other properties, article 44). If there's none, it's the initial containing block, the size of the viewport at the top of the page.
.card { position: relative; } /* the reference box */
.card .badge {
position: absolute;
top: 0.75rem;
right: 0.75rem; /* 0.75rem in from the card's top-right padding corner */
}
Absolutely positioned elements are blockified, like floats (article 54): a positioned <span> computes to display: block. Margins on them never collapse.

The same two offsets mean different things: for relative they're measured from the box's own normal position, for absolute from the containing block's padding edge.
Sizing
Without a width, an absolutely positioned element shrinks to fit its content. Give it both of a pair of opposite offsets instead, and it stretches between them:
.caption { position: absolute; left: 0; right: 0; bottom: 0; } /* full width along the bottom */
.overlay { position: absolute; inset: 0; } /* fills the whole padding box */
With both offsets and a width set, the element is overconstrained. Then auto margins share the leftover space, which centers it, as below; without auto margins, right (or the end offset) is ignored.
The static position
If all of an axis's offsets are auto, the element stays where it would have been in normal flow, its static position, though still out of flow. An absolutely positioned element with no offsets at all appears in its normal place, and the content after it moves up underneath. That's useful for a dropdown that should start right below its trigger: set only top, or nothing, and the horizontal position stays where it was.
Centering
Two modern ways to center an absolutely positioned element in its containing block:
/* 1: stretch to all edges, give it a size, let auto margins share the rest */
.modal { position: absolute; inset: 0; margin: auto; inline-size: 20rem; block-size: fit-content; }
/* 2: self-alignment, in all browsers since 2025 */
.modal { position: absolute; inset: 0; place-self: center; inline-size: fit-content; block-size: fit-content; }
align-self, justify-self and place-self work on absolutely positioned elements, aligning them within the area between their offsets: Chrome 122, Firefox 134 and Safari 26. The older top: 50%; left: 50%; transform: translate(-50%, -50%) also works, but the transform can blur text on some screens and gets in the way of other transforms.

The last panel is the static position: without offsets, the box stays where it would have been in the line, but the text no longer makes room for it.
body { font: 0.85rem/1.45 system-ui, sans-serif; margin: 1rem; display: grid; gap: 1rem; }
.card { position: relative; border: 1px solid #d8cfc0; border-radius: 10px; padding: 0.5rem; }
.badge {
position: absolute; top: -0.5rem; inset-inline-end: -0.5rem;
background: #8a3b12; color: white; font-weight: 700; padding: 0.1rem 0.5rem; border-radius: 99px;
}
.cover { position: relative; block-size: 7rem; border-radius: 6px; background: linear-gradient(#7fb4d1 0 55%, #2f7d4f 55%); }
.caption {
position: absolute; inset-inline: 0; bottom: 0; margin: 0;
padding: 0.4rem 0.6rem; color: white; font-weight: 700;
background: linear-gradient(transparent, rgb(0 0 0 / 0.7));
}
.card > p { margin: 0.4rem 0 0; }
.search { position: relative; display: block; }
.search input { box-sizing: border-box; inline-size: 100%; font: inherit; padding: 0.45rem 0.6rem 0.45rem 2rem; border: 1px solid #8a8070; border-radius: 6px; }
.search .icon { position: absolute; inset-block: 0; inset-inline-start: 0.6rem; display: grid; place-items: center; color: #555; pointer-events: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.frame { position: relative; block-size: 9rem; border: 2px dashed #8a3b12; padding: 0.5rem; }
.frame p { margin: 0; }
.centered { position: absolute; inset: 0.5rem; place-self: end center; inline-size: fit-content; block-size: fit-content; background: #f4c95d; padding: 0.3rem 0.6rem; border-radius: 6px; }
.static { position: absolute; inline-size: 9rem; background: rgb(47 125 79 / 0.85); color: white; padding: 0.2rem; }
Common patterns
- Badges and close buttons:
position: absolutewithtopandinset-inline-end, inside aposition: relativecard. Negative offsets let them overlap the edge. - Overlays and captions:
inset: 0, or offsets on three sides, inside a positioned image wrapper. - Icons in inputs: a positioned label or wrapper, the icon absolutely placed, padding on the input to make room, and
pointer-events: noneso clicks reach the input. - Stretched links: a card whose whole area is clickable, using a link's
::afterwithposition: absolute; inset: 0. Keep other interactive elements in the card above it withposition: relativeand az-index. - Visually hidden text: the
.sr-onlypattern (article 46) uses absolute positioning to take text out of the layout.
For tooltips and menus that should follow a button anywhere on the page, even outside its container, anchor positioning (article 59) is now the better tool.
Things to watch
- An absolutely positioned element doesn't take space, so its container won't grow to fit it, and text can end up underneath it. Reserve room with padding.
- An ancestor with
overflow: hiddenclips absolutely positioned descendants only if it is, or contains, their containing block. A clipped dropdown often means theposition: relativeelement is inside the clipping one. - Absolute positioning changes where things appear, not their order for keyboard and screen reader users. Keep the HTML order meaningful.
What's current
inset,inset-blockandinset-inlinework in all browsers.- Self-alignment (
place-self,align-self,justify-self) on absolutely positioned elements: Chrome 122, Firefox 134, Safari 26. - Anchor positioning (article 59) handles positioning relative to elements other than the containing block.
Common questions
Why is my absolutely positioned element relative to the page instead of its parent?
No ancestor is positioned. Add position: relative to the parent you want as the reference.
Why did the content below jump up when I made an element absolute? Absolute positioning removes the element from normal flow, so it no longer takes space.
What's the difference between relative and absolute?
relative keeps the element in flow and shifts it from its normal position. absolute removes it from flow and places it relative to the nearest positioned ancestor.
How do I make an absolutely positioned element fill its parent?
inset: 0, with the parent positioned.
Why is my absolute element only as wide as its text?
Absolutely positioned elements shrink to fit. Set a width, or both left and right.
Summary
relativeshifts an element from its normal position and keeps its space; it's mostly used to create a containing block.absoluteremoves an element from flow and places it in the padding box of the nearest positioned ancestor.- Offsets go inward from each edge;
insetsets all four, with logical forms. - Without offsets, an absolute element stays at its static position. Opposite offsets stretch it; without them it shrinks to fit.
- Center with
inset: 0plusmargin: autoorplace-self: center. - Absolutely positioned elements are blockified and their margins don't collapse.
Practice questions
Write CSS for a "Sale" badge 0.5rem inside the top-left corner of .product, in any writing direction.
Answer
.product { position: relative; }
.product .sale {
position: absolute;
inset-block-start: 0.5rem;
inset-inline-start: 0.5rem;
}
How wide is the .bar below, and where is it?
.panel { position: relative; width: 500px; padding: 20px; }
.panel .bar { position: absolute; left: 10px; right: 10px; bottom: 0; }
Answer
520px wide, along the bottom of the panel's padding box. The padding box is 500 + 2 × 20 = 540px wide, and the bar stretches between offsets of 10px on each side: 540 − 20 = 520px.
An element has position: relative; top: 1rem; bottom: 3rem;. Where does it move?
Answer
1rem down. With opposite offsets on a relatively positioned element, top wins and bottom is ignored.
Center a 16rem-wide notice horizontally and vertically inside a positioned container, without transforms.
Answer
.notice { position: absolute; inset: 0; margin: auto; inline-size: 16rem; block-size: fit-content; }
Previous: CSS Shapes: shape-outside, shape-margin and Non-Rectangular Text Wrap
Next: Fixed and Sticky Positioning: Headers, Modals and Sticky Sections

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.