inset
A shorthand that sets top, right, bottom, and left in a single declaration.
- Initial value
auto- Inherited
- No
- Animatable
- yes, as a length or percentage
- Applies to
- positioned elements (position other than static)
- JavaScript
element.style.inset = "0"
Example
#.cover {
position: relative;
}
.cover::after {
content: "";
position: absolute;
inset: 0;
background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
}<div class="cover" style="height: 150px; border-radius: 12px; overflow: hidden; background: url('/collections/covers/jak-zostac-programista.webp') center / cover;"></div>Definition and usage
#The inset property is a shorthand for the four offset properties: top, right, bottom, and left. It works like margin or padding: one value sets every side, two values set the vertical and horizontal sides separately, and four values set the top, right, bottom, and left in turn.
The most common use is inset: 0 on an element with position: absolute. The element then stretches to fill its entire parent, which is handy for overlays, backgrounds, and image dimming.
Like the individual offsets, inset only works on elements whose position is something other than static.
Syntax
#inset: <top> <right>? <bottom>? <left>?;Values
#- The element stretches to cover the entire reference point.
- An equal 16px gap on every side.
- The first value is top and bottom, the second is left and right.
- Four values: the element sticks to the bottom-left corner.
Values in practice
#0
.demo { inset: 0; }16px
.demo { inset: 16px; }20px 40px
.demo { inset: 20px 40px; }auto auto 12px 12px
.demo { inset: auto auto 12px 12px; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#inset: 0together withmargin: autoand a known size is a simple way to center an absolutely positioned element.- For full-screen modal windows, combine
position: fixedwithinset: 0.
Related entries
#- positionChooses how an element is positioned: in the flow, relative, absolute, fixed, or sticky.
- topMoves a positioned element away from the top edge of its reference point.
- leftMoves a positioned element away from the left edge of its reference point.
- z-indexDetermines which element sits on top when elements overlap.
Spotted a mistake or missing an example? Write to us.