Step 1 Create a List Group
To create a list group in Bootstrap 4, we use list-group class and for all the items inside the list group container, we use list-group-item class.
- <div class="container">
- <div class="row">
- <div class="col-4">
- <div class="list-group" role="tablist"> <a href="# " class="list-group-item">Home</a> <a href="# " class="list-group-item">Profile</a> <a href="# " class="list-group-item">Settings</a> <a href="# " class="list-group-item">log Out</a> </div>
- </div>
- </div>
- </div>
Run the application
Step 2 Make the list items toggleable.
In order to make the list items toggleable, we need to add an HTML attribute in all the anchor elements, i.e., data-toggle and set its value to the list.
- <div class="container">
- <div class="row">
- <div class="col-4">
- <div class="list-group" role="tablist"> <a href="# " role="tablist" class="list-group-item active" data-toggle="list">Home</a> <a href="# " role="tablist" class="list-group-item" data-toggle="list">Profile</a> <a href="# " role="tablist" class="list-group-item" data-toggle="list">Settings</a> <a href="# " role="tablist" class="list-group-item" data-toggle="list">log Out</a> </div>
- </div>
- </div>
- </div>
Run the application.
Click on any of the links.
So, our links are working as expected.
Step 3 Contents for each list items.
Let’s say whenever a user clicks on any of the list items, we want to display the contents in the tab, and in order to make this happen, we need two classes.
- Tab-content class
- Tab-pane class
- <div class="col-8">
- <div class="tab-content">
- <div class="tab-pane">Home Panel</div>
- <div class="tab-pane">Profile Panel</div>
- <div class="tab-pane">Settings Panel</div>
- <div class="tab-pane">Logout Panel </div>
- </div>
Run the application
In the output, you will notice that no content is displayed on our Home Section, and this is same for the rest as well.
We need two things to make it work. First, we need to give an id in all the tab-pane sections and in the anchor element, we need to pass the id in the href attribute. So, whenever any of the links is clicked, it will display the content of panel whose id matches with the href value of anchor element.
- <div class="container">
- <div class="row">
- <div class="col-4">
- <div class="list-group" role="tablist"> <a href="#home" role="tablist" class="list-group-item active" data-toggle="list">Home</a> <a href="#profile" role="tablist" class="list-group-item" data-toggle="list">Profile</a> <a href="#settings" role="tablist" class="list-group-item" data-toggle="list">Settings</a> <a href="#logout" role="tablist" class="list-group-item" data-toggle="list">log Out</a> </div>
- </div>
- <div class="col-8">
- <div class="tab-content">
- <div class="tab-pane" id="home">Home Panel</div>
- <div class="tab-pane" id="profile">Profile Panel</div>
- <div class="tab-pane" id="settings">Settings Panel</div>
- <div class="tab-pane" id="logout">Logout Panel </div>
- </div>
- </div>
- </div>
- </div>
Run the application
You will notice that again, nothing is displayed in the home section. But when we click on any other link, you will see the content.
In order to solve this problem, we need to add a bootstrap class “active” in the home content tab pane.
Run the application.
So, in this article, we saw how easy it is to create a tablist. I hope you liked it.