search results:

    • Standard
    • React
    Pricing Learn Community
    • + D
    • Light
    • Dark
    • System
    logo TW Elements
    • Getting started
      • Quick start
      • Tutorials
      • Design system
      • Local installation
      • Optimization
      • Dark mode
      • Theming
      • Changelog
      • Migrating to v2
      • Internationalization guide
      • Class customization
      • Icons Integration
    • CommercialNew
      • Pricing
      • License
      • Installation
      • Git & repository
      • Premium Support
    • Integrations
      • Angular
      • ASP.NET
      • Django
      • Express
      • Laravel
      • Next
      • Nuxt
      • Qwik
      • React
      • Remix
      • Solid
      • Svelte
      • SvelteKit
      • Vue
    • Content & styles
      • Animations
      • Animations Extended
      • Colors
      • Dividers
      • Figures
      • Headings
      • Hover effects
      • Icons
      • Images
      • Mask
      • Shadows
      • Typography
    • Navigation
      • Breadcrumbs
      • Footer
      • Headers
      • Mega menu
      • Navbar
      • Offcanvas
      • Pagination
      • Pills
      • Scrollspy
      • Sidenav
      • Tabs
    • Components
      • Accordion
      • Alerts
      • Avatar
      • Badges
      • Button group
      • Buttons
      • Cards
      • Carousel
      • Chips
      • Collapse
      • Dropdown
      • Gallery
      • Jumbotron
      • Lightbox
      • Link
      • List group
      • Modal
      • Notifications
      • Paragraphs
      • Placeholders
      • Popconfirm
      • Popover
      • Progress
      • Rating
      • Scroll back to top button
      • Social buttons
      • Spinners
      • Stepper
      • Testimonials
      • Timeline
      • Toast
      • Tooltip
      • Video
      • Video carousel
    • Forms
      • Autocomplete
      • Checkbox
      • Datepicker
      • Datetimepicker
      • File input
      • Form templates
      • Input Group
      • Inputs
      • Login form
      • Multi range slider
      • Radio
      • Range
      • Registration form
      • Search
      • Select
      • Switch
      • Textarea
      • Timepicker
      • Validation
    • Data
      • Charts
      • Charts advanced
      • Datatables
      • Tables
    • Methods
      • Clipboard
      • Infinite scroll
      • Lazy loading
      • Loading management
      • Ripple
      • Scrollbar
      • Smooth scroll
      • Sticky
      • Touch
    • Design Blocks
      • Admin Charts
      • Admin Complex
      • Admin Forms
      • Admin Maps
      • Admin Navigation
      • Admin tables
      • Banners
      • Contact
      • Content
      • CTA
      • FAQ
      • Features
      • Headers
      • Hero / Intro sections
      • Logo clouds
      • Mega menu
      • News
      • Newsletter
      • Pricing
      • Projects
      • Stats
      • Stats admin
      • Team
      • Testimonials
    • Tools
      • Button generator
      • Card generator
      • Flexbox generator
      • Footer generator
      • Form builder
      • Grid generator
      • Icon generator
      • Instagram Filters generator
      • Logo generator
      • Table generator
      • Typography generator
    • Coming Soon
      • Angular
      • Builder
      • Templates
      • Vue
    • Resources
      • Playground
      • YouTube Channel
      • Private FB Group
      • Newsletter
      • UI Design course New
      • UI / UX tips
    • Overview
    • API

    Tailwind Sticky method

    Tailwind CSS Sticky

    Sticky is a component which allows elements to be locked in a particular area of the page. It is often used in navigation menus.

    Required ES init: Sticky *
    * UMD autoinits are enabled by default. This means that you don't need to initialize the component manually. However if you are using TW Elements ES format then you should pass the required components to the initTWE method.

    Basic example

    To start use sticky just add a data-twe-sticky-init attribute to the element you want to pin.

    Download TWE
    • HTML
    • javascript
            
                
          <div class="min-h-[500px] text-center">
            <a
              type="button"
              href="/docs/standard/getting-started/quick-start/"
              target="_blank"
              class="rounded border-2 border-primary px-6 pb-[6px] pt-2 text-xs font-medium uppercase leading-normal text-primary transition duration-150 ease-in-out hover:border-primary-accent-300 hover:bg-primary-50/50 hover:text-primary-accent-300 focus:border-primary-600 focus:bg-primary-50/50 focus:text-primary-600 focus:outline-none focus:ring-0 active:border-primary-700 active:text-primary-700 dark:text-primary-500 dark:hover:bg-blue-950 dark:focus:bg-blue-950"
              data-twe-ripple-init
              data-twe-sticky-init>
              Download TWE
            </a>
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            Sticky,
            initTWE,
          } from "tw-elements";
          
          initTWE({ Sticky });
          
            
        

    Hey there 👋 we're excited about TW elements and want to see it grow! If you enjoy it, help the project grow by sharing it with your peers. Every share counts, thank you!

    Sticky bottom

    You can pin element to bottom by adding data-twe-sticky-position="bottom".

    TE Logo
    • HTML
    • javascript
            
                
          <div class="min-h-[500px]">
            <img
              id="TE-logo-1"
              src="https://tecdn.b-cdn.net/img/logo/te-transparent-noshadows.webp"
              alt="TE Logo"
              data-twe-sticky-init
              data-twe-sticky-position="bottom" />
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            Sticky,
            initTWE,
          } from "tw-elements";
          
          initTWE({ Sticky });
          
            
        

    Boundary

    Set data-twe-sticky-boundary="true" so that sticky only works inside the parent element. Remember to set the correct parent height.

    TE Logo
    • HTML
    • javascript
            
                
          <div class="min-h-[500px]">
            <img
              id="TE-logo-2"
              src="https://tecdn.b-cdn.net/img/logo/te-transparent-noshadows.webp"
              alt="TE Logo"
              data-twe-sticky-init
              data-twe-sticky-boundary="true" />
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            Sticky,
            initTWE,
          } from "tw-elements";
          
          initTWE({ Sticky });
          
            
        

    Outer element as a boundary

    You can specify an element selector to be the sticky boundary.

    TE Logo
    Stop here
    • HTML
    • javascript
            
                
          <div
            class="flex min-h-[500px] w-full flex-col justify-center text-center">
            <div class="flex justify-center">
              <img
                id="TE-logo-3"
                src="https://tecdn.b-cdn.net/img/logo/te-transparent-noshadows.webp"
                alt="TE Logo"
                data-twe-sticky-init
                data-twe-sticky-boundary="#sticky-top"
                data-twe-sticky-direction="both" />
            </div>
            <div id="sticky-top" class="mt-auto h-[5rem]">Stop here</div>
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            Sticky,
            initTWE,
          } from "tw-elements";
          
          initTWE({ Sticky });
          
            
        

    Direction

    Default direction of sticky component is down. You can change it by setting data-twe-sticky-direction="up" or data-twe-sticky-direction="down".

    • HTML
    • javascript
            
                
          <div class="min-h-[500px] text-center">
            <button
              type="button"
              data-twe-ripple-init
              data-twe-ripple-color="light"
              data-twe-sticky-init
              data-twe-sticky-boundary="true"
              data-twe-sticky-direction="up"
              class="rounded bg-primary px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-white shadow-primary-3 transition duration-150 ease-in-out hover:bg-primary-accent-300 hover:shadow-primary-2 focus:bg-primary-accent-300 focus:shadow-primary-2 focus:outline-none focus:ring-0 active:bg-primary-600 active:shadow-primary-2 dark:shadow-black/30 dark:hover:shadow-dark-strong dark:focus:shadow-dark-strong dark:active:shadow-dark-strong">
              Up
            </button>
            <button
              type="button"
              data-twe-ripple-init
              data-twe-ripple-color="light"
              data-twe-sticky-init
              data-twe-sticky-boundary="true"
              data-twe-sticky-direction="down"
              class="rounded bg-primary px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-white shadow-primary-3 transition duration-150 ease-in-out hover:bg-primary-accent-300 hover:shadow-primary-2 focus:bg-primary-accent-300 focus:shadow-primary-2 focus:outline-none focus:ring-0 active:bg-primary-600 active:shadow-primary-2 dark:shadow-black/30 dark:hover:shadow-dark-strong dark:focus:shadow-dark-strong dark:active:shadow-dark-strong">
              Down
            </button>
            <button
              type="button"
              data-twe-ripple-init
              data-twe-ripple-color="light"
              data-twe-sticky-init
              data-twe-sticky-boundary="true"
              data-twe-sticky-direction="both"
              class="rounded bg-primary px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-white shadow-primary-3 transition duration-150 ease-in-out hover:bg-primary-accent-300 hover:shadow-primary-2 focus:bg-primary-accent-300 focus:shadow-primary-2 focus:outline-none focus:ring-0 active:bg-primary-600 active:shadow-primary-2 dark:shadow-black/30 dark:hover:shadow-dark-strong dark:focus:shadow-dark-strong dark:active:shadow-dark-strong">
              Both
            </button>
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            Sticky,
            initTWE,
          } from "tw-elements";
          
          initTWE({ Sticky });
          
            
        

    Animation

    You can add an animation that will run when the sticky starts and when the sticky element is hidden. just specify the css class of the animation using the data-twe-sticky-animation-sticky and data-twe-sticky-animation-unsticky attributes.

    TE Logo
    • HTML
    • javascript
            
                
          <div class="min-h-[500px]">
            <img
              id="TE-logo-4"
              src="https://tecdn.b-cdn.net/img/logo/te-transparent-noshadows.webp"
              alt="TE Logo"
              data-twe-sticky-init
              data-twe-sticky-animation-sticky="animate-[slide-in-down_0.2s_ease-in-out]"
              data-twe-sticky-animation-unsticky="animate-[slide-up_0.2s_ease-in-out]"
              data-twe-sticky-boundary="true"
              data-twe-sticky-z-index="49" />
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            Sticky,
            initTWE,
          } from "tw-elements";
          
          initTWE({ Sticky });
          
            
        

    Sticky with navbar

    By default, sticky locks the element at the edge of the screen. If you have a navbar element on your website, it will hide behind it. You can prevent this by setting an offset and delay. You can also change z-index of the sticky element by setting data-twe-sticky-z-index attribute.

    Navbar
    TE Logo
    • HTML
    • javascript
            
                
          <div class="min-h-[500px] w-full text-center">
            <nav
              class="sticky top-0 z-auto mb-3 flex w-full flex-wrap items-center justify-between bg-zinc-50 py-2 shadow-dark-mild dark:bg-neutral-700 lg:flex-wrap lg:justify-start lg:py-4">
              <div class="flex w-full flex-wrap items-center justify-between px-3">
                <div class="ms-2">
                  <a class="text-xl text-black dark:text-white" href="#">Navbar</a>
                </div>
              </div>
            </nav>
            <img
              id="TE-logo-5"
              src="https://tecdn.b-cdn.net/img/logo/te-transparent-noshadows.webp"
              alt="TE Logo"
              data-twe-sticky-init
              data-twe-sticky-boundary="true"
              data-twe-sticky-direction="both"
              data-twe-sticky-offset="75"
              data-twe-sticky-delay="75"
              data-twe-sticky-z-index="auto"
              class="mx-auto" />
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            Sticky,
            initTWE,
          } from "tw-elements";
          
          initTWE({ Sticky });
          
            
        

    Related resources

    Scroll back to top button Navbar Animations
    • Basic example
    • Sticky bottom
    • Boundary
    • Outer element as a boundary
    • Direction
    • Animation
    • Sticky with navbar
    • Related resources

    Tailwind Sticky method - API


    Import

    Importing components depends on how your application works. If you intend to use the TW elements ES format, you must first import the component and then initialize it with the initTWE method. If you are going to use the UMD format, just import the tw-elements package.

    • javascript
    • umd
            
                
            import { Sticky, initTWE } from "tw-elements";
            initTWE({ Sticky });
            
            
        
            
                
            import "tw-elements";
            
            
        

    Usage

    Via data attributes

    Simply add data-twe-sticky-init attribute to the element to initialize the Sticky component. For ES format, you must first import and call the initTWE method.

    • HTML
            
                
            <img
              id="TE-logo"
              src="https://tecdn.b-cdn.net/img/logo/te-transparent-noshadows.webp"
              alt="TE Logo"
              data-twe-sticky-init />
            
            
        

    Via JavaScript

    • javascript
    • umd
            
                
            const mySticky = new Sticky(document.getElementById('example'), options, classes);
            
            
        
            
                
            const mySticky = new twe.Sticky(document.getElementById('example'), options, classes);    
            
            
        

    Options

    Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-twe-, as in data-twe-sticky-direction="down".

    Name Type Default Description
    stickyAnimationSticky String - Set sticky animation. You have to pass the valid animation class like animate-[fade-in_1s_ease-in-out]
    stickyAnimationUnsticky String - Set unsticky animation You have to pass the valid animation class like animate-[fade-out_1s_ease-in-out]
    stickyBoundary Boolean false Set to true to stop sticky on the end of the parent.
    stickyDelay Number 0 Set the number of pixels beyond which the item will be pinned.
    stickyDirection String "down" Set the scrolling direction for which the element is to be sticky.
    stickyMedia Number 0 Set the minimum width in pixels for which the sticky should work.
    stickyOffset Number 0 Set the distance from the top edge of the element for which the element is sticky.
    stickyPosition String 'top' Set the edge of the screen the item should stick to.

    Classes

    Custom classes can be passed via data attributes or JavaScript. For data attributes, append the class name to data-twe-class, as in data-twe-class-sticky-active="".

    Name Default Description
    stickyActive - Set the class to be added when the sticky element is active.

    Methods

    Method Description Example
    active Start sticky manually. mySticky.active()
    inactive Stop sticky manually. mySticky.active()
    dispose Manually deletes an instance of sticky. mySticky.dispose()
    getInstance Static method which allows you to get the Sticky instance associated to a DOM element. Sticky.getInstance(mySticky)
    getOrCreateInstance Static method which returns the Sticky instance associated to a DOM element or create a new one in case it wasn't initialized. Sticky.getOrCreateInstance(mySticky)
    • javascript
    • umd
            
                
            const myStickyEl = document.getElementById('sticky')
            const sticky = new Sticky(myStickyEl)
            sticky.inactive();
            
            
        
            
                
            const myStickyEl = document.getElementById('sticky')
            const sticky = new twe.Sticky(myStickyEl)
            sticky.inactive();
            
            
        

    Events

    Event type Description
    activated.twe.sticky This event fires immediately when sticky start.
    deactivated.twe.sticky This event fires immediately when sticky stop.
    • JavaScript
            
                
            const myStickyEl = document.getElementById('sticky')
            myStickyEl.addEventListener('activated.twe.sticky', (e) => {
                // do something...
            })
            
            
        
    • Import
    • Usage
    • Options
    • Methods
    • Events
    Get useful tips & free resources directly to your inbox along with exclusive subscriber-only content.
    Join our mailing list now
    © 2024 Copyright: MDBootstrap.com

    Access restricted

    To view this section you must have an active PRO account

    Log in to your account or purchase an TWE subscription if you don't have one.

    Buy TWE PRO