How to Set Top, Right, Bottom, and Left at Once with CSS inset
This article was translated from the Japanese original with AI. 日本語の原文を読む
With the CSS inset property, you can write the top, right, bottom, and left of a positioned element in one line. The values are ordered the same way as in margin, so inset: 0 sets all four sides to 0. It is used, for example, for the semi-transparent backdrop behind a modal that covers the whole screen, or for a badge in the top-right corner of a card.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I placed a position: absolute child inside a 400×300px parent with position: relative, read the four side values with getComputedStyle(), and read the child's outer box with getBoundingClientRect(). I did not test Firefox or Safari.
- 1. Same as writing the four sides separately
- 2. Number of values and which side each one sets
- 3. Filling the parent or the whole screen
- 4. Mixing in auto to align to a corner or edge
- 5. When inset does not work
- 6. inset maps to physical top, right, bottom, and left even when the writing direction changes
- 7. Browser support
Same as writing the four sides separately
The following two rules produce the same result.
.child {
position: absolute;
top: 20px;
right: 40px;
bottom: 60px;
left: 80px;
}
.child {
position: absolute;
inset: 20px 40px 60px 80px; /* top right bottom left */
}
In the test environment, both gave top 20px, right 40px, bottom 60px, and left 80px. The child's outer box was also the same in both: left 80, top 20, width 280, height 220.
As MDN's explanation of inset states, inset is a shorthand that sets four separate properties at once. When I set element.style.inset = "20px 40px" in JavaScript, the values were split into the four individual properties: element.style.top was 20px and element.style.left was 40px. Conversely, reading style.cssText for an element whose four sides were set separately returned them combined as inset: 1px 2px 3px 4px;.
Number of values and which side each one sets
You can write one to four values. The CSS Positioned Layout specification defines the inset values as assigned to the four sides using the same rules as margin. The order is top, right, bottom, left, going clockwise from the top.
| Syntax | Top | Right | Bottom | Left |
|---|---|---|---|---|
inset: 20px |
20px | 20px | 20px | 20px |
inset: 20px 40px |
20px | 40px | 20px | 40px |
inset: 20px 40px 60px |
20px | 40px | 60px | 40px |
inset: 20px 40px 60px 80px |
20px | 40px | 60px | 80px |
The values in the table are the ones I read with getComputedStyle() in the test environment. A side whose value is omitted takes the value of the opposite side. With two values, the first sets top and bottom, and the second sets left and right. With three values, left takes the same value as right.

Separate the values with spaces. Writing commas, as in inset: 10px, 20px, or listing five or more values made CSS.supports() return false. inset: 10 without a unit also returned false, but inset: 0 works without a unit.
Filling the parent or the whole screen
inset: 0 matches all four sides of the parent (the containing block). When I gave inset: 0 to a child without a width or height, the child became 400×300px, the same as the parent.
To cover the whole screen, as with a modal backdrop, combine it with position: fixed.
.backdrop {
position: fixed;
inset: 0;
background: rgb(0 0 0 / 0.5);
}
Even after scrolling a 3000px-tall page by 1000px, this element covered exactly the visible area: from the top-left (0, 0) of the screen (800×600px), with a width of 800px and a height of 600px.
Mixing in auto to align to a corner or edge
A side set to auto does not fix a position. To place a badge in the top-right corner of a card, set only the top and right to 0.
.badge {
position: absolute;
inset: 0 0 auto auto; /* top 0, right 0, bottom and left auto */
width: 50px;
height: 50px;
}
In a 400×300px parent, this child was placed at left 350, top 0, which is the top-right corner. With inset: auto 0 0 auto, it was at left 350, top 250, which is the bottom-right corner.
If you do not give a size along an axis where a side is auto, that axis is sized to the content. For a child with a three-character label, inset: 0 auto made the height 300px, the full height of the parent, and the width 49.13px, sized to the content. With inset: 0 0 auto, the width was 400px, the full width of the parent, and the height was 21px, one line of text. The element was attached to the top edge of the parent like a strip.
Percentages are relative to the parent's height for top and bottom, and to its width for left and right (the specification's explanation of top, right, bottom, and left). With a 400×300px parent, inset: 10% gave 30px for top and bottom and 40px for left and right.
When inset does not work
position is not set
As MDN's explanation of inset states, inset has no effect on an element that is not positioned. I placed a 50×50px child just below a 40px-high box, gave it inset: 20px, and compared different position values.
position |
Child's outer box position (left, top) |
|---|---|
static (initial value) |
(0, 40) |
relative |
(20, 60) |
absolute |
(20, 20) |
With static, getComputedStyle() reported top as 20px, but the child did not move from its original position (0, 40). With relative, it moved 20px to the right and 20px down from its original position. With absolute, it moved to 20px from the top-left of the parent.
With relative, setting all four sides does not stretch the element
position: relative only shifts an element from its normal position without changing its size (the specification's explanation of relative). I compared a child with a three-character label, no explicit width or height, and inset: 20px.
position |
Child's outer box (left, top, width, height) |
|---|---|
relative |
(20, 20, 400, 21) |
absolute |
(20, 20, 360, 260) |
The relative child kept the width of 400px and height of 21px it would have as a normal block. It only shifted 20px to the right and 20px down. The right and bottom values had no effect. On a page written left to right, the top and left values are used, and the bottom and right values are ignored. To stretch an element to fill the inside of its parent, use absolute or fixed.
Overwritten by a longhand property written later
inset and the individual properties both set the same four values, so the one written later wins.
.a { inset: 20px; top: 0; } /* only top becomes 0 */
.b { top: 0; inset: 20px; } /* top: 0 is overwritten, so top is also 20px */
With the .a rule, top was 0px, and right, bottom, and left were 20px. With the .b rule, all four sides were 20px. If you want to set all four sides with inset and then change only one side, write the individual property after inset.
inset maps to physical top, right, bottom, and left even when the writing direction changes
inset maps to the physical top, right, bottom, and left even when the writing direction changes. Even when the parent has direction: rtl (right to left) or writing-mode: vertical-rl (vertical text), a child with inset: 20px 40px 60px 80px was at left 80, top 20, the same as in horizontal left-to-right writing.
To swap left and right to match the writing direction, use inset-inline (the inline direction) and inset-block (the block direction). With direction: rtl on the parent and inset-inline: 80px 40px on the child, the first 80px went to the right and the second 40px went to the left. The specification defines the first value as the start side of the line and the second as the end side.
Browser support
According to MDN's browser-compat-data, inset is available from Chrome and Edge 87, Firefox 66, and Safari 14.1. If you need to support older browsers, write the four sides separately.





