Bootstrap Tables

Bookmark
HTML table element can be nicely styled and make it responsive using Bootstrap. Tables are used to display information in rows and columns format.

Plain HTML tables does not have pleasing look, but Bootstrap will convert them to good looking blocks just easily.

Basic Bootstrap Table

All we need to do is just add a class "table" to the HTML table element and the rest will be taken care by bootstrap.

    <table class="table">
        <thead>
            <tr>
                <th>S.No.</th>
                <th>Name</th>
                <th>Type</th>
                <th>Scientific</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>Crocodile</td>
                <td>Reptile</td>
                <td>Crocodylinae</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Lion</td>
                <td>Animal</td>
                <td>Panthera leo</td>
            </tr>
            <tr>
                <td>3</td>
                <td>Seahorse</td>
                <td>Marine Creature</td>
                <td>Hippocampus</td>
            </tr>
        </tbody>
    </table>

Example Output

Bootstrap Table

Bootstrap Table Classes

Following are the available Bootstrap classes. "table" style along with other below styles can be added to style the tables.
Bootstrap Class How to Apply Description
.table <table class="table"> A basic bootstrap table.
.table-striped <table class="table table-striped"> Zebra-striping for tables with alternate row coloring.
.table-bordered <table class="table table-bordered"> To add border on all sides of the table.
.table-hover <table class="table table-hover"> On mouse over rows will be highlighted.
.table-condensed <table class="table table-condensed"> Reduces cellpadding into half and compacts the table.
.table-responsive <table class="table table-responsive"> Makes the tables responsive by adding horizontal scroll bar on smaller devices.

Contextual classes .active, .success, .info, .warning and .danger can be used to highlight a row or table cell.

Bootstrap Table Example

In the following bootstrap table we can see usage of multiple table styles and contextual classes to highlight rows.

    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>S.No.</th>
                <th>Name</th>
                <th>Type</th>
                <th>Scientific</th>
            </tr>
        </thead>
        <tbody>
            <tr class="info">
                <td>1</td>
                <td>Crocodile</td>
                <td>Reptile</td>
                <td>Crocodylinae</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Lion</td>
                <td>Animal</td>
                <td>Panthera leo</td>
            </tr>
            <tr class="success">
                <td>3</td>
                <td>Seahorse</td>
                <td>Marine Creature</td>
                <td>Hippocampus</td>
            </tr>
        </tbody>
    </table>

Example Output

Bootstrap Table Example

Exception Occured:

TypeErrorException
Messageerror_log(app.log): Failed to open stream: Permission denied
File/home/dh_czz6eb/tutorialwalk.com/common/error.php
Line36
Trace#0 (): log_error(2, 'error_log(app.log): Failed to open stream: Permission denied', '/home/dh_czz6eb/tutorialwalk.com/common/error.php', 36)
#1 /home/dh_czz6eb/tutorialwalk.com/common/error.php(36): error_log('22-12-2024 00:05:19 am (IST),/home/dh_czz6eb/tutorialwalk.com/common/common.php,193,0,ErrorException,Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter,/bootstrap/bootstrap-tables.html,#0 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parame...', '/home/dh_czz6eb...', 193)
#1 [internal function]: check_for_fatal()
#2 {main}
', 3, 'app.log')
#2 /home/dh_czz6eb/tutorialwalk.com/common/error.php(8): log_exception(ErrorException)
#3 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter', '/home/dh_czz6eb/tutorialwalk.com/common/common.php', 193)
#4 (): check_for_fatal()
REQUEST_URI/bootstrap/bootstrap-tables.html