Tailwind Scrollable makes it easy to add vertically or horizontally scrollable sections with custom scrollbar styles, perfect for long lists, data tables, or extended content.
To implement a custom scrollable content with a fixed height, simply utilize the kt-scrollable
custom class. This class is designed to create a improved scrollable area within your HTML document.
Below example demonstrates horizontally scrollable content.
Below example demonstrates a scrollable content with bith horizontal and vertical scrollbars.
Use data-kt-scrollable="true"
to auto initialize the Tailwind KTScrollable component that dynamically calculates the height of the scrollable content by subtracting the the total heights of specific elements defined via data-kt-scrollable-dependencies="#header, #footer"
the total spacings of specific elements defined via data-kt-scrollable-wrappers="#header, #footer"
within the HTML document.
Apply the kt-scrollable-y
class to the modal body element to enable vertical scrolling within the scrollable body.
Customize the scrollbar thumb color to match your design requirements using the CSS variable for default and dark demos using [--tw-scrollbar-thumb-color:red]
and [--tw-scrollbar-thumb-color-dark:yellow]
classes.
These data attributes allow you to set options for the scrollable component during auto initialization.
Option | Type | Default | Description |
---|---|---|---|
data-kt-scrollable-save | Enables saving the last scroll position in browser storage. | ||
data-kt-reparent-target | Specifies the comma-separated IDs of elements whose heights are calculated and subtracted from the window offset height to determine scrollable height. | ||
data-kt-scrollable-wrappers | Specifies the comma-separated IDs of elements whose spacings(margin, padding, border widths) are calculated and subtracted from the window offset height to determine scrollable height. | ||
data-kt-scrollable-offset | Specifies the offset CSS value in pixel to subtract from the calculated height. |
This table details the custom classes and data attributes used by the scrollable component.
Name | Description |
---|---|
Data Attributes | |
data-kt-scrollable="true" | Automatically initializes KTScrollable instances on page load. |
Classes | |
kt-scrollable | Adds both horizontal and vertical scrolling to the content. |
kt-scrollable-x | Adds horizontal scrolling to the content. |
kt-scrollable-y | Adds vertical scrolling to the content. |
kt-scrollable-auto | Automatically adds scrolling based on content overflow. |
kt-scrollable-x-auto | Automatically adds horizontal scrolling based on content overflow. |
kt-scrollable-y-auto | Automatically adds vertical scrolling based on content overflow. |
kt-scrollable-hover | Adds scrollbars on hover. |
kt-scrollable-x-hover | Adds horizontal scrollbars on hover. |
kt-scrollable-y-hover | Adds vertical scrollbars on hover. |
Use KTScrollable component's API methods to programmatically control its behavior.
Method | Description |
---|---|
update() | Updates the calculated height. |
getHeight() | Returnes calculated height. |
getOption(name) | Retrieves the value of a configuration option by |
getElement() | Retrieves the DOM element linked to a specific KTScrollable instance. |
on(eventName, handler) | Allows attaching event listeners to the KTScrollable custom events using the |
off(eventName, eventId) | Removes an event listener for the |
dispose() | Removes the KTScrollable instance from an element, including any associated data stored on the DOM element. |
const scrollableEl = document.querySelector('#my_scrollable');
const scrollable = KTScrollable.getInstance(scrollableEl);
const height = scrollable.getHeight();
scrollable.update();
Manage and interact with KTScrollable instances using these static methods of KTScrollable
JavaScript class.
Method | Description |
---|---|
init() | Automatically initializes |
createInstances() | Allows to create KTScrollable instances for all elements that have been dynamically added to the DOM but haven't been activated yet. |
getInstance(element) | Returns the |
getOrCreateInstance(element) | Returns the existing |
// Initialize all scrollables
KTScrollable.init()
// Initialzie pending scrollables
KTScrollable.createInstances();
// Get scrollable object
const scrollableEl = document.querySelector('#my_scrollable');
const scrollable = KTScrollable.getInstance(scrollableEl);