Basic Tabs
A basic Tailwind CSS Tabs example to navigate between different content areas.
Tab 1 content.
Tab 2 content.
Tab 3 content.
<div>
<div class="tabs mb-5" data-tabs="true">
<button class="tab active" data-tab-toggle="#tab_1_1">
Tab 1
</button>
<button class="tab" data-tab-toggle="#tab_1_2">
Tab 2
</button>
<button class="tab" data-tab-toggle="#tab_1_3">
Tab 3
</button>
</div>
<div class="" id="tab_1_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_1_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_1_3">
Tab 3 content.
</div>
</div>
Icons
Use
KeenIcons
to integrate icons into Tailwind CSS Tabs
providing an intuitive navigation experience.
Tab 1 content.
Tab 2 content.
Tab 3 content.
<div>
<div class="tabs mb-5" data-tabs="true">
<button class="tab active" data-tab-toggle="#tab_2_1">
<i class="ki-outline ki-badge">
</i>
Tab 1
</button>
<button class="tab" data-tab-toggle="#tab_2_2">
<i class="ki-outline ki-user-square">
</i>
Tab 2
</button>
<button class="tab" data-tab-toggle="#tab_2_3">
<i class="ki-outline ki-calendar">
</i>
Tab 3
</button>
</div>
<div class="" id="tab_2_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_2_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_2_3">
Tab 3 content.
</div>
</div>
Notification
An example of Tailwind CSS Tabs that includes notification badges, offering a clear and effective way to highlight updates or messages within each tab.
Tab 1 content.
Tab 2 content.
Tab 3 content.
<div>
<div class="tabs mb-5" data-tabs="true">
<button class="tab active" data-tab-toggle="#tab_3_1">
Tab 1
</button>
<button class="tab relative" data-tab-toggle="#tab_3_2">
Tab 2
<span class="badge badge-dot badge-success size-[5px] absolute top-2 right-0 transform translate-y-1/2 translate-x-full">
</span>
</button>
<button class="tab" data-tab-toggle="#tab_3_3">
Tab 3
</button>
</div>
<div class="" id="tab_3_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_3_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_3_3">
Tab 3 content.
</div>
</div>
Transition
Enable smooth transition effects using
Tailwind CSS Transition
classes
to enhance the user experience of Tailwind CSS Tabs.
Tab 1 content.
Tab 2 content.
Tab 3 content.
<div>
<div class="tabs mb-5" data-tabs="true">
<button class="tab active" data-tab-toggle="#tab_4_1">
Tab 1
</button>
<button class="tab" data-tab-toggle="#tab_4_2">
Tab 2
</button>
<button class="tab" data-tab-toggle="#tab_4_3">
Tab 3
</button>
</div>
<div class="transition-opacity duration-700" id="tab_4_1">
Tab 1 content.
</div>
<div class="hidden transition-opacity duration-700" id="tab_4_2">
Tab 2 content.
</div>
<div class="hidden transition-opacity duration-700" id="tab_4_3">
Tab 3 content.
</div>
</div>
Dropdown
Integrate Tailwind CSS Tabs with the
Tailwind CSS Dropdown
component to enable advanced navigation support.
<div>
<div class="tabs mb-5" data-tabs="true">
<a class="tab active" data-tab-toggle="true" href="#tab_5_1">
Tab 1
</a>
<a class="tab" data-tab-toggle="true" href="#tab_5_2">
Tab 2
</a>
<div class="dropdown" data-dropdown="true" data-dropdown-trigger="click">
<button class="dropdown-toggle tab">
Tab 3
<i class="ki-outline ki-down !text-2xs dropdown-open:hidden btn-active:text-primary">
</i>
<i class="ki-outline ki-up !text-2xs hidden dropdown-open:block btn-active:text-primary">
</i>
</button>
<div class="dropdown-content w-full max-w-32" data-dropdown-dismiss="true">
<div class="menu menu-default flex flex-col w-full">
<span class="menu-item" data-tab-toggle="#tab_5_3">
<span class="menu-link">
<span class="menu-icon">
<i class="ki-outline ki-message-programming">
</i>
</span>
<span class="menu-title">
Tab 3
</span>
</span>
</span>
<span class="menu-item" data-tab-toggle="#tab_5_4">
<span class="menu-link">
<span class="menu-icon">
<i class="ki-outline ki-profile-circle">
</i>
</span>
<span class="menu-title">
Tab 4
</span>
</span>
</span>
<span class="menu-item" data-tab-toggle="#tab_5_5">
<span class="menu-link">
<span class="menu-icon">
<i class="ki-outline ki-setting-2">
</i>
</span>
<span class="menu-title">
Tab 5
</span>
</span>
</span>
</div>
</div>
</div>
</div>
<div class="" id="tab_5_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_5_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_5_3">
Tab 3 content.
</div>
<div class="hidden" id="tab_5_3">
Tab 3 content.
</div>
<div class="hidden" id="tab_5_4">
Tab 4 content.
</div>
<div class="hidden" id="tab_5_5">
Tab 5 content.
</div>
</div>
Group
Integrate Tailwind CSS Tabs with the
Tailwind CSS Button Tabs
component to enable a visually grouped navigation style.
<div>
<div class="btn-tabs mb-5" data-tabs="true">
<a class="btn btn-icon active" data-tab-toggle="true" href="#tab_6_1">
<i class="ki-outline ki-badge">
</i>
</a>
<a class="btn btn-icon" data-tab-toggle="true" href="#tab_6_2">
<i class="ki-outline ki-user-square">
</i>
</a>
<a class="btn btn-icon" data-tab-toggle="true" href="#tab_6_3">
<i class="ki-outline ki-calendar">
</i>
</a>
</div>
<div class="" id="tab_6_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_6_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_6_3">
Tab 3 content.
</div>
</div>
Button
Integrate Tailwind CSS Tabs with the
Tailwind CSS Button
component to enable a more focused and cohesive navigation style.
<div>
<div class="flex gap-4 mb-5" data-tabs="true">
<a class="btn btn-primary btn-clear active" data-tab-toggle="true" href="#tab_7_1">
<i class="ki-outline ki-badge">
</i>
Tab 1
</a>
<a class="btn btn-primary btn-clear" data-tab-toggle="true" href="#tab_7_2">
<i class="ki-outline ki-user-square">
</i>
Tab 2
</a>
<a class="btn btn-primary btn-clear" data-tab-toggle="true" href="#tab_7_3">
<i class="ki-outline ki-calendar">
</i>
Tab 3
</a>
</div>
<div class="" id="tab_7_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_7_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_7_3">
Tab 3 content.
</div>
</div>
Source Code
The following source files manage the JavaScript behavior and CSS styles of the Tailwind CSS Tabs component.
Upon building the assets using
Webpack Build Tools
,
these files are compiled and added into the
dist/assets/css/styles.css
and
dist/assets/js/core.bundle.js
bundles, making them available globally across all pages.
File | Description |
---|---|
src/core/components/tabs/tabs.ts
|
The TypeScript source file that manages the JavaScript behavior of the Tailwind CSS Tabs component. |
src/core/plugins/components/tabs.js
|
The Tailwind CSS compatible plugin written in PostCSS for applying CSS styles to the Tailwind CSS Tabs component. |
HTML Markup
The following markup outlines the core structure of the Tailwind CSS Tabs component.
<div class="flex gap-4 border-b border-b-gray-200 mb-5" data-tabs="true">
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary active" data-tab-toggle="#tab_1_1">
Tab 1
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_2">
Tab 2
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_3">
Tab 3
</button>
</div>
<div class="" id="tab_1_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_1_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_1_3">
Tab 3 content.
</div>
Variants
Utilize the following Tailwind CSS Tabs variants to manage the appearance of the tabs items and their child elements.
For more information, refer to the
Tailwind CSS Variant documentation
.
Class | Description |
---|---|
tab-active:
|
A custom Tailwind variant activated when a tab is shown. It can be applied to the tabs toggle element and its children, controlling their appearance and behavior in the open state. |
<div class="flex gap-4 border-b border-b-gray-200 mb-5" data-tabs="true">
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary active" data-tab-toggle="#tab_1_1">
Tab 1
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_2">
Tab 2
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_3">
Tab 3
</button>
</div>
Data Attribute Initialization
Auto-initialize all KTTabs instances on page load by adding the
data-tabs="true"
attribute to your tabs element.
This triggers on the fly initialization by the KTTabs JavaScript component.
<div class="flex gap-4 border-b border-b-gray-200 mb-5" data-tabs="true" id="my_tabs">
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary active" data-tab-toggle="#tab_1_1">
Tab 1
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_2">
Tab 2
</button>
</div>
<div class="" id="tab_1_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_1_2">
Tab 2 content.
</div>
These attributes allow you to set options for the KTTabs component during initialization.
The values you provide as strings are automatically converted to the appropriate
KTTabs Options
.
HTML Attribute | Type | Default | Description |
---|---|---|---|
data-tabs
|
boolean
|
true
|
Automatically initializes Tailwind CSS Tabs instances on page load. This can be disabled for manual initialization via JavaScript. |
data-tabs-hidden-class
|
string
|
"hidden"
|
Tailwind CSS class to use for the hidden state of the tab contents. |
JavaScript Initialization
To initialize a new tabs component, pass the corresponding DOM element
and configuration options to the KTTabs class constructor.
const tabsEl = document.querySelector('#my_tabs');
const options = {
hiddenClass: 'hidden'
};
const tabs = new KTTabs(tabsEl, options);
<div class="flex gap-4 border-b border-b-gray-200 mb-5" data-tabs="false" id="my_tabs">
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary active" data-tab-toggle="#tab_1_1">
Tab 1
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_2">
Tab 2
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_3">
Tab 3
</button>
</div>
<div class="" id="tab_1_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_1_2">
Tab 2 content.
</div>
<div class="hidden" id="tab_1_3">
Tab 3 content.
</div>
To initialize the tabs with JavaScript, use
data-tabs="false"
attribute instead.
This prevents automatic initialization on page load.
Options
This table outlines the configuration options for initialization of Tailwind CSS Tabs instances, using the KTTabs JavaScript component.
Option | Type | Default | Description |
---|---|---|---|
zindex
|
number
|
105
|
Defines the z-index of the tabs, ensuring it layers correctly above or beneath other page elements. |
hiddenClass
|
string
|
"hidden"
|
Tailwind CSS class to use for the hidden state of the tab contents. |
Utilities
Manage and interact with KTTabs instances using these static methods of
KTTabs
class.
Method | Description |
---|---|
init()
|
Automatically initializes KTTabs object for all elements with the
data-tabs="true"
attribute on page load.
|
createInstances()
|
Allows to create KTTabs instances for all elements that have been dynamically added to the DOM but haven't been activated yet. |
getInstance(element)
|
Returns the
KTTabs
object associated with the given DOM element
element
.
|
getOrCreateInstance(element)
|
Returns the existing
KTTabs
object for the provided DOM element
element
, or creates a new instance if none exists, then returns the same.
|
// Initialize all tabss
KTTabs.init()
// Initialzie pending tabss
KTTabs.createInstances();
// Get tabs object
const tabsEl = document.querySelector('#my_tabs');
const tabs = KTTabs.getInstance(tabsEl);
<div class="flex gap-4 border-b border-b-gray-200 mb-5" data-tabs="true" id="my_tabs">
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary active" data-tab-toggle="#tab_1_1">
Tab 1
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_2">
Tab 2
</button>
</div>
<div class="" id="tab_1_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_1_2">
Tab 2 content.
</div>
Methods
Use KTTabs component's API methods to programmatically control its behavior.
Method | Description |
---|---|
new KTTabs(element, options)
|
Creates an object instance of KTTabs class for the given DOM
element
and configuration
options
.
|
show(tabElement)
|
Activates the tab and displays its associated content pane that corresponds to the given DOM
element
. This method makes the specified tab active and reveals its content, while hiding other tabs' content.
|
isShown(tabElement)
|
Checks whether the content associated with the specified tab
element
is currently visible. Returns a boolean value:
true
if the tab's content is displayed, and
false
otherwise.
|
getOption(name)
|
Retrieves the value of a configuration option by
name
parameter from a KTTabs instance.
|
getElement()
|
Retrieves the DOM element linked to a specific KTTabs instance. |
on(eventName, handler)
|
Allows attaching event listeners to the KTTabs custom events using the
eventName
and
eventId
string parameters. These events enable programmatic interaction based on user actions or
internal state changes of KTTabs. 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 KTTabs instance from an element, including any associated data stored on the DOM element. |
const tabsEl = document.querySelector('#my_tabs');
const tabEl = document.querySelector('#my_tab');
const tabs = KTTabs.getInstance(tabsEl);
tabs.show(tabEl);
<div class="flex gap-4 border-b border-b-gray-200 mb-5" data-tabs="true" id="my_tabs">
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary active" data-tab-toggle="#tab_1_1">
Tab 1
</button>
<button class="btn rounded-none border-0 border-b-2 border-b-transparent tab-active:border-b-primary tab-active:text-primary" data-tab-toggle="#tab_1_2">
Tab 2
</button>
</div>
<div class="" id="tab_1_1">
Tab 1 content.
</div>
<div class="hidden" id="tab_1_2">
Tab 2 content.
</div>
Events
KTTabs
custom events allows you to register callback functions(event listeners)
that will be invoked automatically whenever specific custom events are triggered within the component.
Event | Description |
---|---|
show
|
Triggers before a tab is shown, allowing for actions right before the tab's content becomes visible. |
shown
|
Triggers after a tab is fully displayed, indicating that the tab content is now visible to the user. |
const tabsEl = document.querySelector('#my_tabs');
const tabs = KTTabs.getInstance(tabsEl);
tabs.on('show', (detail) => {
detail.cancel = true;
console.log('show action canceled');
});
tabs.on('shown', () => {
console.log('shown event');
});