CSS Reference · Positioning and Stacking
The inset property
Reference for the CSS inset shorthand: set top, right, bottom and left in one declaration, with one to four values, plus overlay and stretch patterns.
The inset property is a shorthand for top, right, bottom and left. It accepts one to four values and follows the same pattern as margin. Like the longhands, it only affects positioned elements.
Its best-known trick is inset: 0 on an absolutely positioned element, which stretches the element to fill its containing block. That is the shortest way to build overlays, backdrops and full-bleed layers.
.overlay { position: absolute; inset: 0; }
Try it
Syntax
inset: <'top'>{1,4}; inset: 0; /* all four sides */ inset: 10px 20px; /* top/bottom 10px, left/right 20px */ inset: 0 auto auto 0; /* top, right, bottom, left */
- Initial value:
auto(for each longhand) - Applies to: positioned elements
- Inherited: no
- Percentages: vertical offsets use the containing block's height, horizontal ones its width
- Animatable: yes, as lengths or percentages
- Computed value: as for the longhands
Values
| Number of values | Meaning |
|---|---|
| One | All four sides. |
| Two | First value: top and bottom. Second: left and right. |
| Three | Top, then left and right, then bottom. |
| Four | Top, right, bottom, left (clockwise). |
Each value can be auto, a <length>, a <percentage>, or an anchor() expression.
Usage notes
inset: 0stretch. Withposition: absolute(orfixed) and no width/height, all four edges pin to the containing block. Use it for modal backdrops and image overlays.- Centering a sized box.
inset: 0; margin: auto; width: 300px; height: 200px;centers an absolutely positioned element both ways. - Positioned only. On a
staticelement,insetdoes nothing. Dialogs opened withshowModal()are positioned by the user agent, and the defaultinsetthere is0. - Physical, not logical. Order is top, right, bottom, left regardless of writing mode. For direction-aware offsets use
inset-blockandinset-inline. - Cascade caution. A later
top: 10pxoverrides the top part of an earlierinset, and vice versa.
Browser support
Supported in all modern browsers since 2021 (Baseline). Very old browsers need the four separate longhands.
Related
top,right,bottom,leftinset-block,inset-inlineposition- Lesson: CSS Position