π What Is inset?
The inset property is a powerful shorthand for setting an elementβs top, right, bottom, and left offsets in a single declaration. It works on any element with a positioned context:
- position: absolute
- position: fixed
- position: relative
- position: sticky
Note
β Supports 1, 2, 3, or 4 values (like margin/padding)
β Logical alternative: inset-inline + inset-block
π¦ Syntax
Inset syntax
inset: <length> | <percentage> | auto;
/* Shorthand */
inset: <top> <right> <bottom> <left>;π 1-Value Syntax (Applies to All Sides)
One-value inset
.box {
position: absolute;
inset: 20px;
}Equivalent to:top: 20px; right: 20px; bottom: 20px; left: 20px;
π 2-Value Syntax (Vertical | Horizontal)
Two-value inset
.box {
position: absolute;
inset: 10px 30px;
}Equivalent to:
- top & bottom = 10px
- left & right = 30px
π 3-Value Syntax
Three-value inset
.box {
position: absolute;
inset: 10px 20px 40px;
}Equivalent to:
- top = 10px
- left & right = 20px
- bottom = 40px
π 4-Value Syntax (top | right | bottom | left)
Four-value inset
.box {
position: absolute;
inset: 5px 15px 25px 35px;
}Equivalent to setting each side individually.
π 5. Using auto in Inset
Inset auto
.center-box {
position: absolute;
inset: 0 auto;
}auto helps center elements when combined with width/height.
π 6. Center an Element With inset
Center using inset
.center {
position: absolute;
inset: 0;
margin: auto;
width: 200px;
height: 100px;
}This is one of the cleanest ways to center a fixed-size element.
π 7. Fullscreen Element Using inset: 0
Fullscreen
.fullscreen {
position: fixed;
inset: 0;
}Same as:top: 0; right: 0; bottom: 0; left: 0;
π 8. Using Percentages
Percentage offsets
.panel {
position: absolute;
inset: 10% 5%;
}Useful for responsive positioned elements.
π 9. Comparison: Inset vs Logical Inset
| Property | Controls |
|---|---|
| inset | top, right, bottom, left |
| inset-inline | left/right (LTR) or right/left (RTL) |
| inset-block | top/bottom |
Note
π§ͺ Real-World Use Cases
1οΈβ£ Tooltip Positioned Easily
Tooltip example
.tooltip {
position: absolute;
inset: auto 0 100% 0;
}2οΈβ£ Modal Centering
Centered modal
.modal {
position: fixed;
inset: 0;
margin: auto;
width: 400px;
height: 300px;
}3οΈβ£ Sticky Header With Custom Bottom Offset
Sticky header
header {
position: sticky;
inset-block-start: 0; /* same as top: 0 */
}β οΈ Common Mistakes
- β Using inset on non-positioned elements
- β Mixing inset with top/right/bottom/left β overrides occur
- β Forgetting that auto behaves differently depending on width/height constraints
Note
π₯ Summary
The inset property simplifies positioned element layout by replacing four propertiesβtop, right,bottom, leftβwith one shorthand. It is clean, readable, and great for responsive + centered layouts.
Learn more βMDN Docs π