SnippetUI

Animated Underline Tabs

Tab navigation with underline indicators that smoothly follow active tabs.

Tailwind CSSJavaScript

A sophisticated tab UI with smooth sliding underlines that follow the user as they switch between tabs.

Preview

Account settings content goes here. Manage your profile details.

Use Cases & Features

  • Category switching: Ideal for UIs that switch between different sections within the same screen, such as article categories, tabs on settings screens, etc.
  • Space saving: Provides multiple content in limited screen space and is effective even in mobile environments.
  • Visual current position: The underline animation smoothly follows the current position of the tab, allowing you to intuitively understand where the user is currently looking.

Key points of design and UX (Design & UX)

  • Animation following: Using CSS transition or transform, we implement a movement where the underline slides.This provides nice feedback that you’ve switched tabs.
  • Accessibility considerations: The font color (contrast) is clearly separated between active and inactive tabs to facilitate visual differentiation.

How to customize (Customization Guide)

  • Color change: You can unify the design by simply changing classes such as indigo-600 to blue-500 or emerald-600 to match the brand color.
  • Adding tabs: Add buttons and content panels to the HTML, and adjust the JavaScript logic (width and transform ratios) to match the number of tabs.

Implementation code (Implementation)

<div class="max-w-md mx-auto">
<div class="relative flex border-b border-gray-200 dark:border-zinc-800">
  <button class="tab-btn flex-1 py-3 text-sm font-medium text-indigo-600 dark:text-indigo-400" data-target="tab1">Account</button>
  <button class="tab-btn flex-1 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200" data-target="tab2">Security</button>
  <button class="tab-btn flex-1 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200" data-target="tab3">Notifications</button>
  
  <div id="tab-indicator" class="absolute bottom-0 left-0 h-0.5 bg-indigo-600 dark:bg-indigo-400 transition-all duration-300 ease-out" style="width: 33.33%; transform: translateX(0%);"></div>
</div>
<div class="pt-4">
  <div id="tab1" class="tab-content text-gray-600 dark:text-gray-300 text-sm">Account settings content goes here. Manage your profile details.</div>
  <div id="tab2" class="tab-content hidden text-gray-600 dark:text-gray-300 text-sm">Security settings content goes here. Change your password.</div>
  <div id="tab3" class="tab-content hidden text-gray-600 dark:text-gray-300 text-sm">Notification preferences go here. Manage email alerts.</div>
</div>
</div>
<script>
const btns = document.querySelectorAll('.tab-btn');
const contents = document.querySelectorAll('.tab-content');
const indicator = document.getElementById('tab-indicator');

btns.forEach((btn, i) => {
  btn.addEventListener('click', () => {
    btns.forEach(b => {
      b.classList.remove('text-indigo-600', 'dark:text-indigo-400');
      b.classList.add('text-gray-500', 'dark:text-gray-400');
    });
    btn.classList.remove('text-gray-500', 'dark:text-gray-400');
    btn.classList.add('text-indigo-600', 'dark:text-indigo-400');
    
    indicator.style.transform = `translateX(${i * 100}%)`;
    
    contents.forEach(c => c.classList.add('hidden'));
    document.getElementById(btn.dataset.target).classList.remove('hidden');
  });
});
</script>