CSS Reference ยท Box Model and Sizing
The overflow-anchor property
Reference for the CSS overflow-anchor property: how browsers keep your scroll position stable when content above changes, and how to opt out. Values and notes.
Scroll anchoring is a browser feature that keeps what you are reading in place when content above it changes size, for example when an image finishes loading or an ad is inserted. Without it, the page would jump. The overflow-anchor property lets you opt an element out of this behavior.
.chat-log { overflow-anchor: none; }
Try it
Scroll the list down a little, then insert a message. With anchoring on (the default) the browser pushes the scroll position down so the message you were reading stays put. With the checkbox ticked the new item pushes your content down instead.
Syntax
overflow-anchor: auto | none
- Initial value:
auto - Applies to: all elements
- Inherited: no
- Animatable: no (discrete)
- Computed value: as specified
Values
| Value | Description |
|---|---|
auto |
The element and its descendants can be chosen as anchors, and the browser adjusts the scroll position to keep the anchor still. |
none |
The element and its subtree are never chosen as anchor nodes. Changes there may move the visible content. |
Usage notes
- Applies to the scroller's content. Setting
noneon a scrolling container (or an element inside it) stops that subtree from being used as an anchor. - It is on by default in browsers that implement it, so most pages need no code. Reach for
nonewhen your own script already manages scroll position (chat logs, virtual lists) and the two adjustments would fight each other. - A pinned-to-bottom chat. For a view that should stick to the newest message, anchoring and your script can conflict. Test both settings.
- Not applied during user scrolling or at the top. Browsers skip anchoring when the scroll offset is zero, so content added above the first screen does not move the page.
- Animations. Changes to
top,left,transformand similar positioning do not trigger anchoring adjustments, but changes to layout size do.
Browser support
Supported in Chromium-based browsers and Firefox; Safari did not support scroll anchoring at the time of writing, so treat it as a progressive enhancement.
Related
overflowoverflow-yscroll-behavior- Lesson: Overflow