ktuiktui
ktuiktui
ComponentsDocsProducts

Getting Started

IntroductionInstallationJavaScriptDark modeRTLReferencesChangelog

Components

AccordionAvatarAlertBadgeBreadcrumbButtonCardCheckboxCollapseDatatableDatepickerDismissDrawerDropdownImage InputInputKbdLinkModalPaginationProgressRadio GroupReparentScrollableScrollspyScrolltoSelectSeparatorSkeletonStepperStickySwitchTabsTextareaTheme SwitchToggleToggle GroupToggle PasswordTooltip
©2025 KTUI. All rights reserved.
A project by Keenthemes
Privacy Policy
Docs
Tailwind Sticky

Tailwind Sticky

Tailwind Sticky component allows elements to remain fixed within their parent container as you scroll. This feature enhances user experience by keeping important content, such as navigation menus or headers, in view even when the page is scrolled.

Examples

Basic Usage

Enables the below navigation panel to remain sticky on during the page scrolling.

<div>
  <div data-kt-sticky-wrapper="true">
    <div
      data-kt-sticky="true"
      data-kt-sticky-name="basic"
      data-kt-sticky-top="50px"
      data-kt-sticky-start="auto"
      data-kt-sticky-offset="300px"
      data-kt-sticky-class="fixed z-10 shadow-md"
      class="w-52 mx-auto p-2 text-sm flex flex-wrap justify-center border border-border rounded-lg py-2"
    >
      <div class="kt-accordion-menu-item">
        <a href="#" class="kt-accordion-menu-link"
          ><svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            class="lucide lucide-badge-check"
          >
            <path
              d="M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z"
            ></path>
            <path d="m9 12 2 2 4-4"></path></svg
          >Home</a
        >
      </div>
      <div class="kt-accordion-menu-item">
        <a href="#" class="kt-accordion-menu-link"
          ><svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            class="lucide lucide-circle-user"
          >
            <circle cx="12" cy="12" r="10"></circle>
            <circle cx="12" cy="10" r="3"></circle>
            <path
              d="M7 20.662V19a2 2 0 0 1 2-2h6a2 2 0 0 1 2 2v1.662"
            ></path></svg
          >Profile</a
        >
      </div>
      <div class="kt-accordion-menu-item">
        <a href="#" class="kt-accordion-menu-link"
          ><svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            class="lucide lucide-settings"
          >
            <path
              d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"
            ></path>
            <circle cx="12" cy="12" r="3"></circle></svg
          >Settings<span
            class="kt-badge kt-badge-sm kt-badge-primary rounded-full ms-auto"
            >New</span
          ></a
        >
      </div>
    </div>
  </div>
  <div class="h-[1000px]"></div>
</div>

Release

Enables the below navigation panel to remain sticky during page scrolling and releases the sticky mode once a specific element is reached.

<div>
  <div data-kt-sticky-wrapper="true">
    <div
      data-kt-sticky="true"
      data-kt-sticky-name="basic"
      data-kt-sticky-top="50px"
      data-kt-sticky-start="auto"
      data-kt-sticky-offset="10px"
      data-kt-sticky-activate="#activate"
      data-kt-sticky-release="#release"
      data-kt-sticky-class="fixed z-10 shadow-md"
      class="w-52 mx-auto p-2 text-sm flex flex-wrap justify-center border border-border rounded-lg py-2"
    >
      <div class="kt-accordion-menu-item">
        <a href="#" class="kt-accordion-menu-link"
          ><svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            class="lucide lucide-badge-check"
          >
            <path
              d="M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z"
            ></path>
            <path d="m9 12 2 2 4-4"></path></svg
          >Home</a
        >
      </div>
      <div class="kt-accordion-menu-item">
        <a href="#" class="kt-accordion-menu-link"
          ><svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            class="lucide lucide-circle-user"
          >
            <circle cx="12" cy="12" r="10"></circle>
            <circle cx="12" cy="10" r="3"></circle>
            <path
              d="M7 20.662V19a2 2 0 0 1 2-2h6a2 2 0 0 1 2 2v1.662"
            ></path></svg
          >Profile</a
        >
      </div>
      <div class="kt-accordion-menu-item">
        <a href="#" class="kt-accordion-menu-link"
          ><svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            class="lucide lucide-settings"
          >
            <path
              d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"
            ></path>
            <circle cx="12" cy="12" r="3"></circle></svg
          >Settings<span
            class="kt-badge kt-badge-sm kt-badge-primary rounded-full ms-auto"
            >New</span
          ></a
        >
      </div>
    </div>
  </div>
  <div class="h-[250px]"></div>
  <div id="activate"></div>
  <div class="h-[2000px]"></div>
  <div id="release"></div>
  <div class="h-[1000px]"></div>
</div>

Component API

Options

These data attributes allow you to set options for the sticky component during auto initialization.

