Bootstrap For Beginners - Part Five (Bootstrap Tables)

I have started an article series on Bootstrap and published four articles so far. Read the previous four parts here.
Introduction

In this article we will learn about Bootstrap Tables. Using Bootstrap classes we can improve the appearance of the table. In this article we will create different tables with different layouts.

Supported Table Elements

The following table elements are used with Bootstrap:
  • <table>: It is used for wrapping element for displaying data in a tabular format
  • <thead>: It is used for container element for table header rows (<tr>) to label table columns.
  • <tbody>: It is used for container element for table rows (<tr>) in the body of the table.
  • <tr>: It is used for container element for a set of table cells (<td> or <th>) that appears on a single row.
  • <th>: Special table cell for column (or row) labels. it must be used within a <thead>
  • <td>: It is used for default table cell.
  • <caption>: It is used for description or summary of what the table holds.
Bootstrap Basic Table

We can create a basic Bootstrap table with basic styling that has a small cell padding and only horizontal dividers by adding Bootstrap class ".table" to the <table> element.

Example 1 : Bootstrap Basic Table using .table class.

In this example we will create a simple table with Bootstrap class and using html table elements we will create a employee table with Fields Sr. No., Employee Name, Email, City by writing the following code.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part5</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10.   
  11. <body>  
  12.     <div class="container">  
  13.         <h1>Bootstrap Basic Table</h1>  
  14.         <!--Bootstrap Basic Table using .table class-->  
  15.         <table class="table">  
  16.             <thead>  
  17.                 <tr>  
  18.                     <th>Sr.No.</th>  
  19.                     <th>Emolpyee Name</th>  
  20.                     <th>Email</th>  
  21.                     <th>City</th>  
  22.                 </tr>  
  23.             </thead>  
  24.             <tbody>  
  25.                 <tr>  
  26.                     <td>1</td>  
  27.                     <td>Shaili Dashora</td>  
  28.                     <td>[email protected] </td>  
  29.                     <td>Chittorgarh</td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td>2</td>  
  33.                     <td>Sourabh Somani</td>  
  34.                     <td>[email protected] </td>  
  35.                     <td>Banglore</td>  
  36.                 </tr>  
  37.                 <tr>  
  38.                     <td>3</td>  
  39.                     <td>Shobhna Singvi</td>  
  40.                     <td>[email protected]</td>  
  41.                     <td>Mumbai</td>  
  42.                 </tr>  
  43.             </tbody>  
  44.         </table>  
  45.     </div>  
  46.     <script src="js/jquery-2.1.4.min.js"></script>  
  47.     <script src="js/bootstrap.min.js"></script>  
  48. </body>  
  49.   
  50. </html>  
Output: Basic Table Layout



Table with Striped Rows

By using .table-striped class with .table base class, we can get zebra-stripes on rows to a table within <tbody>.

Example 2 : Table with Striped Rows Using .table-striped class

In this example we will create table same as Example1. In this we will create table with Striped Rows by writing the following code.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part5</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10.   
  11. <body>  
  12.     <div class="container">  
  13.         <h1>Bootstrap Table With Striped Rows</h1>  
  14.         <!--Bootstrap Table using .table-striped class-->  
  15.         <table class="table table-striped">  
  16.             <thead>  
  17.                 <tr>  
  18.                     <th>Sr.No.</th>  
  19.                     <th>Emolpyee Name</th>  
  20.                     <th>Email</th>  
  21.                     <th>City</th>  
  22.                 </tr>  
  23.             </thead>  
  24.             <tbody>  
  25.                 <tr>  
  26.                     <td>1</td>  
  27.                     <td>Shaili Dashora</td>  
  28.                     <td>[email protected] </td>  
  29.                     <td>Chittorgarh</td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td>2</td>  
  33.                     <td>Sourabh Somani</td>  
  34.                     <td>[email protected] </td>  
  35.                     <td>Banglore</td>  
  36.                 </tr>  
  37.                 <tr>  
  38.                     <td>3</td>  
  39.                     <td>Shobhna Singvi</td>  
  40.                     <td>[email protected]</td>  
  41.                     <td>Mumbai</td>  
  42.                 </tr>  
  43.             </tbody>  
  44.         </table>  
  45.     </div>  
  46.     <script src="js/jquery-2.1.4.min.js"></script>  
  47.     <script src="js/bootstrap.min.js"></script>  
  48. </body>  
  49.   
  50. </html>  
Output: Striped Table Layout



Bordered Table

The .table-bordered class adds borders on all sides of the table and cells.

Example 3: Table with Borders on All Sides Using .table-bordered class

