Bootstrap Tabs with Example

Bootstrap Tabs with Example
This article is going to explain how to create a Bootstrap tab with a small example.

Step 1: Add CDN

To use Bootstrap or jQuery you need to add a CDN link or you can download and use the package. The following is a list of CDN links to use the Bootstrap styles and scripts.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Step 2: Tab Menu

You must use an unordered list element to create the tab menu. In the following code, you will find an unordered list with three menu list items. nav nav-tabs classes are added to the unordered list to convert the list item to the tab menu.
The first menu mostly always be active, so in the first list item, the active class is included. Added data-toggle="tab" to make the tab togglable. The href attribute is assigned with tab content’s div id.

<ul class="nav nav-tabs">
  <li class="active"><a data-toggle="tab" href="#menu1">Menu 1</a></li>
  <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
  <li><a data-toggle="tab" href="#menu3">Menu 3</a></li>
</ul>

Step 3: Tab Content

The following are the tab content elements. Added tab-content class to create tab content. Here you can see three menu contents with the id menu1, menu2, and menu3. Added tab-panel class in each menu to make it toggleable. Also, active class is included in menu1 content to the active first menu.

<div class="tab-content">
  <div id="menu1" class="tab-pane active">
    <h3>Menu 1</h3>
    <p>Menu 1 tab content</p>
  </div>

  <div id="menu2" class="tab-pane">
    <h3>Menu 2</h3>
    <p>Menu 2 tab content</p>
  </div>

  <div id="menu3" class="tab-pane">
    <h3>Menu 3</h3>
    <p>Menu 3 tab content</p>
  </div>
</div>

Below is the complete coding for creating the Bootstrap tab.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Menu Example</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h4>Bootstrap Sample Tabs</h4>

  <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#menu1">Menu 1</a></li>
    <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
    <li><a data-toggle="tab" href="#menu3">Menu 3</a></li>
  </ul>

  <div class="tab-content">
    <div id="menu1" class="tab-pane active">
      <h3>Menu 1</h3>
      <p>Menu 1 tab content</p>
    </div>

    <div id="menu2" class="tab-pane">
      <h3>Menu 2</h3>
      <p>Menu 2 tab content</p>
    </div>

    <div id="menu3" class="tab-pane">
      <h3>Menu 3</h3>
      <p>Menu 3 tab content</p>
    </div>
  </div>
</div>

</body>
</html>

The following is an output of the above code.

Bootstrap Tabs with Example


I hope this article is helpful to you. Keep coding.

Comments

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

Exploring EventCallback in Blazor: Building Interactive Components

A Step-by-Step Guide to Implementing Identity in ASP.NET Core MVC