fluid-tabs

A lightweight, zero-dependency tabs library with a smooth animated indicator, drag/wheel/swipe scrolling, and multiple visual styles.

Just add the tab-bar class to a row of buttons.

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:

Display nameEwan Howell
Two-factor loginOn
Last password change3 months ago
Active sessions2
news.example.com

Town library extends its opening hours

The library will now stay open until 9pm on weekdays, starting next month.

recipes.example.com

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.

maps.example.com

Route to the station

12 minutes on foot, mostly downhill.

Pro plan

$12/mo

Billed every month

$9/mo

Billed yearly at $108

  • Unlimited projects
  • 10 GB of storage
  • Email support

Choose your delivery

Arrives byTuesday
Delivered to14 Mill Lane
ArrivesTomorrow by 1pm
Delivered to14 Mill Lane
Order within3 hours
ReadyToday after 4pm

Next buses

12 to Harbour4 min
7 to Station9 min
12 to Harbour19 min
31 to Hospital26 min

Search

Elm StreetStop
Market SquareStop

Saved

HomeElm Street
WorkStation

Alerts

12 diverted until Friday
Market Square stop closed
Your pass renews on 3 June

Profile

E
NameEwan Howell
PassMonthly
1KAT48,200
2DAN41,750
3MEG38,900
4ROB22,100
1MEG96,450
2KAT88,300
3LEE74,600
4DAN61,250
1LEE312,800
2MEG298,150
3TOM254,900
4KAT247,300
BristolLight rain
14°
15:00Rain, 14°
16:00Cloudy, 13°
17:00Clear, 12°
TuesdaySunny, 17°
WednesdayCloudy, 15°
ThursdayRain, 12°
FridayWindy, 13°
Wind12 mph
Sunset20:31
CPU41%
Memory6.2 GB
Uptime12 days
Ping18 ms
Down84 Mb/s
Up19 Mb/s
12:04Backup finished
12:31Disk check passed
13:02Update installed

Milk, eggs, bin bags

Light bulbs (the small ones)

Fix the back gate

Book the van

Ring the dentist

Return the drill to Dave

Paint the shed green?

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.

Volume28 litres
Weight780 g
MaterialRecycled nylon
Warranty5 years

"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

The newest articles, most recent first.

The most read articles this week.

Articles you have saved for later.

Pill

The newest articles, most recent first.

The most read articles this week.

Articles you have saved for later.

Nav

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:

1 to 30 JuneUpdated 5 minutes ago
12.4kVisitors
38.1kPage views
41%Bounce rate
318Orders
$9,240Revenue
14Open tickets
2hFirst reply
96%Satisfaction
6Refunds

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:

PlanPro

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:

What the product does and who it is for.
Sizes, weights, and materials.
Which devices and versions are supported.
Plans and what each one includes.
Answers to the most common questions.
How to get help from the team.
What customers are saying.
Installers and past versions.
Everything new in each release.
Guides and the full reference.
Forums, events, and meetups.
What is coming next.
Connect it to the tools you already use.
How your data is kept safe.
Keyboard and screen reader support.
Get in touch with the sales team.

Add data-tab-wrap to wrap onto new rows instead. The indicator follows the active tab between rows:

What the product does and who it is for.
Sizes, weights, and materials.
Which devices and versions are supported.
Plans and what each one includes.
Answers to the most common questions.
How to get help from the team.
What customers are saying.
Installers and past versions.
Everything new in each release.
Guides and the full reference.
Forums, events, and meetups.
What is coming next.
Connect it to the tools you already use.
How your data is kept safe.
Keyboard and screen reader support.
<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:

LanguageEnglish
Time zoneLondon
ProfilePublic
Search enginesAllowed
ActivityHidden
PlanPro
LanguageEnglish
Time zoneLondon
ProfilePublic
Search enginesAllowed
ActivityHidden
PlanPro
LanguageEnglish
Time zoneLondon
ProfilePublic
Search enginesAllowed
ActivityHidden
PlanPro
LanguageEnglish
Time zoneLondon
ProfilePublic
Search enginesAllowed
ActivityHidden
PlanPro
LanguageEnglish
Time zoneLondon
ProfilePublic
Search enginesAllowed
ActivityHidden
PlanPro

Give the bar a max-height and it scrolls vertically, with the same dragging, momentum, and edge fades:

What the product does and who it is for.
Sizes, weights, and materials.
Plans and what each one includes.
Answers to the most common questions.
How to get help from the team.
What customers are saying.
Installers and past versions.
Everything new in each release.
Forums, events, and meetups.
Get in touch with the sales team.
<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:

128 photos
14 videos
36 files
<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:

Nina Design review notes09:41
Tom Lunch on Friday?Yesterday
Sam Flights are bookedTuesday
Newsletter This week's top storiesTuesday
Billing Your invoice is readyMonday
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:

Trail Backpack$89
Water bottle$18
Standard, 3 to 5 daysFree
Total to pay$107
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 */
}