日本語

“CSS scroll-target-group: Highlight the Current Section in a Table of Contents”

​​CSS​Accessibility,HTML,Playwright

Adding one line, scroll-target-group: auto, to a table-of-contents list (<ol>) makes the link for the section you're reading selectable with :target-current. The browser decides which link counts as the current one and updates it as you scroll. No JavaScript is needed to track the scroll position.

A page with a table of contents on the left and the body on the right, scrolling down. Each time the top edge of a body section (the light blue line) crosses the dotted line at the center of the screen, the pink item in the table of contents moves down one entry, from Introduction to FAQ. The labels in the image are in Japanese.
The table of contents highlight moving as the page scrolls down. The scrollY readout at the top, the pink highlight shown when it switches, the dotted line at the center of the screen, and the gray bars standing in for body text were added for the recording. The screen size (640×480) and top margin differ from the test page used in the text, so the scrollY values do not match the tables below.
Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened a test page (screen size 800×600) with Playwright, scrolled and clicked on it, and checked which link matched :target-current using element.matches(). I did not test Firefox or Safari, screen readers, or scrolling with a touch screen or trackpad. The browser support versions come from the compatibility data (@mdn/browser-compat-data 8.1.8).

Add One Line to the List and Color :target-current

The table of contents is an ordinary list of links to sections on the same page. The # part of each link's href is followed by the id of the section in the body.

html
<div class="layout">
<nav class="toc" aria-label="目次">
  <ol>
    <li><a href="#intro">はじめの節</a></li>
    <li><a href="#install">インストール</a></li>
    <li><a href="#usage">使い方</a></li>
    <li><a href="#options">オプション</a></li>
    <li><a href="#faq">よくある質問</a></li>
  </ol>
</nav>
<main>
  <h1>記事のタイトル</h1>
  <section id="intro"><h2>はじめの節</h2>…</section>
  <section id="install"><h2>インストール</h2>…</section>
  <!-- The remaining sections are the same -->
</main>
</div>

In CSS, I add scroll-target-group: auto to the table-of-contents <ol> and color the link that matches :target-current. In the test page, I placed the table of contents on the left with a grid and used position: sticky to keep it on screen.

css
.layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 24px;
}
.toc {
  position: sticky;
  top: 16px;
  align-self: start;
}
.toc ol {
  scroll-target-group: auto;
  list-style: none;
  margin: 0;
  padding: 0 0 0 12px;
}
.toc a {
  display: block;
  padding: 6px 10px;
  color: #1f2a30;
  text-decoration: none;
  border-left: 3px solid transparent;
}
.toc a:target-current {
  color: #d85878;
  font-weight: bold;
  border-left-color: #d85878;
  background: #fdeef2;
}

According to MDN's page for scroll-target-group, an element with auto becomes a scroll marker group container, and the <a> elements inside it that have a # link become scroll markers automatically. Among them, the one whose target is currently displayed matches :target-current.

When I moved scrollY from 0 to the end in 1px steps in the test environment, exactly one link matched :target-current at every position. At the very top of the page (scrollY of 0), Introduction matched, so nothing was left unhighlighted. On a page with only the scroll-target-group line removed, no link matched at any position when I ran the same check. On a page where I added one item whose id did not exist on the page, that item was never highlighted, and the positions where the other items switched were the same as before I added it.

The Highlight Switches When a Section's Top Passes the Middle of the Screen

The test page has a 200px-tall title followed by five 700px-tall sections. I moved scrollY from 0 in 1px steps and recorded where the highlight switched. The screen height was 600px.

Highlighted item Section top position scrollY at switch
Introduction 200 0 (from the start)
Install 900 601
Usage 1600 1301
Options 2300 2001
FAQ 3000 2701

At scrollY 601, the top of the Install section is 299px from the top of the screen, a little above half of the 600px screen height. In other words, the highlight moved to the next item when the top of the next section came up past the middle of the screen. The GIF at the top shows the same thing: when the top edge of a section (the light blue line) crosses the dotted line (the center of the screen), the pink item in the table of contents moves down one entry.

This selection follows the procedure given as an example in the specification (CSS Overflow Module Level 5, calculation of active scroll markers). The example picks the last section whose top has reached or passed the top edge of the screen. It also picks a section that has entered the top half of the screen when the previous section's top has moved more than half of the way off the screen.

A Short Last Section Is Also Highlighted When You Scroll to the Bottom

If the last sections are short, their tops may never rise to the middle of the screen even when the page is scrolled to the bottom. So I tested a page where the fourth and fifth sections were 150px tall. The maximum scroll value on this page is 2000, which is short of both the top of the fourth section (2300) and the top of the fifth section (2450).

Highlighted item scrollY at switch
Usage 1301
Options 1790
FAQ 1990

Within about 200px near the end of the scroll, Options and then FAQ were highlighted in turn. Scrolling all the way to the bottom highlights the last item. The specification's example procedure also describes a way to remap positions that cannot reach the edge to positions near the edge. However, the range that the example remaps is 75px on this page, which does not match the roughly 200px I observed.

With a Fixed Header, Use scroll-padding-top to Switch Earlier

When a fixed header sits at the top of the page, the upper part of the body is hidden behind it. I placed an 80px-tall fixed header on the test page, moved the body down by that amount, and tested again (the section tops are now 980, 1680, 2380, and 3080).

