Open In App

How To Control The Scroll Snap in Tailwind CSS?

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To simplify scroll snap effects, a utility-first CSS framework, Tailwind CSS, helps with some utilities. When scroll snapping is in place, users are guided through the content smoothly by snapping elements into position as they scroll down the page, resulting in a more polished and responsive experience for your websites.

Prerequisites

Approach

  • Creating a horizontally or vertically scrolling container.
  • Applying scroll snap properties to align content after scrolling.
  • Customizing scroll snap behavior to control the snap alignment.

Steps To Control The Scroll Snap

Step 1: Create a new project

  • Create a new project directory and navigate into it using the following commands in your terminal:
mkdir tailwind-colors
cd tailwind-colors
  • Next, initialize a new Node.js project using the following command:
npm init -y

Step 2: Install Tailwind CSS

npm i tailwindcss postcss autoprefixer
npx tailwindcss init

Step 3: Configure Tailwind to remove unused styles in tailwind.config.js:

/** @type {import('tailwindcss').Config} */

module.exports = {
content: ["./public/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
  • Create a new public directory in the root of our project directory. In the public directory create new files .public/index.html and .public/tailwind.css.

Step 4: Configure tailwind.css

@tailwind base;
@tailwind components;
@tailwind utilities;

After adding code in tailwind.css run on command in terminal for style.css.

npx tailwindcss -i public/tailwind.css -o public/styles.css --watch

import image:

https://drive.google.com/drive/u/0/folders/1l7jxjRtehNuBqMPf4EXeXmVU5csGll1H

Start the Tailwind CLI build process: Run the CLI tool to scan your template files for classes and build your CSS:

npx tailwindcss -i public/tailwind.css -o public/styles.css --watch

Project Structure:

Screenshot-2024-10-03-182147
Project Structure

Updated Dependencies:

{
"name": "tailwind-skew-button-gfg",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"autoprefixer": "^10.4.20",
"postcss": "^8.4.47",
"tailwindcss": "^3.4.13"
}
}

Example 1: Snap-start in Horizontal way using the images all setup is same only change in index.html code file

HTML
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Scroll Snap End Demo</title>
    <link href="styles.css" rel="stylesheet">
    <style>
        /* Basic styles for horizontal scrolling and snapping */
        .snap-x {
            display: flex;
            overflow-x: scroll;
            scroll-snap-type: x mandatory;
            gap: 10px;
            /* Add some space between the items */
            padding: 10px;
        }

        .snap-center {
            scroll-snap-align: center;
            flex-shrink: 0;
        }

        img {

            width: 100px;
            height: 90px;
            object-fit: cover;
            border-radius: 8px;
        }

        /* Hide the scrollbar for better UX */
        .snap-x::-webkit-scrollbar {
            display: none;
        }

        .snap-x {
            -ms-overflow-style: none;
            /* IE and Edge */
            scrollbar-width: none;
            /* Firefox */
        }
    </style>
</head>

<body>
    <div class="mt-20 snap-x">
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads/
                      20241007122637359872/1.png">
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122822282016/2.png">
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007123135405175/file.jpg" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads/
                      20241007122637359872/1.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads/
                      20241007122637359872/1.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122822282016/2.png">
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007123135405175/file.jpg" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122822282016/2.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122822282016/2.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007123135405175/file.jpg" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads/
                      20241007122637359872/1.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007123135405175/file.jpg" />
        </div>
    </div>
</body>

</html>

Output:

Example 2: This example shows the Snap-start vertical way.

HTML
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Scroll Snap End Demo</title>
    <link href="styles.css" rel="stylesheet">
    <style>
        /* Basic styles for vertical scrolling and snapping */
        .snap-y {
            display: flex;
            flex-direction: column;
            overflow-y: scroll;
            scroll-snap-type: y mandatory;
            gap: 10px;
            /* Add some space between the items */
            padding: 10px;
            height: 100vh;
            /* Full viewport height */
            justify-content: center;
            /* Center content horizontally */
            align-items: center;
            /* Center content vertically */
        }

        .snap-center {
            scroll-snap-align: center;
            flex-shrink: 0;
        }

        img {
            width: 100px;
            height: 90px;
            object-fit: cover;
            border-radius: 8px;
        }

        /* Hide the scrollbar for better UX */
        .snap-y::-webkit-scrollbar {
            display: none;
        }

        .snap-y {
            -ms-overflow-style: none;
            /* IE and Edge */
            scrollbar-width: none;
            /* Firefox */
        }
    </style>
</head>

<body>
    <div class="snap-y">
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122637359872/1.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122822282016/2.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007123135405175/file.jpg" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122822282016/2.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007122637359872/1.png" />
        </div>
        <div class="snap-center">
            <img src="https://media.geeksforgeeks.org/wp-content/uploads
                      /20241007123135405175/file.jpg" />
        </div>
    </div>
</body>

</html>

Output:


Next Article
Article Tags :

Similar Reads