Scroll Margin

    Utilities for controlling the scroll offset around items in a snap container.

    Show all classes

    Use the utilities to set the scroll offset around items within a snap container.

    Scroll in the grid of images to see the expected behaviour

    Scroll Margin - 图2

    Using negative values

    To use a negative scroll margin value, prefix the class name with a dash to convert it to a negative value.

    1. <div class="-scroll-ml-6 snap-start ...">
    2. <!-- ... -->
    3. </div>

    Scroll in the grid of images to see the expected behaviour

    Scroll Margin - 图4

    For more control, you can also use the to conditionally apply specific styles depending on the current text direction.


    Hover, focus, and other states

    Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:scroll-m-0 to only apply the scroll-m-0 utility on hover.

    1. <div class="scroll-m-8 hover:scroll-m-0">
    2. </div>

    For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

    You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:scroll-m-0 to apply the scroll-m-0 utility at only medium screen sizes and above.


    Customizing your theme

    By default, Tailwind’s scroll margin scale uses the default spacing scale. You can customize your spacing scale by editing theme.spacing or theme.extend.spacing in your tailwind.config.js file.

    tailwind.config.js

    1. module.exports = {
    2. theme: {
    3. spacing: {
    4. }
    5. }
    6. }
    7. }

    Alternatively, you can customize just the scroll margin scale by editing theme.scrollMargin or theme.extend.scrollMargin in your tailwind.config.js file.

    tailwind.config.js

    Learn more about customizing the default theme in the documentation.

    If you need to use a one-off scroll-margin value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

    1. <div class="scroll-m-[24rem]">
    2. <!-- ... -->

    Learn more about arbitrary value support in the documentation.