Tabs are a common UI pattern used to organize and present content in a structured manner. They allow users to switch between different sections of content within the same space, improving navigation and user experience. This documentation provides examples of implementing tabs using HTML, CSS, and JavaScript.
<!-- HTML -->
<div class="flex gap-l-f padding-cnt tabs">
<button tab-t=".target" tab-d class="button tab">Photos</button>
<button tab-t=".target2" class="button tab">Music</button>
<button tab-t=".target3" class="button tab">Videos</button>
</div>
<div class="target gallery h1 gap-l">
Content-Photos
</div>
<div class="target2 gallery h1 gap-l">
Content-Music
</div>
<div class="target3 gallery h1 gap-l">
Content-Videos
</div>
/* CSS */
<style>
.gallery {
display: none;
text-align: center;
}
.gallery[tab-active] {
display: block;
}
</style>
Tabs Options
Tabs can be customized and configured using various options to suit different design requirements and functionalities. Here are the available options:
tab-t
(Tab Target)tab-t
attribute to the tab element and specify the CSS selector of the target element as its value.tab-d
(Default Tab)tab-d
attribute to the tab element to set it as the default tab.tabs-url
(Tabs URL Pattern)hash
: Activates URL pattern matching using hash fragments.unset
: Disables URL pattern matching.tabs-url
attribute to the tabs container and set its value accordingly.tab-f-t
(Tab Focus Target)tab-f-t
attribute to the tab element and specify the CSS selector of the focus target element.tab-label
(Tab Label)tab-label
attribute to the tab element and specify the desired label as its value.tab-change
(Tab Change Event)change
event.tab-spy
(Tab Scroll Spy)tab-spy
attribute to the tabs container and specify the CSS selector of the scrollable container.scrollspy
(Scrollspy)scrollspy
attribute to the tabs container and specify the CSS selector of the scrollable container.self-spy
(Self Scroll Spy)self-spy
attribute to the tabs container, indicating that the scroll spy functionality should be applied to the container itself.URL Pattern Example
<!-- HTML -->
<div class="flex gap-l-f padding-cnt tabs" tabs-url="hash">
<a href="#photos" tab-t=".photos" tab-d class="button tab">Photos</a>
<a href="#musics" tab-t=".music" class="button tab">Musics</a>
<a href="#videos" tab-t=".video" class="button tab">Videos</a>
</div>
<div class="photos gallery h1 gap-l">
Content-Photos
</div>
<div class="music gallery h1 gap-l">
Content-Music
</div>
<div class="video gallery h1 gap-l">
Content-Videos
</div>
/* CSS */
<style>
.gallery {
display: none;
text-align: center;
}
.gallery[tab-active] {
display: block;
}
</style>
Tab by Scroll to Element Focus
<div class="flex gap-l-f padding-cnt tabs" tab-spy=".focus">
<button tab-f-t=".a" tab-d class="button tab">A</button>
<button tab-f-t=".b" class="button tab">B</button>
<button tab-f-t=".c" class="button tab">C</button>
</div>
<div class="focus border g g-gap " style="height:300px;overflow:auto;">
<div class="a" style="height:400px">
AAAAAA
</div>
<div class="b" style="height:400px;">
BBBBB
</div>
<div class="c" style="height:400px;">
CCCCC
</div>
</div>
Scrollspy
<div class="flex gap-l-f padding-cnt tabs" tab-spy=".focus" scrollspy=".focus">
<button tab-f-t=".a" tab-d class="button tab">A</button>
<button tab-f-t=".b" class="button tab">B</button>
<button tab-f-t=".c" class="button tab">C</button>
</div>
<div class="focus border g g-gap " style="height:300px;overflow:auto;">
<div class="a" style="height:400px">
AAAAAA
</div>
<div class="b" style="height:400px;">
BBBBB
</div>
<div class="c" style="height:400px;">
CCCCC
</div>
</div>
self-spy
<div class="tabs" self-spy="self" tab-spy=".focus" scrollspy=".focus" style="height:100px;overflow:auto;">
<button tab-f-t=".a" tab-d class="button tab">A</button>
<button tab-f-t=".b" class="button tab">B</button>
<button tab-f-t=".c" class="button tab">C</button>
</div>
<div class="parent-target" style="height:100px;overflow:auto;">
<div class="tabs" self-spy=".parent-target" tab-spy=".focus" scrollspy=".focus">
<button tab-f-t=".a" tab-d class="button tab">A</button>
<button tab-f-t=".b" class="button tab">B</button>
<button tab-f-t=".c" class="button tab">C</button>
</div>
</div>
Customize
<!-- HTML -->
<div class="tabs customize">
<div tab-t=".target-el" tab-d class=" tab my-tabs"> Home</div>
<div tab-t=".target-el" class="tab my-tabs">About</div>
</div>
/* CSS */
<style>
.customize .my-tabs {
background: gray;
}
.customize .my-tabs[tab-active] {
background: green;
}
</style>
Event Handling
<!-- HTML -->
<div class="event-test flex gap-l-f padding-cnt tabs">
<button tab-label="photos" tab-d class="button tab">Photos</button>
<button tab-label="musics" class="button tab">Music</button>
<button tab-label="videos" tab-t=".this-target" class="button tab">Videos</button>
</div>
// JavaScript
<script>
document.querySelector('.event-test').addEventListener("change", function(event) {
console.log(event.label);
});
</script>
The Tabs component provides a way to organize content into separate tabs. Below are the variables you can customize to match your desired theme:
Variable | Description |
---|---|
--color-surface-container |
Background color of the tab container. |
--color |
Text color of the tabs. |
--color-primary-container |
Background color of the active tab. |
Variable | Description |
---|---|
--color |
Fill color of the tab icon. |
.tabs .tab {
background: var(--color-surface-container);
color: var(--color);
}
.tabs .tab .icon {
fill: var(--color);
}
.tabs .tab[tab-active] {
background-color: var(--color-primary-container);
color: var(--color-surface);
}
.tabs .tab[tab-active] .icon {
fill: var(--color-surface);
}
Tabs are a versatile UI component that can enhance the usability and organization of your web applications. By following the examples provided in this documentation, you can easily implement tabs in your projects.
This version includes CSS styles for the gallery, ensuring consistent presentation across all examples. Let me know if there's anything else you'd like to add or modify!