CSS inset Property
The CSS inset property is a shorthand for setting the top, right, bottom, and left positioning offsets of an element.
It accepts one to four values, following the same assignment pattern as the margin and padding shorthands. The property is useful for positioning overlays, badges, fixed elements, and other content without writing four separate offset declarations.
inset Syntax
The inset property accepts one to four space-separated offset values. Each value may be a length, percentage, auto, or a supported positioning expression.
Basic Syntax
selector {
inset: 20px;
}
Two Values
.box {
inset: 10px 20px;
}
Three Values
.box {
inset: 10px 20px 30px;
}
Four Values
.box {
inset: 10px 20px 30px 40px;
}
Percentage Values
.box {
inset: 5% 10%;
}
Mixed Values
.box {
inset: 20px auto 10px 5%;
}
CSS-Wide Keywords
.box {
inset: initial;
}
.box {
inset: inherit;
}
.box {
inset: unset;
}
.box {
inset: revert;
}
.box {
inset: revert-layer;
}
The shorthand's initial longhand values are all auto. The inset property sets physical offsets rather than logical writing-mode-relative offsets.
Property Values
The inset shorthand sets four physical positioning properties: top, right, bottom, and left.
| Value | Description |
|---|---|
auto |
Allows the applicable positioning rules to determine the offset. |
<length> |
Sets an offset using units such as px, rem, em, or viewport units. |
<percentage> |
Sets an offset as a percentage of the relevant containing block dimension. |
calc(...) |
Calculates an offset using compatible CSS values. |
min(...) |
Uses the smallest result from the supplied offset expressions. |
max(...) |
Uses the largest result from the supplied offset expressions. |
clamp(...) |
Constrains an offset between minimum and maximum values. |
anchor(...) |
Uses an anchor-positioning expression where supported and permitted. |
initial |
Resets all four longhand offsets to auto. |
inherit |
Inherits the computed values of all four offsets from the parent. |
unset |
Resets all four offsets to auto because the longhands are not inherited. |
revert |
Reverts the four offsets according to cascade origin rules. |
revert-layer |
Reverts the four offsets according to cascade layer rules. |
Shorthand Assignment
| Number of Values | Assignment |
|---|---|
| One | All four sides receive the same value. |
| Two | First value sets top and bottom; second sets right and left. |
| Three | First sets top; second sets right and left; third sets bottom. |
| Four | Values set top, right, bottom, and left in clockwise order. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto for each longhand |
| Inherited | No |
| Applies to | Positioned elements |
| Computed value | Determined separately for each longhand |
| Animation type | Determined separately for each longhand |
| Percentages | Relative to the relevant containing block dimension |
| Longhand properties | top, right, bottom, left |
The shorthand itself does not have a single independent computed value or animation type. Its behavior is determined by the four longhand properties.
How It Works
The inset property provides a compact way to assign positioning offsets to all four physical sides of an element.
Equivalent Longhand Declarations
.box {
inset: 10px 20px 30px 40px;
}
This is equivalent to:
.box {
top: 10px;
right: 20px;
bottom: 30px;
left: 40px;
}
Positioning Requirement
.box {
position: absolute;
inset: 20px;
}
The position property determines how the offsets are interpreted. With position: absolute, they position the element relative to its containing block.
Important Consideration
The inset property does not establish a positioning mode by itself. For a statically positioned element, the offsets do not affect its position.
One Value
When one value is supplied, it is applied to all four sides.
Example
.box {
position: absolute;
inset: 20px;
}
Equivalent Longhands
.box {
top: 20px;
right: 20px;
bottom: 20px;
left: 20px;
}
Fill a Container
.container {
position: relative;
}
.overlay {
position: absolute;
inset: 0;
}
With automatic sizing and no conflicting constraints, the overlay can stretch to the edges of its containing block.
Important Consideration
Setting all four offsets does not always force an element to fill the available area. Explicit dimensions, margins, intrinsic sizing, and positioning rules can affect the result.
Two Values
When two values are supplied, the first sets the top and bottom offsets, while the second sets the right and left offsets.
Example
.box {
position: absolute;
inset: 10px 20px;
}
Equivalent Longhands
.box {
top: 10px;
right: 20px;
bottom: 10px;
left: 20px;
}
Overlay with Different Vertical and Horizontal Offsets
.overlay {
position: absolute;
inset: 15px 30px;
}
This places the overlay 15 pixels from the top and bottom and 30 pixels from the left and right of its containing block, subject to the applicable positioning and sizing rules.
Three Values
When three values are supplied, the first sets the top offset, the second sets both horizontal offsets, and the third sets the bottom offset.
Example
.box {
position: absolute;
inset: 10px 20px 30px;
}
Equivalent Longhands
.box {
top: 10px;
right: 20px;
bottom: 30px;
left: 20px;
}
Different Top and Bottom Offsets
.panel {
position: absolute;
inset: 40px 15px 20px;
}
The panel uses 40 pixels at the top, 15 pixels at both horizontal sides, and 20 pixels at the bottom.
Four Values
When four values are supplied, they are assigned clockwise in the order top, right, bottom, and left.
Example
.box {
position: absolute;
inset: 10px 20px 30px 40px;
}
Equivalent Longhands
.box {
top: 10px;
right: 20px;
bottom: 30px;
left: 40px;
}
Mixed Units
.box {
position: absolute;
inset: 1rem 5% 20px 2em;
}
Important Consideration
The order is physical and does not change with the writing direction. The first value always represents the top offset.
Positioning Modes
The effect of inset depends on the element's position value.
| Position Value | How inset Behaves |
|---|---|
static |
Offsets have no positioning effect. |
relative |
Offsets move the element visually from its normal position while its original layout space remains reserved. |
absolute |
Offsets position the element relative to its absolute-positioning containing block. |
fixed |
Offsets position the element relative to its fixed-positioning containing block, usually the viewport. |
sticky |
Offsets define thresholds used to constrain the element's position while scrolling. |
Important Consideration
For absolute and fixed positioning, the element is removed from normal document flow. Relative and sticky positioning preserve the element's normal layout space.
Absolute Positioning
Absolutely positioned elements use offsets to determine their position relative to an established containing block.
Position Inside a Container
.container {
position: relative;
}
.badge {
position: absolute;
inset: 10px 10px auto auto;
}
The badge is positioned 10 pixels from the top and right edges of its containing block.
Stretch an Overlay
.container {
position: relative;
}
.overlay {
position: absolute;
inset: 0;
background-color: rgb(0 0 0 / 40%);
}
Inset Panel
.panel {
position: absolute;
inset: 20px;
padding: 16px;
box-sizing: border-box;
}
Important Consideration
A positioned ancestor commonly establishes the containing block for an absolutely positioned descendant. Other properties, including certain transforms, can also establish containing blocks.
Fixed Positioning
Fixed positioning allows an element to remain at a specified location relative to its fixed-positioning containing block, which is usually the viewport.
Fixed Notification
.notification {
position: fixed;
inset: auto 20px 20px auto;
}
This places the notification near the bottom-right corner of the viewport in a typical fixed-positioning context.
Full-Screen Overlay
.modal-backdrop {
position: fixed;
inset: 0;
background-color: rgb(0 0 0 / 60%);
}
Fixed Header
.fixed-header {
position: fixed;
inset: 0 0 auto;
block-size: 70px;
}
Important Consideration
Certain ancestor properties can establish a containing block for fixed descendants, so a fixed element is not always positioned relative to the viewport.
Relative Positioning
Relatively positioned elements remain in normal document flow but can be visually shifted using positioning offsets.
Move an Element
.box {
position: relative;
inset: 10px auto auto 20px;
}
The element is shifted downward by 10 pixels and rightward by 20 pixels from its normal position.
Opposing Offsets
.box {
position: relative;
inset: 10px 20px 30px 40px;
}
When opposing offsets are specified for a relatively positioned element, they cannot all independently determine the visual shift. CSS positioning rules resolve the conflicting values.
Important Consideration
Relative positioning does not remove the element's original layout space. Nearby elements are laid out as though the visual offset had not moved it.
Sticky Positioning
Sticky positioning combines normal-flow placement with offset thresholds that constrain an element during scrolling.
Sticky Header
.sticky-header {
position: sticky;
inset: 0 auto auto;
}
The top offset establishes a zero-pixel sticky threshold in the relevant scrolling context.
Sticky Sidebar
.sticky-sidebar {
position: sticky;
inset: 20px auto auto;
}
Important Consideration
Sticky positioning depends on the scroll container, available space, and containing block. A sticky element also needs a non-auto inset on the relevant axis for sticky positioning to take effect on that axis.
Percentage Values
Percentage offsets are calculated relative to the relevant dimensions of the containing block.
Percentage Offsets
.box {
position: absolute;
inset: 10% 5%;
}
The top and bottom offsets use the containing block's height, while the left and right offsets use its width in the usual horizontal positioning context.
Center an Element with a Transform
.centered-box {
position: absolute;
inset: 50% auto auto 50%;
transform: translate(-50%, -50%);
}
The percentage offsets place the element's top-left positioning reference at the center of its containing block. The transform shifts the element by half its own dimensions.
Important Consideration
Percentage offsets and percentage translations use different reference dimensions. Positioning percentages refer to the containing block, while percentage translations generally refer to the transformed element's own reference box.
Negative Values
Negative length and percentage offsets are allowed and can position an element beyond the corresponding edge of its containing block.
Negative Top Offset
.badge {
position: absolute;
inset: -10px auto auto 20px;
}
Negative Right Offset
.badge {
position: absolute;
inset: 10px -15px auto auto;
}
Decorative Element
.decoration {
position: absolute;
inset: -20px -20px auto auto;
}
Important Consideration
Negative offsets can cause content to extend outside its container or viewport. Check overflow and visibility when using them.
inset vs. margin
The inset and margin properties both accept one to four values, but they serve different purposes.
Positioning Offsets
.box {
position: absolute;
inset: 20px;
}
The offsets establish positioning constraints relative to the containing block.
Outer Spacing
.box {
margin: 20px;
}
Margins create space around the element's border box and participate in the applicable layout rules.
Comparison
| Property | Purpose |
|---|---|
inset |
Sets the physical positioning offsets of an element. |
margin |
Sets outer spacing around an element's border box. |
padding |
Sets inner spacing between an element's content and border. |
Important Consideration
Margins can also affect the placement of positioned elements. The inset shorthand does not replace margin or padding.
Logical Inset Properties
The inset shorthand uses physical sides. CSS also provides logical inset properties that follow the element's writing mode and direction.
Logical Block Offsets
.box {
position: absolute;
inset-block: 10px 20px;
}
Logical Inline Offsets
.box {
position: absolute;
inset-inline: 10px 20px;
}
Individual Logical Offsets
.box {
position: absolute;
inset-block-start: 10px;
inset-block-end: 20px;
inset-inline-start: 30px;
inset-inline-end: 40px;
}
Comparison
| Property | Purpose |
|---|---|
inset |
Sets top, right, bottom, and left offsets. |
inset-block |
Sets logical block-start and block-end offsets. |
inset-inline |
Sets logical inline-start and inline-end offsets. |
Important Consideration
Use logical inset properties when positioning must adapt to different writing modes or text directions. Avoid conflicting physical and logical declarations unless their cascade behavior is intentional.
inset and Element Sizing
For absolutely or fixed-positioned elements, opposing offsets can influence the used dimensions when the corresponding size is automatic.
Stretch an Element
.overlay {
position: absolute;
inset: 0;
}
With automatic width and height and no conflicting constraints, the overlay can stretch across its containing block.
Explicit Width and Height
.box {
position: absolute;
inset: 10px;
width: 200px;
height: 100px;
}
The element has explicit dimensions. When its offsets and dimensions over-constrain the layout, CSS positioning rules determine which constraints take precedence.
Use box-sizing
.panel {
position: absolute;
inset: 20px;
padding: 16px;
box-sizing: border-box;
}
Important Consideration
The final size of a positioned element depends on its offsets, dimensions, margins, padding, borders, intrinsic size, and positioning context.
Inheritance
The inset shorthand is not inherited. Each of its four longhand properties has an initial value of auto.
Parent Offsets
.parent {
position: relative;
inset: 10px;
}
A child does not automatically inherit these offset values.
Explicit Inheritance
.child {
position: relative;
inset: inherit;
}
Reset to Initial Values
.child {
inset: initial;
}
All four longhand offsets are reset to auto.
Reset with unset
.child {
inset: unset;
}
Because the longhands are not inherited, unset resets them to their initial values.
inset Example
This example demonstrates how the inset shorthand positions an absolutely positioned element inside a relatively positioned container.
HTML Example
<div class="inset-demo">
<div class="demo-container">
<div class="demo-overlay">
inset: 20px
</div>
</div>
</div>
CSS Example
.inset-demo .demo-container {
position: relative;
height: 220px;
border: 2px solid #2563eb;
background-color: #eff6ff;
}
.inset-demo .demo-overlay {
position: absolute;
inset: 20px;
display: flex;
align-items: center;
justify-content: center;
border: 2px dashed #1d4ed8;
background-color: #bfdbfe;
color: #1e3a8a;
font-weight: bold;
}
The inner element is positioned 20 pixels from each edge of the containing block. Because its width and height are automatic, the opposing offsets allow it to stretch across the remaining space.
Try Two Values
.inset-demo .demo-overlay {
inset: 10px 30px;
}
This changes the top and bottom offsets to 10 pixels and the left and right offsets to 30 pixels.
Try Four Values
.inset-demo .demo-overlay {
inset: 10px 20px 30px 40px;
}
This gives each side an independent offset.
Common Mistakes
Forgetting to Set position
.box {
inset: 20px;
}
The offsets do not affect the position of a statically positioned element. Use an appropriate position value.
Using Commas Between Values
.box {
inset: 10px, 20px;
}
This is invalid. Values must be separated by spaces.
Using a Unitless Length
.box {
inset: 20;
}
Nonzero CSS lengths require an appropriate unit.
Confusing inset with margin
The inset property sets positioning offsets, while margin sets outer spacing.
Assuming inset Always Stretches an Element
Opposing offsets can stretch an absolutely positioned element when its corresponding size is automatic, but explicit dimensions and other constraints can change the result.
Forgetting the Containing Block
An absolutely positioned element is positioned relative to its containing block, which is not always its immediate parent.
Assuming inset Is a Logical Property
The inset shorthand uses physical sides. Use inset-block and inset-inline for writing-mode-aware positioning.
Over-Constraining Relative Positioning
Specifying opposing offsets for a relatively positioned element does not create independent movement in both directions. CSS resolves the conflicting constraints.
Important Considerations
Keep the following points in mind when using inset:
- The property is shorthand for
top,right,bottom, andleft. - It accepts one to four space-separated values.
- One value applies to all four sides.
- Two values set vertical and horizontal offsets.
- Three values set top, horizontal sides, and bottom.
- Four values follow the order top, right, bottom, and left.
- The initial value of each longhand is
auto. - The property is not inherited.
- The
positionproperty determines how offsets are interpreted. - Offsets do not affect statically positioned elements.
- Absolute and fixed positioning remove elements from normal document flow.
- Relative positioning preserves the element's original layout space.
- Sticky positioning uses offsets as scrolling thresholds.
- Negative length and percentage offsets are allowed.
- Percentage offsets depend on the relevant containing block dimensions.
- Opposing offsets may influence the used dimensions of absolutely positioned elements.
- The shorthand uses physical sides rather than logical writing-mode-relative sides.
- Use logical inset properties when positioning must adapt to writing modes or text direction.
- Test positioned content for overlap, clipping, and viewport overflow.
Browser Support
The CSS inset shorthand property is widely supported in modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
Basic length, percentage, and auto offsets are broadly supported. Newer positioning features, such as anchor positioning, may have separate compatibility requirements.
For current compatibility information, visit Can I Use? and search for inset.
Specifications
The inset shorthand property is defined in the CSS Logical Properties and Values Module Level 1 specification.
CSS Logical Properties and Values Module Level 1: Inset Properties
The specification defines the physical inset shorthand and its relationship to logical inset properties.
For additional documentation and examples, consult MDN Web Docs.