In this example we will creat a table same as Example 1, 2. In this we will create a table with borders on all sides of the table and cells by using .table-bordered class with .table base class by writing the following code.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part5</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10.   
  11. <body>  
  12.     <div class="container">  
  13.         <h1>Bootstrap Bordered Table</h1>  
  14.         <!--Bootstrap Table using .table-bordered class-->  
  15.         <table class="table table-bordered">  
  16.             <thead>  
  17.                 <tr>  
  18.                     <th>Sr.No.</th>  
  19.                     <th>Emolpyee Name</th>  
  20.                     <th>Email</th>  
  21.                     <th>City</th>  
  22.                 </tr>  
  23.             </thead>  
  24.             <tbody>  
  25.                 <tr>  
  26.                     <td>1</td>  
  27.                     <td>Shaili Dashora</td>  
  28.                     <td>[email protected] </td>  
  29.                     <td>Chittorgarh</td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td>2</td>  
  33.                     <td>Sourabh Somani</td>  
  34.                     <td>[email protected] </td>  
  35.                     <td>Banglore</td>  
  36.                 </tr>  
  37.                 <tr>  
  38.                     <td>3</td>  
  39.                     <td>Shobhna Singvi</td>  
  40.                     <td>[email protected]</td>  
  41.                     <td>Mumbai</td>  
  42.                 </tr>  
  43.             </tbody>  
  44.         </table>  
  45.     </div>  
  46.     <script src="js/jquery-2.1.4.min.js"></script>  
  47.     <script src="js/bootstrap.min.js"></script>  
  48. </body>  
  49.   
  50. </html>  
Output: Bordered Table Layout



Hover Rows

By using Bootstrap's class .table-hover with the .table base class, we can enable a hover state on table rows within a <tbody> element.

Example 4 : Hover Rows on table Using .table-hover class

In this example we will create atable same as Example 1, 2, 3. For this we will create table by using .table-hover class, a light gray background will be added to rows when we mouse over, by writing the following code.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part5</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10.   
  11. <body>  
  12.     <div class="container">  
  13.         <h1>Bootstrap Table With Hover Rows State</h1>  
  14.         <!--Bootstrap Table using .table-hover class-->  
  15.         <table class="table table-hover">  
  16.             <thead>  
  17.                 <tr>  
  18.                     <th>Sr.No.</th>  
  19.                     <th>Emolpyee Name</th>  
  20.                     <th>Email</th>  
  21.                     <th>City</th>  
  22.                 </tr>  
  23.             </thead>  
  24.             <tbody>  
  25.                 <tr>  
  26.                     <td>1</td>  
  27.                     <td>Shaili Dashora</td>  
  28.                     <td>[email protected] </td>  
  29.                     <td>Chittorgarh</td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td>2</td>  
  33.                     <td>Sourabh Somani</td>  
  34.                     <td>[email protected] </td>  
  35.                     <td>Banglore</td>  
  36.                 </tr>  
  37.                 <tr>  
  38.                     <td>3</td>  
  39.                     <td>Shobhna Singvi</td>  
  40.                     <td>[email protected]</td>  
  41.                     <td>Mumbai</td>  
  42.                 </tr>  
  43.             </tbody>  
  44.         </table>  
  45.     </div>  
  46.     <script src="js/jquery-2.1.4.min.js"></script>  
  47.     <script src="js/bootstrap.min.js"></script>  
  48. </body>  
  49.   
  50. </html>  
Output: Hover Table Layout


Condensed Table

By using the .table-condensed class we can make a table more compact by cutting cell padding in half.

Example 5 : Condensed or Compact Table Using .table-condensed class

In this example we will create table same as Example 1, 2, 3, 4. For this we will create table by using .table-condensed class, with .table base class by writing the following code.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part5</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10.   
  11. <body>  
  12.     <div class="container">  
  13.         <h1>Bootstrap Condensed Table</h1>  
  14.         <!--Bootstrap Table using .table-condensed class-->  
  15.         <table class="table table-condensed">  
  16.             <thead>  
  17.                 <tr>  
  18.                     <th>Sr.No.</th>  
  19.                     <th>Emolpyee Name</th>  
  20.                     <th>Email</th>  
  21.                     <th>City</th>  
  22.                 </tr>  
  23.             </thead>  
  24.             <tbody>  
  25.                 <tr>  
  26.                     <td>1</td>  
  27.                     <td>Shaili Dashora</td>  
  28.                     <td>[email protected] </td>  
  29.                     <td>Chittorgarh</td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td>2</td>  
  33.                     <td>Sourabh Somani</td>  
  34.                     <td>[email protected] </td>  
  35.                     <td>Banglore</td>  
  36.                 </tr>  
  37.                 <tr>  
  38.                     <td>3</td>  
  39.                     <td>Shobhna Singvi</td>  
  40.                     <td>[email protected]</td>  
  41.                     <td>Mumbai</td>  
  42.                 </tr>  
  43.             </tbody>  
  44.         </table>  
  45.     </div>  
  46.     <script src="js/jquery-2.1.4.min.js"></script>  
  47.     <script src="js/bootstrap.min.js"></script>  
  48. </body>  
  49.   
  50. </html>  
Output: Condensed Table Layout



Contextual Classes

