
π Modern CSS That Replaces JavaScript (Part 2)
You donβt need JavaScript to detect sticky headers anymore. Sticky Header Style on Scroll β NO JS Needed For years, changing a header style on scroll meant JavaScript. Scroll listeners. State management. Performance tuning. π But modern CSS can now handle sticky header state changes natively. π₯ Whatβs New? With scroll-state container queries, CSS can detect when an element becomes sticky (stuck) and change styles automatically. No scroll listeners. No JS state. No hacks. π§ Real Use Case: Sticky Header Style Change A common UX pattern: π Header changes background, radius, or shadow when it sticks to the top. Traditionally: JS checks scroll position Adds/removes a class Handles animations With modern CSS: CSS detects sticky state Styles update automatically Smooth transitions included π§© Minimal CSS Example .header-wrapper { position: sticky; top: 0; container-type: scroll-state; container-name: header; } @container header scroll-state(stuck: top) { .header-inner { background: yellow; col
Continue reading on Dev.to Webdev
Opens in a new tab


