CSS overflow-anchor Property
The CSS overflow-anchor property controls whether an element can participate in the browser's scroll anchoring behavior.
Scroll anchoring helps maintain a stable viewing position when content is inserted, resized, or loaded above the visible portion of a page or scroll container. The property allows developers to exclude elements or entire sections from this automatic adjustment when it interferes with an interface.
overflow-anchor Syntax
The overflow-anchor property accepts two keyword values: auto and none.
Basic Syntax
selector {
overflow-anchor: auto;
}
Keyword Values
.box {
overflow-anchor: auto;
}
.box {
overflow-anchor: none;
}
CSS-Wide Keywords
.box {
overflow-anchor: initial;
}
.box {
overflow-anchor: inherit;
}
.box {
overflow-anchor: unset;
}
.box {
overflow-anchor: revert;
}
.box {
overflow-anchor: revert-layer;
}
The initial value is auto, which allows an element to participate in scroll anchoring when the browser supports the feature.
Property Values
| Value | Description |
|---|---|
auto |
Allows the element to be considered as a scroll anchor candidate when eligible. This is the initial value. |
none |
Excludes the element and its descendants from scroll anchor selection. |
initial |
Resets the property to auto. |
inherit |
Uses the computed value of the parent element. |
unset |
Resets the property to auto because the property is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | All elements |
| Computed value | As specified |
| Animation type | Discrete |
| Affects layout dimensions | No |
| Controls scrolling directly | No |
Valid Values
.content {
overflow-anchor: auto;
}
.content {
overflow-anchor: none;
}
Invalid Values
.content {
overflow-anchor: hidden;
}
.content {
overflow-anchor: scroll;
}
.content {
overflow-anchor: 10px;
}
The property does not accept overflow keywords such as hidden or scroll, or numeric length values.
How It Works
When content changes above the current viewing position, the browser may adjust the scroll offset to keep the user looking at approximately the same content.
Without Scroll Anchoring
Imagine reading an article halfway down a page. If an image near the top loads late and increases the page height, the visible text may suddenly move downward.
With Scroll Anchoring
The browser selects an eligible anchor element near the visible portion of the scroll container. When content above that anchor changes size, the browser may adjust the scroll position to compensate.
Default Behavior
.article {
overflow-anchor: auto;
}
This permits eligible elements in the article to participate in the browser's scroll anchoring algorithm.
Excluding a Section
.dynamic-panel {
overflow-anchor: none;
}
The panel and its descendants cannot be selected as scroll anchors. Other eligible elements outside the excluded section may still be used.
Important Consideration
The property does not directly set or preserve a scroll position. It influences the browser's selection of scroll anchors, and the browser decides when adjustments are appropriate.
auto Value
The auto value permits an element to participate in scroll anchoring. It is the default setting.
Basic Example
.article-content {
overflow-anchor: auto;
}
Scrollable Panel
.reading-panel {
height: 300px;
overflow-y: auto;
overflow-anchor: auto;
}
Eligible elements inside the scrollable panel may be selected as anchors when its content changes.
When auto Is Useful
- Articles containing images that load after the text.
- Pages where content is inserted above the current viewing position.
- Scrollable reading panels with dynamically changing content.
- Layouts where preserving the user's reading position improves usability.
Important Consideration
auto allows anchor selection but does not guarantee that an element will be chosen. Eligibility, visibility, and other rules determine which element becomes the anchor.
none Value
The none value excludes an element and its descendants from scroll anchor selection.
Basic Example
.dynamic-content {
overflow-anchor: none;
}
Excluding a Loading Area
.loading-panel {
overflow-anchor: none;
}
This prevents elements within the loading panel from becoming scroll anchors while its content changes.
Disabling Scroll Anchoring for a Container
.scroll-panel {
height: 300px;
overflow-y: auto;
overflow-anchor: none;
}
When applied to the scroll container itself, none opts the container's contents out of anchor selection for that scrolling box.
When none Is Useful
- Interfaces that manage scroll positions with JavaScript.
- Dynamic sections that should not become scroll anchors.
- Components where automatic anchoring interferes with intended behavior.
- Scrollable interfaces that use their own positioning logic.
Important Consideration
Once a subtree is excluded with overflow-anchor: none, a descendant cannot opt back into scroll anchoring simply by declaring overflow-anchor: auto.
Scroll Anchor Selection
The browser selects an anchor node using rules defined by the scroll anchoring specification. Developers cannot directly choose a particular anchor with overflow-anchor.
Eligible Elements
Anchor candidates generally need to be visible or partially visible within the scroll container and meet the specification's eligibility requirements.
Anchor Priority
The selection algorithm considers priority candidates, including certain focused elements and targets of scrolling operations, before selecting other eligible nodes.
Excluding Candidates
.advertisement {
overflow-anchor: none;
}
The advertisement and its descendants are excluded from anchor selection.
Keeping Other Content Eligible
.article {
overflow-anchor: auto;
}
.article .advertisement {
overflow-anchor: none;
}
The article remains eligible for scroll anchoring while the advertisement subtree is excluded.
Important Consideration
Excluding an element from anchor selection does not prevent its size changes from affecting the document. The browser may still adjust the scroll position to preserve a different eligible anchor.
Nested Elements and Inheritance
The overflow-anchor property is not inherited, but excluding a subtree affects all of its descendants.
Parent with auto
.parent {
overflow-anchor: auto;
}
.child {
overflow-anchor: none;
}
The child subtree is excluded, while other eligible content within the parent may participate.
Parent with none
.parent {
overflow-anchor: none;
}
.child {
overflow-anchor: auto;
}
The child remains excluded because it belongs to a subtree that has already been opted out.
Explicit Inheritance
.child {
overflow-anchor: inherit;
}
The inherit keyword copies the computed value from the parent, but this is different from the normal subtree exclusion behavior.
Important Consideration
Apply none to the smallest appropriate section when only part of a page should be excluded from scroll anchoring.
Dynamic Content
Scroll anchoring is particularly useful when a page changes after its initial rendering.
Late-Loading Images
Images without reserved dimensions can cause surrounding content to shift when they load. Scroll anchoring may compensate for these changes, but reserving the correct image dimensions is a better way to prevent the layout shift.
<img src="example.jpg" alt="Example illustration" width="640" height="360">
Inserted Content
Adding new elements above the visible area can change the position of content already on screen. Scroll anchoring may adjust the scroll offset to keep the current content stable.
Dynamic Notifications
.notification-area {
overflow-anchor: none;
}
This prevents notifications from being selected as scroll anchors. It does not guarantee that inserting a notification will leave the scroll position unchanged.
JavaScript-Controlled Interfaces
.custom-scroll-panel {
height: 300px;
overflow-y: auto;
overflow-anchor: none;
}
Disabling anchoring may be appropriate when application code deliberately controls the panel's scroll position.
Important Consideration
Scroll anchoring is not a substitute for stable layout design. Reserve space for images, advertisements, and asynchronously loaded components whenever possible.
Scroll Containers
Scroll anchoring operates within scrolling boxes, including the document viewport and independently scrollable elements.
Scrollable Content Panel
.scroll-panel {
max-height: 300px;
overflow-y: auto;
}
The panel becomes scrollable when its content exceeds the maximum height.
Allowing Scroll Anchoring
.scroll-panel {
max-height: 300px;
overflow-y: auto;
overflow-anchor: auto;
}
Disabling Scroll Anchoring
.scroll-panel {
max-height: 300px;
overflow-y: auto;
overflow-anchor: none;
}
Document-Level Scroll Anchoring
html {
overflow-anchor: none;
}
In supporting browsers, applying none to the root element opts the document's scrolling content out of scroll anchoring.
Important Consideration
The overflow-anchor property does not create a scroll container. Use properties such as overflow-y: auto and appropriate size constraints when a scrollable region is required.
Scroll Adjustments and Suppression
Scroll anchoring does not compensate for every layout change. The browser may suppress an adjustment when certain operations occur.
Automatic Scroll Adjustments
When an anchor moves because content above it changes size, the browser may adjust the scroll offset to preserve the anchor's position in the visible area.
Suppression Triggers
Changes to certain layout-related properties, including dimensions, margins, padding, positioning, and transforms on relevant elements, can suppress an anchoring adjustment.
Intentional Scrolling
Browser scrolling operations and application-controlled scrolling can affect how anchoring adjustments are applied.
Example
.dynamic-panel {
overflow-anchor: auto;
}
Even with auto, the browser may choose not to adjust the scroll position when suppression rules apply.
Important Consideration
Do not assume that enabling scroll anchoring guarantees an unchanged visual position after every content update. Test dynamic layouts under realistic loading and interaction conditions.
overflow-anchor Example
This example creates two scrollable panels. One uses the default scroll anchoring behavior, while the other opts out. Adding content above the visible items demonstrates how supporting browsers may adjust their scroll positions differently.
HTML Example
<div class="anchor-demo">
<div class="anchor-column">
<h3>overflow-anchor: auto</h3>
<div class="anchor-panel anchor-auto" id="anchor-auto">
<div class="anchor-items">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
<div>Item 6</div>
<div>Item 7</div>
<div>Item 8</div>
</div>
</div>
</div>
<div class="anchor-column">
<h3>overflow-anchor: none</h3>
<div class="anchor-panel anchor-none" id="anchor-none">
<div class="anchor-items">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
<div>Item 6</div>
<div>Item 7</div>
<div>Item 8</div>
</div>
</div>
</div>
<button type="button" id="add-anchor-items">Add Items Above</button>
</div>
CSS Example
.anchor-demo {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.anchor-column {
width: 220px;
}
.anchor-column h3 {
font-size: 16px;
}
.anchor-panel {
height: 150px;
overflow-y: auto;
border: 2px solid #64748b;
background-color: #f8fafc;
}
.anchor-items > div {
padding: 12px;
border-bottom: 1px solid #cbd5e1;
}
.anchor-auto {
overflow-anchor: auto;
}
.anchor-none {
overflow-anchor: none;
}
JavaScript Example
const autoPanel = document.querySelector("#anchor-auto");
const nonePanel = document.querySelector("#anchor-none");
const addButton = document.querySelector("#add-anchor-items");
autoPanel.scrollTop = 120;
nonePanel.scrollTop = 120;
addButton.addEventListener("click", () => {
[autoPanel, nonePanel].forEach((panel) => {
const item = document.createElement("div");
item.textContent = "New item";
panel.querySelector(".anchor-items").prepend(item);
});
});
overflow-anchor: auto
overflow-anchor: none
Both panels start partway down their content. Select Add Items Above to insert a new item at the beginning of each list. In browsers that support scroll anchoring, the first panel may adjust its scroll position to keep the previously visible items in place, while the second panel does not use scroll anchoring.
The exact result depends on browser support and scroll anchoring eligibility rules. If the panels are not initially scrolled, scroll them downward before adding an item.
Accessibility Considerations
Stable scroll positions can improve usability for people reading long pages or navigating dynamically updated content.
Preserve the Reading Position
Unexpected content movement can make it difficult to follow text, especially when images or other elements load above the current reading position.
Avoid Unnecessary Opt-Outs
Disabling scroll anchoring across an entire page can reintroduce unexpected scroll position changes. Prefer targeted exclusions when automatic anchoring causes a specific problem.
Reserve Space for Dynamic Content
Provide dimensions or appropriate placeholders for asynchronously loaded content to reduce layout shifts before they occur.
Keyboard Navigation
Test keyboard focus behavior when content is inserted or resized. Scroll anchoring does not replace the need to manage focus appropriately in interactive interfaces.
Important Consideration
Scroll anchoring is a browser behavior rather than a complete accessibility solution. Combine it with stable layouts, predictable focus management, and accessible scrolling controls.
Common Mistakes
Assuming none Prevents All Scroll Movement
.dynamic-panel {
overflow-anchor: none;
}
This excludes the panel from anchor selection. It does not prevent layout changes or all possible scroll adjustments elsewhere.
Trying to Re-Enable an Excluded Descendant
.parent {
overflow-anchor: none;
}
.parent .child {
overflow-anchor: auto;
}
The child cannot opt back into scroll anchoring while it remains inside the excluded subtree.
Confusing overflow-anchor with overflow
The overflow property controls clipping and scrolling. The overflow-anchor property controls participation in scroll anchoring.
Using Invalid Values
.box {
overflow-anchor: hidden;
}
Only auto, none, and CSS-wide keywords are valid.
Expecting auto to Select a Specific Element
The browser determines the anchor element. The property does not provide a way to explicitly designate one.
Using Scroll Anchoring Instead of Stable Layouts
Scroll anchoring may compensate for layout shifts, but reserving space for content is usually the better solution.
Ignoring Browser Support
Some browsers do not support the property. Interfaces should remain functional without it.
Disabling Anchoring Globally Without Testing
A global opt-out can make long pages less stable when content loads dynamically.
Important Considerations
Keep the following points in mind when using overflow-anchor:
- The property controls participation in scroll anchor selection.
- Its initial value is
auto. - It accepts
autoandnone. - The property is not inherited.
- It applies to all elements.
autoallows eligible elements to participate in scroll anchoring.noneexcludes an element and its descendants from anchor selection.- Descendants of an excluded subtree cannot opt back in using
auto. - The browser selects anchor elements automatically.
- The property does not directly set a scroll position.
- The property does not create a scroll container.
- Scroll anchoring can compensate for content inserted or resized above the viewing position.
- Automatic scroll adjustments are not guaranteed for every layout change.
- Suppression rules can prevent certain anchoring adjustments.
- Excluding one element does not disable anchoring for unrelated elements.
- Applying
noneto a scroll container can disable anchoring for that container. - Dynamic content can cause layout shifts even when anchoring is enabled.
- Reserving space for images and asynchronous content helps prevent layout shifts.
- Scroll anchoring is particularly useful for long documents and dynamic interfaces.
- Custom scroll management may require targeted anchoring exclusions.
- Browser support is not universal.
- Interfaces should remain functional when the property is unsupported.
- Test scrolling, focus behavior, and content updates together.
Browser Support
The CSS overflow-anchor property has Limited availability and is not supported in every major browser.
It is supported in Chromium-based browsers, including Chrome and Edge, and in Firefox. Safari support remains a compatibility consideration.
Browsers that do not support the property ignore its declarations. Their scrolling behavior is determined by their own scrolling implementations.
For current compatibility information, visit Can I Use? and search for overflow-anchor.
Specifications
The overflow-anchor property is defined in the CSS Scroll Anchoring Module Level 1 specification.
CSS Scroll Anchoring Module Level 1: Exclusion API
The specification describes anchor selection, scroll position adjustments, suppression rules, and the mechanism for excluding elements from scroll anchoring.
For additional documentation and examples, consult MDN Web Docs.
