Sub topic 8
Positioning with inset-inline and inset-block
This chapter assumes you know how position values work (relative, absolute, fixed, sticky) and what a containing block is. It also assumes you know the block and inline axes from the earlier chapters. Here you will replace top, right, bottom and left with their flow-relative counterparts and learn where the shorthands stop being logical.
What the offset properties do
Positioned elements use offset properties to say how far an edge sits from the matching edge of a reference box. For absolute and fixed elements, the reference box is the containing block. For relative elements, it is the element's normal position. For sticky elements, it is the scrollport, as the sticky threshold.
The physical offsets are top, right, bottom and left. The logical offsets name the same four edges by axis and side.
| Logical property | Meaning |
|---|---|
| `inset-block-start` | Offset from the block-start edge |
| `inset-block-end` | Offset from the block-end edge |
| `inset-inline-start` | Offset from the inline-start edge |
| `inset-inline-end` | Offset from the inline-end edge |
Like their physical versions, these properties have no effect on an element with position: static.
How the four properties map to physical sides
The element's writing-mode and direction decide which physical side each logical property controls. The table shows the four common cases.
| Writing mode and direction | block-start | block-end | inline-start | inline-end |
|---|---|---|---|---|
| `horizontal-tb`, `ltr` | top | bottom | left | right |
| `horizontal-tb`, `rtl` | top | bottom | right | left |
| `vertical-rl`, `ltr` | right | left | top | bottom |
| `vertical-lr`, `ltr` | left | right | top | bottom |
Setting direction: rtl in a vertical mode flips the inline axis, so inline-start becomes the bottom. The block axis in a vertical mode is horizontal, which is why vertical-rl puts block-start on the right.
A badge that follows the text direction
A common pattern is a small badge pinned to a corner of a card. With physical offsets you would write top: 0; right: 0, and the badge would stay on the right in Arabic or Hebrew pages, where the reader's attention starts on the right.
.badge {
position: absolute;
inset-block-start: 0;
inset-inline-end: 0;
}The badge sits in the block-start, inline-end corner of its containing block. That is the top right in English and the top left in Arabic. The parent must be positioned, for example with position: relative, or the badge will attach to a more distant ancestor or to the initial containing block.
The two-value shorthands
Two shorthands set both sides of one axis:
inset-blocksetsinset-block-startandinset-block-end.inset-inlinesetsinset-inline-startandinset-inline-end.
With one value, both sides get it. With two values, the first is the start side and the second is the end side.
.toolbar {
position: fixed;
inset-block: auto 0; /* block-start: auto, block-end: 0 */
inset-inline: 1rem; /* both inline sides: 1rem */
}This pins the toolbar to the bottom of the viewport in a horizontal writing mode, with 1rem gaps on the inline sides. The auto on the block-start side leaves that edge unconstrained, so the toolbar's height comes from its content.
Reading order matters. inset-inline: 0 auto means start is 0 and end is auto. In a left-to-right page that is left: 0; right: auto. In a right-to-left page it is right: 0; left: auto. This is the same start-then-end order you saw for padding-inline and margin-inline.
Stretching and centering with insets
When both offsets of an axis are set and the element's size on that axis is auto, the browser derives the size from the available space. This works the same way as with left and right, so the usual tricks carry over.
.overlay {
position: absolute;
inset-block: 0;
inset-inline: 0;
background: rgb(0 0 0 / 0.5);
}
.dialog {
position: absolute;
inset-block: 0;
inset-inline: 0;
inline-size: min(90%, 32rem);
block-size: fit-content;
margin: auto;
}The overlay stretches over the whole containing block. The dialog has a definite inline size and a content-based block size, and its insets are all 0. Together with margin: auto, that centers it on both axes, in any writing mode. Nothing in this rule refers to left or top.
The inset shorthand is physical
A callout is worth adding here because this catches many people.
Using inset: 0 is still fine when all four sides get the same value, because the result is symmetrical in every writing mode. The trouble starts when the values differ by side.
Percentages and `auto`
Percentage offsets resolve against the containing block's size in the axis they control. A percentage on inset-inline-start refers to the containing block's inline size in a horizontal mode. In a vertical mode, the same property maps to top and resolves against the containing block's height. In both cases, the percentage follows the physical side the property maps to.
The value auto works as it does for physical offsets. If an offset is auto, the element's position on that axis is computed from the other offset, from the element's size, or from its static position.
Sticky positioning
Sticky positioning uses the same properties as the threshold. A sticky table header can use a logical offset:
thead th {
position: sticky;
inset-block-start: 0;
background: canvas;
}The header sticks to the block-start edge of the scrollport. In a vertical writing mode, where the table scrolls horizontally, the same rule sticks to the correct edge without changes. A sticky first column would use inset-inline-start: 0, which keeps it at the start side for both left-to-right and right-to-left readers.
Writing mode consistency
The offsets map to physical sides through the writing mode and direction of the element. If you place an absolutely positioned child inside a container with a different writing mode or direction, check the result in a browser instead of assuming it. Keeping the child and its containing block in the same writing mode and direction avoids surprises. Set dir and lang on the document or section, not on individual widgets, so every positioned descendant inherits the same context.
Migrating physical offsets
Most conversions are mechanical:
| Physical | Logical |
|---|---|
| `top` | `inset-block-start` |
| `bottom` | `inset-block-end` |
| `left` | `inset-inline-start` |
| `right` | `inset-inline-end` |
| `top` and `bottom` together | `inset-block` |
| `left` and `right` together | `inset-inline` |
Before converting left or right, decide whether the placement belongs to the text flow or to the screen. A close button in a dialog belongs to the flow and should convert to inset-inline-end. A scrubber on a video timeline that must always move from left to right on screen is physical, and later chapters cover when to keep those values.
Common errors
- Forgetting
position. The offsets do nothing on a static element. - Using
insetwith different values. It is physical, soinset: 0 0 auto autopins to the top and left in every language. - Swapping start and end in a two-value shorthand. The order is start, then end.
- Mixing physical and logical offsets on one side. If
leftandinset-inline-startboth apply, the later declaration in the cascade wins. They are different properties but they set the same underlying value, so the order in your stylesheet decides the result. - Testing in one direction only. The badge bug is invisible until you load the page with
dir="rtl".
Exercise
A card has position: relative. Inside it, a label has position: absolute; inset-block-start: 0.5rem; inset-inline-end: 0.5rem;. On which physical corner does the label appear in a page with dir="rtl" and the default horizontal writing mode? Then say what inset: 0 1rem does to the same label if you add it.
Show answer
In a horizontal right-to-left page, block-start is the top and inline-end is the left, so the label appears in the top left corner, 0.5rem from each edge. The inset: 0 1rem shorthand is physical. It sets top and bottom to 0 and left and right to 1rem, whatever the direction. Because it has no logical meaning, it would also override the label's logical offsets if it comes later in the cascade.