With only the header added, the highlight switched at scrollY 681, 1381, 2081, and 2781. In every case, the top of the section was 299px from the top of the screen, which is the same middle-of-screen boundary as without a header. The header's height was not reflected in the switch positions.

When I set the header's height with scroll-padding-top on the root element, the switch positions changed.

css
html {
  scroll-padding-top: 80px;
}

On the page with both the fixed header and scroll-padding-top: 80px, the highlight switched at scrollY 601, 1301, 2001, and 2701. Each of these is exactly 80px earlier than with the header alone. The switch happens when the top of the section is 379px from the top of the screen, which is 299px below the bottom edge of the header. On a page without a fixed header, adding only scroll-padding-top: 80px also moved the switch points to 521, 1221, 1921, and 2621, 80px earlier than without it.

After Clicking a Link, the Highlight Moves Before the Scroll Ends

When I clicked Options in the table of contents, the page moved to the Options section, and the URL ended with #options. The highlight also moved to Options.

I also tested a page with scroll-behavior: smooth set on html so that the scroll is visibly animated. About 156ms after the click, while scrollY was still 380, the highlight was already on Options. Normally, the highlight would be on Introduction at that position. I kept checking about every 150ms. The highlight stayed on Options until the last check, about 780ms after the click (scrollY 2299). The specification says that the item to highlight is decided based on where the scroll finally stops, so that items change right away.

After clicking a link, when I scrolled back up with the mouse wheel, the URL still ended with #options, but the highlight moved back through Usage, Install, and Introduction to match the scroll position.

In a Nested Table of Contents, Write It Only on the Outer List

I also tested a table of contents where subheading items were nested in an inner <ol>.

html
<li><a href="#install">インストール</a>
  <ol>
    <li><a href="#install-npm">npm で入れる</a></li>
    <li><a href="#install-cdn">CDN から読む</a></li>
  </ol>
</li>

If I wrote .toc ol { scroll-target-group: auto; } here, the same setting applied to the inner <ol> too. Then the table of contents had two highlight units, and two items were highlighted at the same time. Even at the top of the page, Introduction and "Install with npm" were both highlighted. When I scrolled down to Usage, "Load from a CDN" stayed highlighted in the inner group. The specification says a marker belongs to the group of its nearest ancestor scroll marker group container. Because the inner <ol> also formed a group, the subheading links left the outer group and were highlighted one at a time only within the inner group.

To apply the setting only to the outer list, I changed the first CSS rule .toc ol to the child selector .toc > ol. On the test page, I rewrote the rule as follows.

css
.toc > ol {
  scroll-target-group: auto;
  list-style: none;
  margin: 0;
  padding: 0 0 0 12px;
}
.toc ol ol {
  padding-left: 16px;
}

After this change, only one item was highlighted at a time. The switch points for scrollY were as follows.

Highlighted item Section top position scrollY at switch
Install 900 601
Install with npm 961 931
Load from a CDN 1211 1087
Usage 1600 1406

On the nested page, the subheading section and the next section, Usage, switched after their tops had moved well above the middle of the screen. "Install with npm" switched when its top was 30px from the top of the screen, and Usage when its top was 194px from the top. If I calculate with the specification's example procedure, "Install with npm" would switch at 961 and Usage at 1512. Those values did not match what I observed.

Read and Unread Items Can Be Styled with :target-before and :target-after

Items before the highlighted one can be selected with :target-before, and items after it with :target-after. When I scrolled to a position where Usage was highlighted in the test environment, :target-before matched Introduction and Install, and :target-after matched Options and FAQ.

For example, to fade out the items for sections you have already read, you can write the following.

css
.toc a:target-before {
  color: #9aa5ab;
}

These two pseudo-classes arrived in a later version than :target-current (see the table in the last section).

Only the Appearance Changes; No Screen Reader Information Is Added

:target-current changes only the visual appearance in CSS. In the test environment, I pulled the accessibility tree from the Chrome DevTools Protocol. Every table-of-contents link was a plain link, whether or not it was highlighted. No state indicating the current item was added, and the links had no aria-current attribute.

Keyboard behavior is the same as for ordinary links. When I pressed the Tab key, focus moved through the table-of-contents links one at a time from the top, regardless of which item was highlighted. MDN's page for scroll-target-group also notes that markers created with scroll-target-group have no special navigation behavior. They do not get the tablist and tab roles or arrow-key movement that markers created with scroll-marker-group receive.

Firefox and Safari Do Not Support It Yet

The compatibility data shows the following support. MDN marks all of these features as experimental.

Browser scroll-target-group :target-current :target-before and :target-after
Chrome and Edge (including Chrome on Android) From 140 From 135 From 142
Opera From 124 From 120 From 126
Samsung Internet From 30.0 From 29.0 From 30.0
Firefox (including Android) Not supported Not supported Not supported
Safari (including iOS) Not supported Not supported Not supported

Although :target-current is supported from 135, according to MDN's page for :target-current, it matches only the ::scroll-marker pseudo-element and elements that became scroll markers through scroll-target-group. So for table-of-contents links, it works only in versions that support scroll-target-group.

In Chromium 153 in the test environment, CSS.supports('scroll-target-group', 'auto'), CSS.supports('selector(:target-current)'), and the checks for :target-before and :target-after all returned true.

​Posted by 86