CSS Portal

CSS anchor-name Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The CSS anchor-name property is part of the CSS Scroll Snap Module and is primarily used to give a scroll container a named anchor that can be referenced for linking and navigation purposes. It allows sections of content within a scrollable container to be targeted, often in combination with smooth scrolling or programmatically controlling scroll positions.

Think of it as a way to assign a label or identifier to a scrollable element so that it can be referenced elsewhere in CSS or JavaScript. This is especially useful in creating scroll snapping layouts or single-page navigation patterns, where you want certain content areas to align neatly or respond to anchor links.

When an element has an anchor-name, it can be scrolled into view automatically when that name is used in a scroll-snap context or in fragment identifiers in URLs. For example, a user might click a link like #section3, and the browser can scroll the container so that the element with anchor-name: section3 becomes visible.

Essentially, the property provides a semantic handle to scrollable content, decoupling the scroll behavior from structural IDs or classes. It’s a way to name scroll positions explicitly, improving accessibility, navigation, and dynamic content control.

Key concepts:

  • Useful for scroll-snapping containers or smooth-scrolling interfaces.
  • Works as a reference point for linking to specific parts of a scrollable container.
  • Helps organize content for navigational purposes without relying solely on HTML id attributes.

Definition

Initial value
none
Applies to
All elements that generate a principal box
Inherited
no
Computed value
as specified
Animatable
yes
JavaScript syntax
object.style.anchorName

Syntax

.anchor-element { anchor-name: --my-anchor-name; }

Values

  • noneThe default value. The element is not an anchor and cannot be referenced by other elements.
  • <dashed-ident>A user-defined identifier. It must start with two dashes (e.g., --tooltip-anchor, --main-menu). This name is then used by the anchor() or anchor-size() functions on a positioned element.
  • initialSets the property to its default value (none).
  • inheritInherits the value from the parent element.

Example

<div class="container">
<nav class="nav">
<a href="#section1">Go to Section 1</a>
<a href="#section2">Go to Section 2</a>
</nav>

<!-- Named anchors -->
<a id="section1" name="section1"></a>
<section class="section">
<h2>Section 1</h2>
<p>Content for Section 1.</p>
</section>

<a id="section2" name="section2"></a>
<section class="section">
<h2>Section 2</h2>
<p>Content for Section 2.</p>
</section>
</div>
/* Basic layout */
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial;
  line-height: 1.5;
  padding: 24px;
  background: #f9fafb;
  color: #111827;
}

/* Navigation */
.nav {
  margin-bottom: 24px;
}

.nav a {
  margin-right: 12px;
  text-decoration: none;
  color: #0366d6;
}

/* Select and style anchors by their name attribute */
a[name="section1"]::before,
a[name="section2"]::before {
  content: "Named anchor: " attr(name);
  display: inline-block;
  background: #eef2ff;
  color: #312e81;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.9rem;
  margin-bottom: 12px;
}

/* Keep the named anchor element as a block so the pseudo-element appears above the section */
a[name="section1"],
a[name="section2"] {
  display: block;
  margin-top: -70px; /* example offset for a fixed header */
  padding-top: 70px;
}

/* Section styling */
.section {
  background: #fff;
  padding: 18px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  margin-bottom: 18px;
}

Browser Support

The following information will show you the current browser support for the CSS anchor-name property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by most modern browsers, though support in some browsers is unknown.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!