Modern CSS That Replaces JavaScript (Part 1) — Scroll-Based Visibility Without JS
This article is the first part of a series where I’ll cover modern CSS features with example that can replace common JavaScript patterns — improving performance, accessibility, and maintainability.
For years, showing or hiding UI elements based on scroll position meant one thing: JavaScript.
Scroll listeners, throttling, performance concerns — we’ve all been there.
But modern CSS is changing that narrative.
With the introduction of scroll-driven container queries, we can now manage scroll-based UI visibility purely with CSS — no JavaScript required.
In this article, we’ll build a “Back to Top” button that appears only when the page is scrollable, using new CSS scroll-state features.
Why This Matters
JavaScript scroll handlers can:
- Hurt performance on low-end devices
- Add unnecessary complexity
- Require debouncing or throttling
- Increase bundle size
Modern CSS gives us:
- Better performance
- Declarative UI behavior
- Cleaner and more maintainable code
The Key Concept: scroll-state Container Queries
CSS now allows us to react to scroll conditions using container queries.
We define:
- A scroll container
- A named scroll state
- Conditional styles based on scroll position
Step 1: Define the Scroll Container
CSS
html {
container-type: scroll-state;
container-name: page;
scroll-behavior: smooth;
}
This turns the html element into a scroll-state container named page.
Step 2: Base Page Styling
CSS
html,
body {
padding: 0;
margin: 0;
}
.title-header {
background-color: #4caf50;
color: white;
padding: 10px 0;
text-align: center;
font-size: 24px;
}
.content {
max-width: 480px;
margin: 0 auto;
}
Nothing special here — just layout and typography.
Step 3: Back to Top Button (Hidden by Default)
CSS
.backtotop {
display: flex;
position: fixed;
bottom: 20px;
right: 20px;
background: #000066;
color: #fff;
padding: 10px 16px;
border-radius: 30px;
font-size: 24px;
text-decoration: none;
translate: 300px 0;
transition: translate 0.3s ease-in-out;
}
We move the button off-screen using translate, not display: none, so animations stay smooth.
Step 4: Reveal Button Using Scroll-State Query
CSS
@container page scroll-state(scrollable: top) {
.backtotop {
translate: 0 0;
}
}
What this means:
- When the page becomes scrollable
- And scrolling is possible from the top
- The button slides into view automatically
- No JavaScript. No scroll listeners.
HTML Markup
<div class="title-header">
Manage Visibility on Scroll with NO JS
</div>
<div class="content">
<h1>NEW CSS Scroll Features Will Replace JavaScript</h1>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry...
</p>
<!-- Long content continues -->
</div>
<a href="#top" class="backtotop">Move to top</a>
Code pen Live Example
Why This Is a Big Deal
✅ Zero JavaScript
✅ Better performance
✅ Declarative UI logic
✅ Cleaner codebase
✅ Perfect for modern design systems
This approach is especially powerful for:
- Sticky headers
- Scroll-to-top buttons
- Progressive disclosure UI
- Accessibility-friendly interactions
Browser Support Notes
These features are cutting-edge and currently supported in modern Chromium-based browsers.
Use progressive enhancement when shipping to production.
Final Thoughts
CSS is no longer just about styling — it’s becoming a behavior layer.
If you care about:
- Performance
- Maintainability
- Accessibility
- Modern frontend architecture
- Then scroll-state container queries are absolutely worth learning.
Happy styling, and thanks for reading!🚀

Top comments (0)