Theodore L.’s Post

Let CSS know when you scroll. No JavaScript needed. Scroll-state queries let you style elements based on how a container is scrolled. You can detect when it’s at the top, bottom, or in motion, and trigger animations directly from CSS. Here’s a simple example: html { container-type: scroll-state; container-name: scroller; } .to-top { position: fixed; bottom: 20px; right: 20px; translate: 80px 0; transition: all 0.4s ease; } @container scroller scroll-state(scrollable: top) { .to-top { translate: 0 0; } } The key is `container-type: scroll-state`. It makes the page a scroll container that CSS can observe. The `@container` rule reacts to its scroll state. When the container is at the top, bottom, or being scrolled, styles update automatically. No scroll listeners. No intersection observers. Just CSS responding to motion. You can use it to fade headers, slide elements, or trigger transitions as users scroll. Browser support is about 68.5%, so it’s still experimental, use with caution (or don't use it yet). This is a glimpse of a more reactive CSS. Less scripting, more design freedom. And if you wanna grasp what's possible to do with modern CSS... I created a small 7-day email course to help you with that... Here's the link: https://lnkd.in/ezBJUPQK

  • Make your elements scroll-aware with the new scroll-state query

Oh very cool, I was coding this just a couple of weeks ago

I really like this but alas it's not ready for production. I get by with a very simple javascript function that toggles a class for top-most position, scrolled, and scrolling upward. That way the styling stays within CSS.

Be careful with the browser support.

Inna Kostiuk

Front-End Engineer | Next.js, React, TypeScript | Building High-Performance Web Applications & Scalable UI

2mo

got into CSS scroll driver animations on one of my projects, the task was to reveal cards on scroll one by one (to force user to see information before scrolling to the bottom of the page). Took me a white to get it right.. 🫣 I used Framer Motion though, excited to get more into CSS scroll driver animations

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories