Sticky positioning keeps an element in normal layout flow while allowing its visible position to shift against a scrollport threshold. Its movement is also limited by the containing block. Understanding those two boundaries explains why a heading can remain near the top for a while and later move away as its own section ends.
The CSS Positioned Layout specification defines sticky positioning relative to the nearest relevant scrollport. A non-auto inset such as top twenty pixels establishes an edge of the sticky view rectangle. The element's visible box is shifted inward when needed, within its containing-block constraint.
The threshold is not simply an instruction to move the original document position down by twenty pixels. While the normal position remains comfortably below that threshold, no sticky correction is needed. Only after scrolling would move it past the threshold does the correction become active.
The following calculation is a deliberately simplified one-axis illustration. It uses no margins, transforms or complicated layout relationships. It is an independently computed geometric model, not a claim that an actual browser screenshot or a production page was tested.

Let the scrollport height be two hundred pixels. The element's normal document top is one hundred fifty, its box height is forty and its top inset is twenty. Its containing block extends from document position one hundred to five hundred. Additional document content extends farther, permitting the later scroll positions used here.
At scroll offset zero, the normal top appears at one hundred fifty in scrollport coordinates. It is below the top threshold, so it remains there. At scroll offset one hundred twenty, the normal top appears at thirty and still needs no correction.
At scroll offset one hundred fifty, the normal top would be zero. Sticky positioning shifts the visible top to twenty, while the containing block still has ample room below. The normal-flow location remains part of the layout even though the visible position has been adjusted.
At scroll offset four hundred fifty, the containing block's bottom appears at fifty in scrollport coordinates. A forty-pixel box placed at top twenty would end at sixty, beyond that bottom. The box therefore cannot retain that placement while remaining within its containing block.
The simplified model places its top at ten, making its bottom fifty. It has moved above the twenty-pixel threshold because the containing block's end limits the allowed sticky correction. Relative to its uncorrected normal top of minus three hundred, the correction is still downward; the example does not contradict the rule that a top-only sticky inset pushes downward from normal position.
For these assumptions, the visible top is the smaller of the top-threshold-clamped normal position and the containing bottom minus the box height. This compact formula is a model for the stated simple geometry, not a complete replacement for the specification's margin, writing-mode and multiple-inset rules.

The MDN position reference explains that a sticky box responds to its relevant scrolling ancestor. An ancestor with a scrolling mechanism can become the reference even when a different outer surface is the one a reader notices moving.
A page investigation should therefore identify the actual scroll container before comparing the threshold with the browser window. Applying the rule to the wrong coordinate system can make a correct sticky relationship look like a failure. The element's containing block is another boundary and need not be the same object as the scrollport.
If both insets for an axis are auto, sticky positioning does not add an offset on that axis. Specifying a vertical threshold does not automatically create a horizontal threshold too. The example intentionally uses only top, leaving the other complexities outside its numeric model.
Sticky positioning preserves the element's normal layout participation. Other content is not automatically pulled into its original place as though the box had been removed from flow. Multiple sticky elements can also be offset independently and can overlap; the property does not itself arrange a stacked set of headings.
The examples compute normal coordinates, threshold correction and containing-block cap separately. They do not adjust image file dimensions, upload assets or claim that any site's layout has been repaired. Preserve all three positions when explaining why a sticky element moves or stops. Related material appears in the web layout methods section and the technical article archive.