Creating a scroll-spy with 2 lines of CSS
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
A new CSS property called scroll-target-group combined with the :target-current pseudo-class enables developers to create scroll-spy table of contents with just two lines of CSS. This feature, landing in Chrome 140, eliminates the need for JavaScript when building navigation that highlights the current section as users scroll. The implementation requires setting scroll-target-group: auto on the parent container and styling active links with :target-current. The feature works well with progressive enhancement, allowing developers to conditionally display the functionality only in supported browsers while maintaining basic anchor link navigation in others.
Table of contents
IntroductionSpec examplescroll-target-groupTLDR; the 2 lines of CSSSmoother navigationProgressive enhancementAccessibility considerationsConclusionSort: