tailwind css tabs tutorial example step by step from scratch

In this tailwind css tutorials we see how to create tabs by using alpine js .we will create tabs ui and use alpine js for tabs working . in this tutorial i am not use any hover or any transition , delay property to more complicated code.


Tailwind CSS Tabs Tutorial Example Step By Step From Scratch


Step 1: Set Up Environment

Step 2: Create Tab ui with Tailwind CSS

Step 3: Working on Tabs using Alpine js

Step 4 : Change Color with Tabs


Step 1: Set Up Environment

In this section we will use Tailwind 2V CDN and Apline js 2v CDN .it is easy to test your components CDN mode . Note in alpine you need put put defer

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title> Tailwind CSS Tabs Tutorial Example Step By Step From Scratch </title>
        <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
        <script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>
    </head>

    <body>
        
    </body>

</html>

Step 2: Create Tab ui with Tailwind CSS

1) You need to create container main div and set mx-auto it work like bootstrap container

2) Next you need to create ul and li ,a tag you can also use button if you like .

3) Give ul to flex class it will set all under li in one row then give justify-center it is also flex it take all elements in middle . use space-x-2 class it is very use full class you don't need to put mr-2 each elements just simply put parents class space

4) Set the a tag to tabs give px-4 and py-2 and give color which you like remember inline-block add class

5) Now create three div put some content and give border style

<div class="container mx-auto">
            <ul class="flex justify-center space-x-2">
                <li>
                    <a href="#" class="inline-block px-4 py-2 bg-purple-500 text-white">Tab 1</a>
                </li>
                <li> 
                    <a href="#" class="inline-block px-4 py-2 bg-purple-500 text-white">Tab 2</a> 
                </li>
                <li>
                    <a href="#"class="inline-block px-4 py-2 bg-purple-500 text-white">Tab 3</a>
                </li>
            </ul>
            <div class="mt-6 bg-white border p-3">
                <div> Tab 1 Content show ... </div>
                <div>Tab 2 Content show ...</div>
                <div>Tab 3 Content show ...</div>
            </div>
        </div>


tailwind tab1

tailwind tab1


Step 3: Working on Tabs using Alpine js

Note : if you don't alpine js or not much know then first read this article

👉 Alpine js x-data Tutorial by Example

👉 Alpine js x-show Example

First you need to start with x-data and create object and give value activeTab: 1 .Then you need give a tag x-on:click it is event like JavaScript events then give each one click after value 1,2,3. next you need to give down three div give x-show . The x-show is like display:none; it pass expression true of false if value is not match then it will pass false.

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title> Tailwind CSS Tabs Tutorial Example Step By Step From Scratch </title>
        <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
        <script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>
    </head>

    <body>
        <div x-data="{ activeTab:1 }" class="container mx-auto">
            <ul class="flex justify-center space-x-2 text-white">
                <li>
                    <a href="#" x-on:click="activeTab = 1" class="inline-block px-4 py-2 bg-purple-500">Tab 1</a>
                </li>
                <li> 
                    <a href="#" x-on:click="activeTab = 2" class="inline-block px-4 py-2 bg-purple-500">Tab 2</a> 
                </li>
                <li> 
                    <a href="#" x-on:click="activeTab = 3" class="inline-block px-4 py-2 bg-purple-500">Tab 3</a> 
                </li>
            </ul>
            <div class="mt-6 bg-white border p-3">
                <div x-show="activeTab === 1"> Tab 1 Content show ... </div>
                <div x-show="activeTab === 2">Tab 2 Content show ...</div>
                <div x-show="activeTab === 3">Tab 3 Content show ....</div>
            </div>
        </div>
    </body>

</html>


Step 4 : Change Color with Tabs

Change with color with tab you need to create two more class and give tailwind class .Now you need to bind x-bind:class or :class and give condition the bind class now all we set.

<div x-data="{ 
            activeTab:1,
            activeClass: 'inline-block px-4 py-2 bg-red-500',
            inactiveClass : 'inline-block px-4 py-2 bg-purple-500'
         }" class="container mx-auto mt-20">
            <ul class="flex justify-center space-x-2 text-white">
                <li>
                    <a href="#" x-on:click="activeTab = 1" :class="activeTab === 1 ? activeClass : inactiveClass">Tab 1</a>
                </li>
                <li> 
                    <a href="#" x-on:click="activeTab = 2" :class="activeTab === 2 ? activeClass : inactiveClass">Tab 2</a> 
                </li>
                <li> 
                    <a href="#" x-on:click="activeTab = 3" :class="activeTab === 3 ? activeClass : inactiveClass">Tab 3</a> 
                </li>
            </ul>
            <div class="mt-6 bg-white border p-3">
                <div x-show="activeTab === 1"> Tab 1 Content show...  </div>
                <div x-show="activeTab === 2">Tab 2 Content show ...</div>
                <div x-show="activeTab === 3">Tab 3 Content show ...</div>
            </div>
        </div>
    </body>


tailwind tab2

tailwind tab2


Read Also

Tailwind CSS 3 FAQ Accordion UI Example

Tailwind CSS 3 Alert Message Example

Tailwind CSS 3 Avatars Example

Tailwind CSS 3 Badges Example

Tailwind CSS 3 Breadcrumb Example

Tailwind CSS Gradient Button Example

Tailwind CSS 3D Button Example

Tailwind CSS Loading Button Example

Tailwind CSS v3 Cards Examples

Tailwind CSS Checkbox Form Examples

Tailwind CSS Dropdowns (Menu) on Hover Example

Tailwind CSS Multiselect Dropdown Example

How to use dark mode toggle switch in Tailwind CSS 3

How to use @apply directive in Tailwind CSS

Tailwind CSS sticky header & fixed navbar example

Tailwind CSS Navbar UI Example

Tailwind CSS 3 Login Page UI Example

Tailwind CSS Login Modal Example

Tailwind CSS Search Examples

Tailwind CSS 3 Overlay Image Example

Tailwind CSS Thank You Page Example

Tailwind CSS Timeline UI Example

Tailwind CSS Responsive Footer Section Example

Tailwind CSS Background Image Header Example