What width does an absolutely positioned element take when both left: 0 and right: 0 are set and width is auto, and what happens when a width is also specified?
answer
- it is one equation with unknowns
- opposing insets make width the unknown
- too many values, something must give
- direction decides which inset loses
- auto margins split the leftover
basics
~20 sWith opposing insets and width: auto, the box stretches to span the containing block minus those insets and its margins. Adding an explicit width over-constrains the equation, so the browser ignores right in a left-to-right writing mode — unless both horizontal margins are auto, which centres the box.
solid answer
~40 sSetting both `left` and `right` on an absolutely positioned box with `width: auto` is the way to make an out-of-flow element stretch: the used width becomes the containing block's width minus the two insets and any margins, which is what `inset: 0` on an overlay relies on. If you also declare a width, the three values cannot all be satisfied — the layout is over-constrained — and the browser resolves it by ignoring `right` when the containing block's direction is left-to-right, or `left` when it is right-to-left. The one escape is `margin-left` and `margin-right` both being `auto`: then the leftover space is split evenly and the box is centred, which is the trick behind `position: absolute; inset: 0; margin: auto` with a fixed width and height.
code
css · 12 lines.overlay {
position: absolute;
inset: 0;
}
.centred {
position: absolute;
inset: 0;
margin: auto;
width: 320px;
height: 200px;
}go deeper
Know the useful half: inset: 0 on an absolutely positioned element makes it fill its containing block without declaring any width or height, which is how full-cover overlays are written.
Explain it as an equation — opposing insets leave width as the unknown, and adding a width over-constrains it so right is dropped in a left-to-right context — and name the auto-margin centring case.
Show why insets beat width: 100% on out-of-flow boxes: they solve for available space rather than adding margins and borders on top, so the result survives borders, padding changes and re-inset requirements.
Own the convention: prefer declarations whose behaviour is derived rather than coincidental, so a stylesheet does not rely on values that only work because the writing direction happens to drop the one that conflicts.
## Three unknowns, one equation Along the horizontal axis, an absolutely positioned box has to satisfy a simple sum: `left + margin-left + border + padding + width + padding + border + margin-right + right` equals the width of the containing block. The browser solves for whatever is `auto`. Which values you leave `auto` decides which behaviour you get. ## Both insets, auto width — the stretch Leave `width: auto` and give both `left` and `right` values, and `width` is the only unknown, so the box stretches to fill whatever is left over. This is the mechanism behind the most common absolute-positioning idiom: ```css .overlay { position: absolute; inset: 0; /* top, right, bottom, left all 0 */ background: rgb(0 0 0 / 0.5); } ``` The overlay has no declared size yet fills its containing block exactly, on both axes — the vertical equation works identically with `top`, `bottom` and `height`. Without the opposing insets, `width: auto` on an out-of-flow box means shrink-to-fit, and the same element would collapse to its content. Non-zero insets work the same way: `left: 16px; right: 16px` produces a box inset 16px from each side of the containing block whatever its width, with no percentage arithmetic and no `calc()`. ## Adding a width — over-constraint Declare `left`, `right` *and* `width` with non-auto margins and there are no unknowns left, but the numbers rarely add up. CSS calls this **over-constrained** and resolves it deterministically rather than by error: the used value of `right` is discarded and recomputed when the containing block's `direction` is `ltr`. In a right-to-left containing block, `left` is dropped instead. Vertically, the same rule discards `bottom`. So `left: 0; right: 0; width: 200px` gives you a 200px box flush to the left edge — the `right: 0` looks like it was ignored, because it was. This is a frequent source of "why is my right offset doing nothing", and the answer is that the box was over-specified and the writing direction decided which declaration lost. ## Auto margins — the centring escape hatch The one case where over-specification is *productive* is when both horizontal margins are `auto`. Then the leftover space is not assigned to an inset but split equally between the two margins, and the box centres inside the space between the insets: ```css .centred { position: absolute; inset: 0; margin: auto; width: 320px; height: 200px; } ``` That centres the box both horizontally and vertically inside its containing block. Note the vertical half: `margin: auto` centres vertically here in a way it never does in normal flow, precisely because the absolute positioning equation has `top`, `bottom` and a definite `height` to work with. It needs a definite size on the axis — with `height: auto` there is no leftover space to split and the box stretches instead. If splitting evenly would make the margins negative — the box is wider than the space between the insets — the spec falls back to setting the start-side margin to zero and solving for the other, so the box overflows the end side rather than being centred. ## One inset only For completeness: with only `left` set and `right: auto`, the box is placed from the left and sized shrink-to-fit; the right edge lands wherever the content ends. With only `right` set, the box's right edge is pinned and it grows leftward. This is how you right-align an out-of-flow badge without knowing its width. ## Choosing between the two idioms Stretching with opposing insets is preferable to `width: 100%` on an absolutely positioned element whenever the box has margins, borders or insets of its own: `width: 100%` measures the containing block and then adds those extras on top, overflowing, while opposing insets solve for the space that is actually available. It also survives a change in the inset values without any recalculation. ## Interview shape This is usually asked as a prediction: "what does this render?" over a snippet with `left`, `right` and `width` all set. The strong answer names the over-constrained rule *and* the direction dependence, then adds the auto-margin case as the exception — showing that the behaviour is a solved equation rather than a set of memorised special cases.
- Why prefer left: 0; right: 0 over width: 100% on an absolutely positioned element?Because opposing insets solve for the space actually available, while `width: 100%` matches the containing block and then adds the element's own margins and borders on top — overflowing unless `box-sizing` and zero margins happen to line up. Insets also keep working unchanged if you later inset the box by 16px a side.
- Does margin: auto centre an absolutely positioned box vertically?Yes, provided both `top` and `bottom` are set and the height is definite. The vertical equation then has leftover space to split evenly between the auto margins. This is one of the few places `margin: auto` centres vertically — in normal flow, auto vertical margins compute to zero.
saying these in an interview costs you the question
- Says setting left, right and width together is simply invalid
- Expects the browser to honour all three by shrinking the box
- Forgets that writing direction decides which inset is dropped
- Claims margin: auto never centres an element vertically
- Uses width: 100% with insets and is surprised by the overflow