Some Contextual classes can be used to change the background color of table rows (<tr>) or table cells (<td>). The Contextual classes are following,These classes can be applied to <tr>, <td> or <th>.
  • .active: It is used to apply the hover color to the table row or table cell
  • .info: It is used to indicates a informative change or action
  • .success: It is used to indicates a successful or positive action
  • .danger: It is used to indicates a dangerous or negative action
  • .warning: It is used to indicates a warning
Example 6: Use Contextual Classes

In this example we will create table using .table base class, with above Contextual Classes on <tr> element for Coloring rows background like warning, success, danger, etc. by writing the following code.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part5</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10.   
  11. <body>  
  12.     <div class="container">  
  13.         <h1>Bootstrap Contextual Classes For Background Color</h1>  
  14.         <!--Bootstrap Table using .table class-->  
  15.         <table class="table">  
  16.             <thead>  
  17.                 <tr>  
  18.                     <th>Sr.No.</th>  
  19.                     <th>Emolpyee Name</th>  
  20.                     <th>Salary</th>  
  21.                     <th>City</th>  
  22.                 </tr>  
  23.             </thead>  
  24.             <tbody>  
  25.                 <tr class="active">  
  26.                     <td>1</td>  
  27.                     <td>Shaili Dashora</td>  
  28.                     <td>50,000</td>  
  29.                     <td>Banglore</td>  
  30.                 </tr>  
  31.                 <tr class="success">  
  32.                     <td>2</td>  
  33.                     <td>Sourabh Somani</td>  
  34.                     <td>1,20,000</td>  
  35.                     <td>Dehli</td>  
  36.                 </tr>  
  37.                 <tr class="info">  
  38.                     <td>3</td>  
  39.                     <td>Milan</td>  
  40.                     <td>40,000</td>  
  41.                     <td>Udaipur</td>  
  42.                 </tr>  
  43.                 <tr class="danger">  
  44.                     <td>4</td>  
  45.                     <td>KumKum</td>  
  46.                     <td>30,000</td>  
  47.                     <td>Banglore</td>  
  48.                 </tr>  
  49.                 <tr class="warning">  
  50.                     <td>5</td>  
  51.                     <td>Bhumika</td>  
  52.                     <td>20,000</td>  
  53.                     <td>Udaipur</td>  
  54.                 </tr>  
  55.             </tbody>  
  56.         </table>  
  57.     </div>  
  58.     <script src="js/jquery-2.1.4.min.js"></script>  
  59.     <script src="js/bootstrap.min.js"></script>  
  60. </body>  
  61.   
  62. </html>  
Output: Contextual Table Layout



Creating Responsive Tables with Bootstrap

In Bootstrap 3 we can create a responsive table by using .table-responsive class. The table will scroll horizontally on small devices (screen width under 768px). When viewing on larger than 768px wide, we will not see any difference in tables.
Example 7 : Creating Responsive Table

In this example to mak the table responsive we will place the table inside a <div> element and then we will apply the class .table-responsive on it by using the following code.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part5</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10.   
  11. <body>  
  12.     <div class="container">  
  13.         <h1>Bootstrap Responsive Table</h1>  
  14.         <!-- Div using .table-responsive class-->  
  15.         <div class="table-responsive">  
  16.             <!--Bootstrap Table using .table class-->  
  17.             <table class="table">  
  18.                 <thead>  
  19.                     <tr>  
  20.                         <th>Sr.No.</th>  
  21.                         <th>Emolpyee Name</th>  
  22.                         <th>Salary</th>  
  23.                         <th>City</th>  
  24.                     </tr>  
  25.                 </thead>  
  26.                 <tbody>  
  27.                     <tr>  
  28.                         <td>1</td>  
  29.                         <td>Shaili Dashora</td>  
  30.                         <td>50,000</td>  
  31.                         <td>Banglore</td>  
  32.                     </tr>  
  33.                     <tr>  
  34.                         <td>2</td>  
  35.                         <td>Sourabh Somani</td>  
  36.                         <td>1,20,000</td>  
  37.                         <td>Dehli</td>  
  38.                     </tr>  
  39.                     <tr>  
  40.                         <td>3</td>  
  41.                         <td>Milan</td>  
  42.                         <td>40,000</td>  
  43.                         <td>Udaipur</td>  
  44.                     </tr>  
  45.                     <tr>  
  46.                         <td>4</td>  
  47.                         <td>KumKum</td>  
  48.                         <td>30,000</td>  
  49.                         <td>Banglore</td>  
  50.                     </tr>  
  51.                     <tr>  
  52.                         <td>5</td>  
  53.                         <td>Bhumika</td>  
  54.                         <td>20,000</td>  
  55.                         <td>Udaipur</td>  
  56.                     </tr>  
  57.                 </tbody>  
  58.             </table>  
  59.         </div>  
  60.     </div>  
  61.     <script src="js/jquery-2.1.4.min.js"></script>  
  62.     <script src="js/bootstrap.min.js"></script>  
  63. </body>  
  64.   
  65. </html>  
Output: Responsive Table Layout



In this article we focused on Bootstrap Tables. Then in the next articles we will understand all the components of Bootstrap step by step.
 
Read more articles on Bootstrap:


Similar Articles