Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Elastic Overflow Scrolling

    Elastic Overflow Scrolling

    August 21, 2024

    A client asked if we could mimic the “rubber band” scrolling behavior on many mobile devices. I’m sure you know what I’m talking about. It’s a behavior that already exists and happens automatically in most browsers. In iOS Safari, for example, you’re allowed to scroll beyond the top or bottom edge of the viewport by a few hundred pixels, and letting go snaps the page back in place.

    I had heard of some instances where someone might want to prevent the bounce from happening but no one had asked me to implement it, especially in a way that supports devices without a touch interface. I was actually a bit surprised there isn’t an existing CSS property for this. There’s the non-standard -webkit-overflow-scrolling property but that’s for a different type of “momentum” scrolling. Nor would I want to rely on a non-standard property that’s not on track to become part of the specifications.

    OK, so what if we want to force this sort of rubber banding in our work? For starters, we’d need some sort of element acting as a container for content that requires scrolling. From there, we could reach for JavaScript, of course, but that involves adding scroll listeners or a combination of pointerDown, pointerUp, and pointerMove events, not to mention keeping track of positions, inertial movement, etc.

    A CSS-only solution would be much more ideal.

    Here is a container with a few child elements:

    <div class=”carousel”>
    <div class=”slides”>
    <div class=”slide”>1</div>
    <div class=”slide”>2</div>
    <div class=”slide”>3</div>
    <div class=”slide”>4</div>
    <div class=”slide”>5</div>
    </div>
    </div>

    Let’s get some baseline styles in place, specifically to create a situation where we’re guaranteed to overflow a parent container.

    /* Parent container with fixed dimensions for overflow */
    .carousel {
    width: 200px;
    height: 400px;
    overflow-x: hidden;
    overflow-y: auto;
    }

    /* Wrapper for slides, stacked in a column */
    .slides {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    width: 100%;
    height: fit-content;
    }

    /* Each slide is the full width of the carousel */
    .slide {
    width: 100%;
    aspect-ratio: 1;
    }

    Let’s start by adding some vertical margins. If your container has only one long item, add it to the top and bottom of the child element. If the container has multiple children, you’ll want to add margin to the top of the first child element and the bottom of the last child element.

    .carousel > .slides > .slide:first-child {
    margin-top: 100px;
    }

    .carousel > .slides > .slide:last-child {
    margin-bottom: 100px;
    }

    Great! We can now scroll past the edges, but we need something to snap it back after the user lifts their finger or pointer. For this, we’ll need the scroll-snap-type and scroll-snap-align properties

    .carousel {
    scroll-snap-type: y mandatory;
    }

    .carousel > .slides > .slide {
    scroll-snap-align: start;
    }

    .carousel > .slides > .slide:first-child {
    margin-top: 100px;
    }

    .carousel > .slides > .slide:last-child {
    scroll-snap-align: end;
    margin-bottom: 100px;
    }

    Note that the same applies to a horizontally scrolling element. For that, you’d change things up so that margin is applied to the element’s left and right edges instead of its top and bottom edges. You’ll also want to change the scroll-snap-type property’s value from y mandatory to x mandatory while you’re at it.

    That’s really it! Here’s the final demo:

    CodePen Embed Fallback

    I know, I know. This isn’t some Earth-shattering or mind-blowing effect, but it does solve a very specific situation. And if you find yourself in that situation, now you have something in your back pocket to use.

    Additional resources

    “The inside story of the iconic ‘rubber band’ effect that launched the iPhone” (Cult of Mac)

    “Six things I learnt about iOS Safari’s rubber band scrolling” (Special Agent Squeaky)

    “Scroll Bouncing On Your Websites” (Smashing Magazine)

    Elastic Overflow Scrolling originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleCommon misconceptions about how to optimize LCP
    Next Article More than 3 in 4 Tech Leaders Worry About SaaS Security Threats, New Survey Reveals

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4831 – TOTOLINK HTTP POST Request Handler Buffer Overflow Vulnerability

    May 17, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    Trello adds 4 major project management features I didn’t know I needed – and they’re free

    News & Updates

    What To Expect When Migrating Your Site To A New Platform

    Development

    Automate Q&A email responses with Amazon Bedrock Knowledge Bases

    Development

    CVE-2025-46761 – Apache HTTP Server Denial of Service

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    News & Updates

    Everything Xbox announced during the January 2025 Developer_Direct

    January 23, 2025

    Xbox hosts a mini-showcase to kick off the new year, highlighting upcoming titles like DOOM:…

    Best Free and Open Source Alternatives to Apple Spotlight

    April 18, 2025

    Pixel Photonics receives €1M grant for multi-mode single photon detection

    February 7, 2025

    Researcher Says Patched Commvault Bug Still Exploitable

    May 6, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.