How to Create Infinite Scroll in CSS and JavaScript
Learn to implement infinite scroll using the Intersection Observer API. Create seamless, endless scrolling experiences like social media feeds.
Live Demo
Scroll down to load more
Item 1
This is content item number 1. Scroll down to load more items automatically.
Item 2
This is content item number 2. Scroll down to load more items automatically.
Item 3
This is content item number 3. Scroll down to load more items automatically.
Item 4
This is content item number 4. Scroll down to load more items automatically.
Item 5
This is content item number 5. Scroll down to load more items automatically.
How It Works
- 1.An Intersection Observer watches a "sentinel" element at the bottom of the list
- 2.When the sentinel becomes visible (user scrolled near bottom), it triggers a callback
- 3.The callback fetches more data from an API and appends new items
- 4.Loading states prevent duplicate requests; hasMore tracks when content is exhausted
Why Intersection Observer? It's asynchronous and doesn't block the main thread, unlike scroll event listeners which fire on every scroll and can cause jank.
Step-by-Step Tutorial
Follow these steps to implement infinite scroll. Click any code block to copy.
Set Up HTML Structure
Create a container for your items, a sentinel element that the observer will watch, and loading/end indicators.
Add CSS Styles
Style your container, items, loading spinner, and end message. The spinner uses a CSS animation for the rotating effect.
Initialize Intersection Observer
Create an Intersection Observer that watches the sentinel element. The rootMargin option starts loading before the sentinel is fully visible.
Load More Content
When the observer triggers, fetch new content from your API and append it to the container. Use an isLoading flag to prevent duplicate requests.
Handle End of Content
When there's no more content, disconnect the observer and show an end message. This prevents unnecessary API calls.
Complete Reusable Implementation
Here's a complete, reusable class-based implementation:
Best Practices
Do
- +Use rootMargin to preload content before user reaches bottom
- +Implement loading states to prevent duplicate requests
- +Disconnect observer when content is exhausted
- +Provide "Load More" button as accessibility fallback
- +Show skeleton loaders during loading
Don't
- -Use scroll event listeners without throttling
- -Load too much data at once (keep to 10-20 items)
- -Forget to clean up observers on unmount
- -Use for SEO-critical paginated content
- -Ignore error handling for failed requests
Browser Support
Intersection Observer has excellent support in modern browsers:
Chrome
51+
Firefox
55+
Safari
12.1+
Edge
15+
IE
No support
For legacy browser support, use a polyfill or fall back to scroll event listeners.
Frequently Asked Questions
Infinite scroll is a web design technique that automatically loads more content as the user scrolls down the page. Instead of traditional pagination with page numbers, new content appears seamlessly, creating an endless scrolling experience like social media feeds.
Use Intersection Observer API for better performance. It runs asynchronously and doesn't block the main thread. Scroll event listeners fire on every scroll, which can cause jank. Only use scroll events if you need to support IE11 or older browsers.
Use a loading flag (isLoading) to prevent duplicate requests while one is in progress. Also implement a hasMore flag to stop requests when all content is loaded. Set rootMargin on your Intersection Observer to preload content before the user reaches the bottom.
Infinite scroll can hurt SEO because search engines may not trigger the scroll events needed to load more content. For SEO-critical pages, consider using pagination or implement progressive enhancement with a 'Load More' button as fallback.
Avoid infinite scroll for e-commerce category pages (users need to compare and bookmark items), pages with important footer content, content with a logical endpoint (like chapters), and when users need to return to a specific position.
More CSS Tutorials
Copy CSS from Website
Extract styles with 2 clicks
Center a Div (CSS)
Flexbox, Grid, and more
Center a Div (Tailwind)
Utility classes for centering
Convert CSS to Tailwind
CSS to utility classes
Glassmorphism Effect
Frosted glass UI style
Neumorphism Button
Soft UI button design
Responsive Navbar
Mobile-friendly navigation
Sticky Header
Fixed navigation on scroll
CSS Pagination
Page navigation styles
CSS Accordion
Expandable content sections
Dropdown Menu
Hover and click dropdowns
CSS Modal
Popup dialog boxes
Toast Notifications
Animated alert messages
Hamburger Menu
Animated menu icon
Animate Gradients
Moving gradient backgrounds
Skeleton Loader
Loading placeholder UI
Element Screenshot
Capture any element as image
Pick Color from Website
Eyedropper tool comparison
Identify Fonts
Find fonts on any website
Download All Images
Bulk save images from any site
Measure Elements
Page ruler and measurements
Extract Colors from Website
Get any color palette instantly
Dark Mode Toggle
CSS variables and localStorage
Responsive Grid
CSS Grid auto-fit and minmax
Center Text in CSS
text-align, Flexbox, and Grid
Make Text Bold
font-weight values explained
Add Shadow in CSS
box-shadow, text-shadow, drop-shadow
Round Corners in CSS
border-radius and pill shapes
Style File Input
Custom upload buttons