OptionTypeDefaultDescription
data-kt-sticky-namestring-When activated, this name is used to apply a specific data attribute to the body tag. For example, if set to “example”, data-kt-sticky-example="on" is added to the body during activation.
data-kt-sticky-activatestring-Selector ID of the element to detect in the viewport to deactivate sticky mode.
data-kt-sticky-releasestring-Selector ID of the element to detect in the viewport to activate sticky mode.
data-kt-sticky-classstring-A list of classes added to the element upon activation of sticky mode.
data-kt-sticky-topstring-CSS value in pixels to apply for the sticky element’s top position when activated.
data-kt-sticky-startstring-CSS value in pixels to apply for the sticky element’s left position when activated. When set to auto, the value is calculated as the element’s offsetLeft.
data-kt-sticky-endstring-CSS value in pixels to apply for the sticky element’s right position when activated. When set to auto, the value is calculated as the offset from the right of the container or viewport.
data-kt-sticky-zindexenumtrueCSS value in pixels, auto, or an ID selector to calculate the width of the element during activation.
data-kt-sticky-offsetstring5Specifies the z-index property of the sticky element, determining its stack order in the context of the page layout.
data-kt-sticky-reversenumber0Sets the offset distance in pixels that the sticky element should activate from the top of the viewport.
data-kt-sticky-reversebooleanfalseDetermines whether the sticky effect should activate in the opposite direction of the scroll, typically used for elements that should become sticky upon scrolling upwards.

Selectors

This table details the custom classes and data attributes used by the sticky component.

NameDescription
Data Attributes
data-kt-stickyAutomatically initializes Tailwind Sticky instances on page load.
Classes
activeSets a specific sticky item to be open initially when used togather with data-kt-sticky-item selector.
hiddenSets a specific sticky content to be hidden initially when used togather with data-kt-sticky-item selector.

Tailwind Modifiers

Custom modifiers to control the sticky’s style and behavior with Tailwind classes.

NameDescription
kt-sticky-active:*A custom Tailwind variant activated when the sticky mode is activate. It can be applied to the sticky and its children, controlling their appearance and behavior in the open state.

Methods

Use KTSticky component's API methods to programmatically control its behavior.

MethodDescription
new KTSticky(element, options)Creates an object instance of KTSticky class for the given DOM element and configuration options .
show(element)Shows the sticky section that corresponds to the given DOM element .
hide(element)Hides the sticky section that corresponds to the given DOM element .
toggle(element)Toggles the sticky section that corresponds to the given DOM element .
getOption(name)Retrieves the value of a configuration option by name parameter from a KTSticky instance.
getElement()Retrieves the DOM element linked to a specific KTSticky instance.
on(eventName, handler)Allows attaching event listeners to the KTSticky custom events using the eventName and eventId string parameters. These events enable programmatic interaction based on user actions or internal state changes of KTSticky. The function returns string as a unique identifier for the registered listener, allowing you to remove it later if needed.
off(eventName, eventId)Removes an event listener for the eventName and eventId parameters attached with the on method.
dispose()Removes the KTSticky instance from an element, including any associated data stored on the DOM element.
const stickyEl = document.querySelector('#my_sticky');
const sticky = KTSticky.getInstance(stickyEl);
 
sticky.update();
const isActive = sticky.isActive();

To initialize Tailwind Collapse with JavaScript, use data-collapse="false" attribute instead. This prevents automatic initialization on page load.

Utilities

Manage and interact with KTSticky instances using these static methods of KTSticky JavaScript class.

NameDescription
init()Automatically initializes KTSticky object for all elements with the data-kt-sticky attribute on page load.
createInstances()Allows to create KTSticky instances for all elements that have been dynamically added to the DOM but haven't been activated yet.
getInstance(element)Returns the KTSticky object associated with the given DOM element element.
getOrCreateInstance(element)Returns the existing KTSticky object for the provided DOM element element , or creates a new instance if none exists, then returns the same.
// Initialize all stickys
KTSticky.init()
 
// Initialzie pending stickys
KTSticky.createInstances();
 
// Get sticky object
const stickyEl = document.querySelector('#my_sticky');
const sticky = KTSticky.getInstance(stickyEl);

Events

KTSticky custom events allows you to register callback functions(event listeners) that will be invoked automatically whenever specific custom events are triggered within the component.

EventDescription
showTriggered immediately before an sticky section is shown.
shownTriggered immediately after an sticky section is shown.
hideTriggered immediately before an sticky section is hidden.
hidenTriggered immediately after an sticky section is hidden.
toggleTriggered immediately before an sticky section is toggled(shown/hidden).
const stickyEl = document.querySelector('#my_sticky');
const sticky = KTSticky.getInstance(stickyEl);
 
sticky.on('change', (detail) => {
	if (detail.active) {
		console.log('sticky mode is active');
	} else {
		console.log('sticky mode is not active');
	}
});
PreviouseStepperNextSwitch

On This Page

  • Examples
    • Basic Usage
    • Release
  • Component API
    • Options
    • Selectors
    • Tailwind Modifiers
    • Methods
    • Utilities
    • Events