Bootstrap provides a clean layout for building tables. Some of the table elements supported by Bootstrap are −
Sr.No. | Tag & Description |
---|---|
1 | <table> Wrapping element for displaying data in a tabular format |
2 | <thead> Container element for table header rows (<tr>) to label table columns. |
3 | <tbody> Container element for table rows (<tr>) in the body of the table. |
4 | <tr> Container element for a set of table cells (<td> or <th>) that appears on a single row. |
5 | <td> Default table cell. |
6 | <th> Special table cell for column (or row, depending on scope and placement) labels. Must be used within a <thead> |
7 | <caption> Description or summary of what the table holds. |
If you want a nice, basic table style with just some light padding and horizontal dividers, add the base class of .table to any table as shown in the following example −
<table class = "table"> <caption>Basic Table Layout</caption> <thead> <tr> <th>Name</th> <th>City</th> </tr> </thead> <tbody> <tr> <td>Tanmay</td> <td>Bangalore</td> </tr> <tr> <td>Sachin</td> <td>Mumbai</td> </tr> </tbody> </table>
Along with the base table markup and the .table class, there are a few additional classes that you can use to style the markup. Following sections will give you a glimpse of all these classes.
By adding the .table-striped class, you will get stripes on rows within the <tbody> as seen in the following example −
<table class = "table table-striped"> <caption>Striped Table Layout</caption> <thead> <tr> <th>Name</th> <th>City</th> <th>Pincode</th> </tr> </thead> <tbody> <tr> <td>Tanmay</td> <td>Bangalore</td> <td>560001</td> </tr> <tr> <td>Sachin</td> <td>Mumbai</td> <td>400003</td> </tr> <tr> <td>Uma</td> <td>Pune</td> <td>411027</td> </tr> </tbody> </table>
By adding the .table-bordered class, you will get borders surrounding every element and rounded corners around the entire table as seen in the following example −
<table class = "table table-bordered"> <caption>Bordered Table Layout</caption> <thead> <tr> <th>Name</th> <th>City</th> <th>Pincode</th> </tr> </thead> <tbody> <tr> <td>Tanmay</td> <td>Bangalore</td> <td>560001</td> </tr> <tr> <td>Sachin</td> <td>Mumbai</td> <td>400003</td> </tr> <tr> <td>Uma</td> <td>Pune</td> <td>411027</td> </tr> </tbody> </table>
By adding the .table-hover class, a light gray background will be added to rows while the cursor hovers over them, as seen in the following example −
<table class = "table table-hover"> <caption>Hover Table Layout</caption> <thead> <tr> <th>Name</th> <th>City</th> <th>Pincode</th> </tr> </thead> <tbody> <tr> <td>Tanmay</td> <td>Bangalore</td> <td>560001</td> </tr> <tr> <td>Sachin</td> <td>Mumbai</td> <td>400003</td> </tr> <tr> <td>Uma</td> <td>Pune</td> <td>411027</td> </tr> </tbody> </table>
By adding the .table-condensed class, row padding is cut in half to condense the table. as seen in the following example. This is useful if you want any denser information.
<table class = "table table-condensed"> <caption>Condensed Table Layout</caption> <thead> <tr> <th>Name</th> <th>City</th> <th>Pincode</th> </tr> </thead> <tbody> <tr> <td>Tanmay</td> <td>Bangalore</td> <td>560001</td> </tr> <tr> <td>Sachin</td> <td>Mumbai</td> <td>400003</td> </tr> <tr> <td>Uma</td> <td>Pune</td> <td>411027</td> </tr> </tbody> </table>
The Contextual classes shown in following table will allow you to change the background color of your table rows or individual cells.
Sr.No. | Class & Description |
---|---|
1 | .active Applies the hover color to a particular row or cell |
2 | .success Indicates a successful or positive action |
3 | .warning Indicates a warning that might need attention |
4 | .danger Indicates a dangerous or potentially negative action |
These classes can be applied to <tr>, <td> or <th>.
<table class = "table"> <caption>Contextual Table Layout</caption> <thead> <tr> <th>Product</th> <th>Payment Date</th> <th>Status</th> </tr> </thead> <tbody> <tr class = "active"> <td>Product1</td> <td>23/11/2013</td> <td>Pending</td> </tr> <tr class = "success"> <td>Product2</td> <td>10/11/2013</td> <td>Delivered</td> </tr> <tr class = "warning"> <td>Product3</td> <td>20/10/2013</td> <td>In Call to confirm</td> </tr> <tr class = "danger"> <td>Product4</td> <td>20/10/2013</td> <td>Declined</td> </tr> </tbody> </table>
By wrapping any .table in .table-responsive class, you will make the table scroll horizontally up to small devices (under 768px). When viewing on anything larger than 768px wide, you will not see any difference in these tables.
<div class = "table-responsive"> <table class = "table"> <caption>Responsive Table Layout</caption> <thead> <tr> <th>Product</th> <th>Payment Date</th> <th>Status</th> </tr> </thead> <tbody> <tr> <td>Product1</td> <td>23/11/2013</td> <td>Pending</td> </tr> <tr> <td>Product2</td> <td>10/11/2013</td> <td>Delivered</td> </tr> <tr> <td>Product3</td> <td>20/10/2013</td> <td>In Call to confirm</td> </tr> <tr> <td>Product4</td> <td>20/10/2013</td> <td>Declined</td> </tr> </tbody> </table> </div>