CSS-only scrollspy effect using scroll-marker-group and :target-current

Sara Soueidan · 2025年8月18日

AI 摘要 AI 生成,可能有误

本文介绍了如何使用新增的 scroll-target-group 属性结合 :target-current 伪类,创建纯 CSS 的滚动侦测效果:当目标区块滚动进入视野时,导航链接(如目录)会自动高亮显示——无需任何 JavaScript。文中描述了将 scroll-target-group 应用于包含 HTML 锚点的容器后,这些锚点会被提升为滚动标记,由浏览器来确定当前处于活动状态的链接并为其应用样式。作者还探讨了无障碍性方面的考虑,包括活动链接样式需满足的 WCAG 对比度要求,以及使用 aria-current 向屏幕阅读器用户传达活动状态。

✨ This post is sponsored by everyone who has bought my Practical Accessibility course. ✨

The Bootstrap Scrollspy—now commonly known as just “Scrollspy”—is a feature that automatically updates navigation links based on the user’s scroll position to indicate which link is currently active in the viewport. It is popular because it aims to enhance the user experience by providing visual cues about which part of the content is currently being viewed.

The Scrollspy effect demonstrated in the Bootstrap documentation shows the navbar links are highlighted when their respective target sections are scrolled into view.
继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Sara Soueidan

The CSS caret-* properties and custom caret accessibility

AI 摘要This article examines the CSS caret-* properties (caret-color, caret-animation, and caret-shape) and the accessibility considerations surrounding the caret, an often-overlooked focus indicator for editable text and caret browsing mode. It explains how caret browsing enables precise keyboard navigation of web pages, and how operating systems like macOS and Windows provide accessibility settings for caret blinking, thickness, and visibility that browsers honor inconsistently. The author urges caution when applying custom caret styles, since the caret may be the only focus indicator some keyboard or pointer users receive.

My top picks for Interop 2027

AI 摘要The author shares their ranked picks for Interop 2027, a yearly project aiming to improve cross-browser interoperability of key web platform features, using Firefox's community ranking page. Their top choices—the HTML focusgroup attribute and the ariaNotify() API—reflect their preference for features that improve both accessibility/user experience and developer experience, followed by other picks like heading-related HTML attributes, Interest Invokers, and various CSS properties. The author notes that features cannot be downvoted, only ranked or left unranked, and concludes by linking to blog posts where other web developers shared their own rankings.

CSS to speech: alternative text for CSS-generated content

AI 摘要The article explains how CSS-generated content inserted via ::before and ::after pseudo-elements is included in an element's accessible name computation, and why meaningful CSS images, icons, and unicode characters need descriptive alternative text while decorative ones should be hidden from screen readers. It introduces the CSS Generated Content Module Level 3 syntax that lets authors provide alternative text after a slash in the content property (with empty-string alt text for decorative content), noting that browser support is now very good except for older browsers like Internet Explorer. The author shares testing results across browsers and screen readers such as VoiceOver, observing that CSS images without alt text are excluded from the accessibility tree, images with alt text are announced properly, and there are some quirks like NVDA not announcing Chrome's CSS image alt text and Safari only showing broken CSS image alt text when VoiceOver is enabled.

🎥 New (Video) Podcast: "Navigating the World of Web Accessibility with Sara Soueidan", with Kevin Powell

AI 摘要The author and Kevin Powell finally recorded a nearly two-hour video podcast about navigating the world of web accessibility, covering topics like HTML semantics, ARIA, screen readers, the accessibility tree, how CSS affects accessibility, and the author's Practical Accessibility course. They also chatted about shared personal interests such as cooking, fermentation, and nutrition. The author invites front-end designers and developers curious about accessibility to watch or listen and share their feedback.

Are 'CSS Carousels' accessible?

AI 摘要The article critically examines the accessibility of the newly announced 'CSS Carousels,' a set of CSS-only scrolling UI patterns built with new pseudo-elements like ::scroll-marker and ::scroll-button defined in the CSS Overflow Module 5 specification. Despite claims that carousel accessibility best practices are handled by the browser, the author's inspection of examples reveals significant accessibility and usability concerns, largely because these interactive pseudo-elements are not real DOM elements and the features are still experimental (currently only available in Chrome Canary behind a flag). The post aims to raise accessibility awareness so developers do not adopt these features prematurely and create confusing experiences for assistive technology users.