Educational guide to position and inset
Find the reference that moves each box
Track the space a box keeps in flow, identify the edge each inset is measured from, and see what changes as you scroll.
Mental model
Before setting an inset, identify the positioning mode, its reference, and the axis you want to control.
- Space in flow
Do the neighboring elements still account for the space occupied by the box?
- Reference
Does the box use its original position, a containing block, or a scroll region?
- Edges
Does the offset follow a physical edge or the start or end of a logical axis?
Flow and the containing block
The first four examples keep top: 12px. The last two compare right: 12px. The dashed border marks the inner frame.
For absolute, look for the nearest ancestor that establishes the containing block. A block ancestor with position: relative establishes it at its padding edge. When the inner frame becomes static in this scene, the outer frame takes over. Other properties, such as transform, can also establish it.
In Flow and reference, change the direction or writing mode to move the origin of the flow. The inset stays tied to the physical edge you selected. Try logical properties in the Insets scene.
top- With relative, a positive value moves the box down from its original position. With absolute, it leaves that distance from the top edge of the containing block.
right- With relative, a positive value moves the box left. With absolute, it leaves that distance from the right edge of the containing block.
bottom- With relative, a positive value moves the box up. With absolute, it leaves that distance from the bottom edge of the containing block.
left- With relative, a positive value moves the box right. With absolute, it leaves that distance from the left edge of the containing block.
A negative value reverses the relative offset. With absolute, it lets the box extend past the chosen edge. Insets leave a static box in its normal position.
With ltr, the inline axis starts at the top in vertical-lr and at the bottom in sideways-lr. Browsers without support for the sideways values keep the horizontal writing mode.
static
relative
absolute with the inner frame
absolute with the outer frame
relative with right
absolute with right
What sets the reference for fixed
Scroll each viewer. The ancestor uses position: relative in both. The only declaration that changes is transform.
In this screen example, fixed takes the box out of flow. With transform: none, it measures insets from the viewer document’s viewport. The identity transform translate(0) establishes a containing block for fixed even though the ancestor stays in the same visual position.
fixed with transform: none
fixed with translate(0)
When sticky sticks
Focus or scroll the region in each viewer. The same dashed section contains the header and limits its travel in both.
The header keeps its space in flow. Once its top edge reaches the 12 px threshold in the scroll region it uses as a reference, it can stay there while it fits within the section. When it reaches the lower boundary, scrolling farther carries the header away with the section.
sticky with overflow: clip
With both insets on the axis set to auto, the box follows its normal flow position on that axis. Try that preset in the Scrolling scene.
The header uses position: sticky. This card shows support for overflow: clip, the declaration that lets the outer scroll region remain the reference in the second viewer.
Physical and logical edges
Compare the first two boxes with horizontal-tb and rtl. Then rotate the axes with vertical-rl.
inset groups top, right, bottom, and left in that physical order. With two values, the first repeats at the bottom and the second at the left. With three, the second repeats at the left.
inset: 12px- All four edges get 12px.
inset: 12px 24px- Top and bottom get 12px. Right and left get 24px.
inset: 12px 24px 10%- Top gets 12px, bottom gets 10%, and both sides get 24px.
inset: 12px 24px 10% auto- Top gets 12px, right 24px, bottom 10%, and left auto.
inset-block and inset-inline assign one or two values to the start and end of each axis. A single value applies to both ends. In horizontal-tb, the block axis starts at the top and direction changes the inline start from left to right. In vertical-rl, the block axis starts at the right and the inline axis starts at the top with ltr and at the bottom with rtl.
Physical inset with rtl
Logical start with rtl
Logical axes in vertical-rl
Percentages on opposite edges
For the absolute box in the example, top and bottom percentages use the height of the containing block. Left and right use its width. With two opposite edges set and an automatic width, the box stretches between them. Set one edge back to auto and the content determines how much width the box takes.
Common pitfalls
Start with the result you expected and check which rule or ancestor determines it.
The inset does not move the box
- Expected result
- Move the box 12 px from its original position and keep its space.
- Cause
- With position: static, the insets have no effect.
- Solution
- Use position: relative and inset-block-start: 12px. Check that the sibling stays in place.
The fixed box scrolls with the page
- Expected result
- Keep the same distance from the viewport.
- Cause
- An ancestor with a transform value other than none establishes the containing block for fixed. Even translate(0) does so.
- Solution
- Inspect the ancestors. If the box should follow the viewport, remove the transform that contains it or move the box outside that ancestor.
The sticky header does not stick
- Expected result
- Keep the header 12 px from the start after it reaches the threshold.
- Cause
- Both insets on the axis may be auto, an ancestor with overflow: hidden may change the scrollport, or the section may end before the header has room to travel.
- Solution
- Set inset-block-start: 12px, check which region scrolls, and leave room before the section ends. If you only need to clip the ancestor, try overflow: clip and check whether you relied on its formatting context.
The edge is opposite the reading start
- Expected result
- Follow the inline start when switching from LTR to RTL.
- Cause
- The left property and the physical inset shorthand still measure from the left edge.
- Solution
- Use inset-inline-start to follow the inline axis and check direction and writing-mode.