Sub topic 8
Positioning with inset-inline and inset-block
This chapter assumes you know position: relative, absolute, fixed and sticky, and that you know the block and inline axes from the earlier chapters. Here you will replace top, right, bottom and left with flow-relative offsets.
The inset properties
The physical offsets top, right, bottom and left move a positioned element away from the edges of its containing block. The logical versions do the same job, but they name edges by axis and direction.
| Physical | Logical (any mode) |
|---|---|
| `top` | `inset-block-start` |
| `bottom` | `inset-block-end` |
| `left` | `inset-inline-start` (in left-to-right text) |
| `right` | `inset-inline-end` (in left-to-right text) |
The inline rows depend on direction. In right-to-left text, inset-inline-start is the right edge. In a vertical writing mode, the block axis runs horizontally, so inset-block-start is a left or right edge.
A badge that follows the text direction
The badge below 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.
.badge {
position: absolute;
inset-block-start: 0;
inset-inline-end: 0;
}The containing block needs a positioning context, such as position: relative on its parent. Without one, the badge is placed against the initial containing block.
Shorthands
Two shorthands set both edges on one axis.
inset-inlinesets the inline-start and inline-end offsets.inset-blocksets the block-start and block-end offsets.
With one value, both edges get it. With two values, the first is the start side and the second is the end side.
.overlay {
position: absolute;
inset-block: 0;
inset-inline: 1rem 2rem;
}This overlay touches both block edges of its containing block. It leaves 1rem on the inline-start side and 2rem on the inline-end side.
The inset shorthand also exists. It takes up to four values in the same order as margin, which is top, right, bottom, left. It is physical. inset: 0 is a safe way to stretch an element across its containing block, because zero is the same in every direction.
Common errors
- Mixing both kinds on one axis. If
leftandinset-inline-startare both set, the later declaration in the cascade wins. Pick one family per element. - Reading two values as left and right. In
inset-inline: 1rem 2rem, the values are start and end. In right-to-left text, 1rem is on the right. - Forgetting the positioning context. Offsets have no effect on
position: static. - Assuming sticky offsets are physical.
inset-block-start: 0on aposition: stickyheader works the same way, and it keeps the header at the block-start edge of its scroll container.
Exercise
A tooltip uses position: absolute; inset-block-start: 100%; inset-inline-start: 0;. In an Arabic page with dir="rtl" and the default horizontal writing mode, which edge of the parent does the tooltip align with, and where does it sit vertically?
Show answer
It aligns with the parent's right edge, because inline-start is the right side in right-to-left text. It sits just below the parent, because block-start is still the top in a horizontal writing mode and 100% pushes it past the bottom edge.