Bootstrap - Tables: A Friendly Guide for Beginners
Hello there, future web developers! Today, we're going to dive into the wonderful world of Bootstrap tables. Don't worry if you've never coded before – I'll be your friendly guide on this journey, and we'll take it step by step. By the end of this tutorial, you'll be creating beautiful, responsive tables like a pro!
What are Bootstrap Tables?
Before we jump in, let's talk about what Bootstrap tables are. Imagine you're organizing your favorite books on a bookshelf. Bootstrap tables are like that bookshelf, but for your website! They help you present information in a neat, organized way that's easy for your visitors to read and understand.
Simple Table
Let's start with the basics. Here's how you create a simple Bootstrap table:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>25</td>
<td>New York</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>30</td>
<td>London</td>
</tr>
</tbody>
</table>
This code creates a table with three columns: Name, Age, and City. The <thead>
section defines the table headers, and the <tbody>
contains the actual data. The class="table"
is what gives it that Bootstrap magic!
Table Variants
Bootstrap offers different styles for your tables. It's like choosing different outfits for your data! Here are some variants:
<table class="table table-primary">...</table>
<table class="table table-secondary">...</table>
<table class="table table-success">...</table>
<table class="table table-danger">...</table>
<table class="table table-warning">...</table>
<table class="table table-info">...</table>
<table class="table table-light">...</table>
<table class="table table-dark">...</table>
Just add these classes to your <table>
tag to change the overall look of your table. It's that easy!
Accented Tables
Want to make certain rows or cells stand out? Bootstrap's got you covered:
<table class="table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr class="table-active">
<td>Active row</td>
<td>Stands out</td>
</tr>
<tr>
<td>Regular row</td>
<td class="table-primary">Accented cell</td>
</tr>
</tbody>
</table>
The table-active
class highlights an entire row, while classes like table-primary
can be used on individual cells.
Striped Rows and Columns
Remember those zebra-striped notebooks you had in school? You can do the same with your tables!
For striped rows:
<table class="table table-striped">
...
</table>
And for striped columns:
<table class="table table-striped-columns">
...
</table>
Hoverable Rows
Want your table to be interactive? Make the rows light up when you hover over them:
<table class="table table-hover">
...
</table>
It's like magic – try it out!
Bordered and Borderless Tables
You can add borders to your table:
<table class="table table-bordered">
...
</table>
Or remove them entirely:
<table class="table table-borderless">
...
</table>
Small Tables
For a more compact look, use the table-sm
class:
<table class="table table-sm">
...
</table>
It's perfect for when you need to fit a lot of data in a small space!
Table Group Dividers
Want to group your data? Use the table-group-divider
class:
<table class="table">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
<tbody class="table-group-divider">
<tr>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</tbody>
</table>
This adds a visible divider between your table body groups.
Vertical Alignment
You can control how your content aligns vertically within cells:
<table class="table table-sm table-bordered">
<tbody>
<tr class="align-bottom">
<td>This cell and its neighbors are bottom-aligned</td>
<td>Bottom</td>
<td class="align-top">This cell is top-aligned</td>
</tr>
</tbody>
</table>
Use align-middle
for middle alignment and align-top
for top alignment.
Responsive Tables
Make your tables look good on all devices with the table-responsive
class:
<div class="table-responsive">
<table class="table">
...
</table>
</div>
This allows your table to scroll horizontally on smaller screens. It's like giving your table superpowers to adapt to any screen size!
Nesting
You can even put tables inside tables! Here's how:
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>Parent row</td>
<td>
<table class="table table-sm">
<tr>
<td>Nested row 1</td>
</tr>
<tr>
<td>Nested row 2</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
It's like Russian nesting dolls, but with tables!
Anatomy of a Table
Let's break down the parts of a table:
Table Head
<table class="table">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
...
</table>
The <thead>
contains your column headers.
Table Foot
<table class="table">
...
<tfoot>
<tr>
<td>Footer 1</td>
<td>Footer 2</td>
</tr>
</tfoot>
</table>
Use <tfoot>
for table footers, great for summaries or totals.
Caption
Add a title to your table with <caption>
:
<table class="table">
<caption>List of users</caption>
...
</table>
It's like giving your table a name tag!
Conclusion
And there you have it, folks! You've just learned how to create and style tables using Bootstrap. Remember, practice makes perfect, so don't be afraid to experiment with different combinations of these classes. Before you know it, you'll be creating tables that not only organize data effectively but also look fantastic!
Happy coding, and may your tables always be beautifully bootstrapped!
Feature | Class | Description |
---|---|---|
Simple Table | table |
Basic Bootstrap table |
Variants |
table-primary , table-secondary , etc. |
Different color schemes |
Accented Tables |
table-active , table-primary , etc. |
Highlight specific rows or cells |
Striped Rows | table-striped |
Alternate row colors |
Striped Columns | table-striped-columns |
Alternate column colors |
Hoverable Rows | table-hover |
Highlight rows on hover |
Bordered Table | table-bordered |
Add borders to all sides of table and cells |
Borderless Table | table-borderless |
Remove all borders |
Small Table | table-sm |
Make tables more compact |
Table Group Dividers | table-group-divider |
Add a separator between table body groups |
Vertical Alignment |
align-top , align-middle , align-bottom
|
Control vertical alignment of cell content |
Responsive Table | table-responsive |
Make tables scroll horizontally on small devices |
Nesting | N/A | Tables can be nested within table cells |
Table Head | <thead> |
Define table header |
Table Foot | <tfoot> |
Define table footer |
Caption | <caption> |
Add a caption to the table |
Credits: Image by storyset