Features
- No dependencies
- Works with mouse, touch, and keyboard
- Six built-in styles to restyle from
- Horizontal or vertical, switchable by screen size
- Animated indicator with an elastic stretch and hover tease
- Panels cross-fade and resize between tabs
- Overflowing tabs scroll with momentum, or wrap onto new rows
- Swipe between tabs on touch
- Screen reader support and right-to-left layouts
- Customizable via CSS variables
- Vue component available
Demos
Tabs in little UI scenes. The first is the default look, and the rest restyle it with a few CSS variables and a little custom CSS, including their own panel transitions:
Town library extends its opening hours
The library will now stay open until 9pm on weekdays, starting next month.
Three ingredient pancakes
Mash a banana, whisk in two eggs, and fry small spoonfuls in butter until golden.
Serve with berries, or a spoonful of yoghurt.
Route to the station
12 minutes on foot, mostly downhill.
Pro plan
- Unlimited projects
- 10 GB of storage
- Email support
Choose your delivery
Next buses
Search
Saved
Alerts
Profile
Basic Usage
A .tab-bar of .tab-bar-buttons, followed by a .tab-contents. Buttons and panels pair up by data-tab, and active marks the starting tab:
Trail Backpack
A light 28 litre pack for day hikes, with a padded back panel and room for a full water bottle on each side.
"Comfortable all day, even when fully packed." Sam, 5 stars
<div class="tab-bar"> <button class="tab-bar-button active" data-tab="overview">Overview</button> <button class="tab-bar-button" data-tab="specs">Specs</button> <button class="tab-bar-button" data-tab="reviews">Reviews</button> </div> <div class="tab-contents"> <div class="tab-content active" data-tab="overview">…</div> <div class="tab-content" data-tab="specs">…</div> <div class="tab-content" data-tab="reviews">…</div> </div>
Styles
Six styles are built in as starting points to restyle from. Add a tab-style-* class to the bar and its panels, or leave it off for the underline:
Underline
The newest articles, most recent first.
The most read articles this week.
Articles you have saved for later.
Classic tabs
The newest articles, most recent first.
The most read articles this week.
Articles you have saved for later.
Slide
Pill
The newest articles, most recent first.
The most read articles this week.
Articles you have saved for later.
Nav
The newest articles, most recent first.
The most read articles this week.
Articles you have saved for later.
Card
The newest articles, most recent first.
The most read articles this week.
Articles you have saved for later.
<div class="tab-bar tab-style-slide">…</div> <div class="tab-contents tab-style-slide">…</div> <button class="tab-bar-button" data-tab="saved"><svg>…</svg>Saved</button> <button class="tab-bar-button" data-tab="saved">Saved<span>Kept for later</span></button>
Panel Transitions
Panels slide and fade by default. Set --tab-panel-distance on the .tab-contents to change how far they slide, or 0 to just fade. Each panel also gets --tab-panel-x, which is -1 before the active tab, 0 when active, and 1 after it, so anything inside it can move on its own:
Slide and fade
Dawn
Sunrise at 06:12
Noon
Sun at its highest at 13:04
Dusk
Sunset at 20:31
Fade only
Dawn
Sunrise at 06:12
Noon
Sun at its highest at 13:04
Dusk
Sunset at 20:31
Background fades, content slides
Dawn
Sunrise at 06:12
Noon
Sun at its highest at 13:04
Dusk
Sunset at 20:31
Panels with their own backgrounds let the page show through as they cross-fade. Set --tab-panel-blend: plus-lighter on the .tab-contents to stop that, as all three cards do. The .tab-contents can't have a background of its own:
.panels-with-backgrounds {
--tab-panel-blend: plus-lighter;
}
.fade-only {
--tab-panel-distance: 0;
}
.background-fades > .tab-content {
transform: none;
}
.background-fades .content {
transform: translateX(calc(var(--tab-panel-x) * 64px));
transition: transform var(--tab-transition-duration) var(--tab-transition-easing);
}
Detached Panels
The bar and its panels don't have to be next to each other. Give the bar an id and point the panels at it with data-tab-bar. Here the date range sits between them, since it applies to every tab:
Most tickets this month were about delivery times.
<div class="tab-bar" id="reports">…</div> <div class="date-range">…</div> <div class="tab-contents" data-tab-bar="reports">…</div>
Nested Tabs
Tabs can go inside another tab's panel. Each bar keeps its own selection and style:
Scrolling & Wrapping
When there are more tabs than fit, the bar scrolls. Drag it, use the wheel, or select a tab to bring it into view. A soft fade marks whichever edge has more tabs:
Add data-tab-wrap to wrap onto new rows instead. The indicator follows the active tab between rows:
<div class="tab-bar tab-style-slide">…</div> <div class="tab-bar tab-style-slide" data-tab-wrap>…</div>
Vertical Tabs
Add data-tab-vertical to run the bar down the side, inside a flex container with its panels. Use a media query as the value to only do this when it matches, so these switch to horizontal on smaller screens:
Give the bar a max-height and it scrolls vertically, with the same dragging, momentum, and edge fades:
<div style="display: flex; flex-wrap: wrap"> <div class="tab-bar" data-tab-vertical="(min-width: 700px)">…</div> <div class="tab-contents">…</div> </div>
Swiping
On a touch screen, swipe left or right across the panels to move between tabs. Swiping doesn't do anything with a mouse on desktop, so try this one on your phone. Add data-tab-no-swipe to turn it off, for example when a panel has its own carousel:
<div class="tab-bar" data-tab-no-swipe>…</div>
Events
The bar fires a tab-changed event when the active tab changes, with the new tab's data-tab in event.detail. Panels are optional, so a bar on its own works as a filter. Here there are no panels, the event just hides the messages that don't match:
bar.addEventListener("tab-changed", e => {
for (const message of messages) {
message.inert = !message.dataset.filter.includes(e.detail)
}
})
Programmatic Control
Switch tabs from your own code by calling .click() on a button. These Back and Next buttons drive the bar:
bar.querySelector('[data-tab="payment"]').click()
Adding & Removing Bars
Use FluidTabs.init() to set up bars added after the page loads, and bar.destroy() to clean one up before removing it:
FluidTabs.init() // whole document FluidTabs.init(sectionEl) // within an element bar.destroy()
Customization Variables
Fine-tune the feel with CSS variables, globally or per bar. Colours, radii, and spacing are plain CSS, as the scenes in Demos show:
Slow and springy
No elastic stretch
No hover tease
Bouncy easing
.tab-bar {
--tab-transition-duration: .25s; /* Duration of every transition */
--tab-transition-easing: cubic-bezier(.4, 0, .2, 1); /* Easing of every transition */
--tab-slide-lag: .333; /* Elastic stretch, as a fraction of the duration */
--tab-tease-x: 10px; /* How far the indicator stretches on hover */
--tab-fade: 60px; /* Width of the scroll edge fade */
}