Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Basic Tables in Bootstrap 5

Basic Tables: Learn how to create and style basic tables with Bootstrap's default table classes.

Default Table

The most basic table styling in Bootstrap 5. Add the .table class to any <table> element.

Basic Table Example
#First NameLast NameUsername
1JohnDoe@johndoe
2JaneSmith@janesmith
3BobJohnson@bobjohnson
4AliceWilliams@alicew
5CharlieBrown@charlieb
<!-- Basic Bootstrap table -->
            <table class="table">
            <thead>
                <tr>
                <th>#</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Username</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <td>1</td>
                <td>John</td>
                <td>Doe</td>
                <td>@johndoe</td>
                </tr>
                <tr>
                <td>2</td>
                <td>Jane</td>
                <td>Smith</td>
                <td>@janesmith</td>
                </tr>
                <tr>
                <td>3</td>
                <td>Bob</td>
                <td>Johnson</td>
                <td>@bobjohnson</td>
                </tr>
            </tbody>
            </table>
Default Table Styling
  • Light gray borders between cells
  • Left-aligned text by default
  • Bold header text
  • Consistent vertical and horizontal padding
  • No outer borders by default
  • Responsive design friendly
CSS Properties Applied
.table {
            width: 100%;
            margin-bottom: 1rem;
            color: #212529;
            vertical-align: top;
            border-color: #dee2e6;
            }

            .table > :not(caption) > * > * {
            padding: 0.5rem 0.5rem;
            border-bottom-width: 1px;
            }

Table Head Options

Light Table Head
#ProductPrice
1Laptop$999
2Mouse$49
3Keyboard$129
<!-- Light table head -->
            <table class="table">
            <thead class="table-light">
                <tr>
                <th>#</th>
                <th>Product</th>
                </tr>
            </thead>
            <tbody>
                <!-- Table body -->
            </tbody>
            </table>
Dark Table Head
#ProductPrice
1Laptop$999
2Mouse$49
3Keyboard$129
<!-- Dark table head -->
            <table class="table">
            <thead class="table-dark">
                <tr>
                <th>#</th>
                <th>Product</th>
                </tr>
            </thead>
            <tbody>
                <!-- Table body -->
            </tbody>
            </table>

Table with Captions

Table Captions

A <caption> functions like a heading for a table. It helps screen readers and provides context.

List of employees and their departments
IDNameDepartmentJoin Date
101John SmithEngineering2022-01-15
102Jane DoeMarketing2021-08-22
103Bob JohnsonSales2023-03-10
Caption Placement Options
<!-- Caption at top (default) -->
            <table class="table">
            <caption>Top caption</caption>
            <!-- table content -->
            </table>

            <!-- Caption at bottom -->
            <table class="table caption-top">
            <caption>Top caption with class</caption>
            <!-- table content -->
            </table>

            <!-- Custom caption styling -->
            <caption class="text-center p-2 bg-light">
            Custom styled caption
            </caption>
Caption Best Practices
  • Be descriptive: Explain what the table contains
  • Keep it concise: Aim for 1-2 sentences
  • Use proper HTML: Always use <caption> tag
  • Accessibility: Required for screen readers
  • Positioning: Use .caption-top or .caption-bottom
  • Styling: Customize with Bootstrap utility classes

Table with Borders

Bordered Table
#ItemQuantityPrice
1Laptop1$999
2Mouse2$98
3Keyboard1$129
<!-- Bordered table -->
            <table class="table table-bordered">
            <thead>
                <tr>
                <th>#</th>
                <th>Item</th>
                </tr>
            </thead>
            <tbody>
                <!-- Table body -->
            </tbody>
            </table>
Borderless Table
#TaskStatusDue Date
1Design ReviewCompleted2023-10-15
2Code DeploymentIn Progress2023-10-20
3TestingPending2023-10-25
<!-- Borderless table -->
            <table class="table table-borderless">
            <thead>
                <tr>
                <th>#</th>
                <th>Task</th>
                </tr>
            </thead>
            <tbody>
                <!-- Table body -->
            </tbody>
            </table>

Table Alignment

Text Alignment in Tables
AlignmentClassDescriptionExample
Left (Default)text-startText aligned to the leftLeft aligned text
Centertext-centerText centered in cellCentered text
Righttext-endText aligned to the rightRight aligned text
Vertical Middlealign-middleVertical center alignmentMiddle aligned
Vertical Bottomalign-bottomBottom alignmentBottom aligned
Practical Example
ProductCategoryPriceStock
Laptop ProElectronics$1,299.0015
Wireless MouseAccessories$49.9942
Code Example
<!-- Table with aligned cells -->
            <table class="table">
            <thead>
                <tr>
                <th class="text-start">Product</th>
                <th class="text-center">Category</th>
                <th class="text-end">Price</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <td class="text-start">Laptop</td>
                <td class="text-center">Electronics</td>
                <td class="text-end">$999.00</td>
                </tr>
            </tbody>
            </table>

            <!-- Vertical alignment -->
            <tr>
            <td class="align-middle">Middle</td>
            <td class="align-top">Top</td>
            <td class="align-bottom">Bottom</td>
            </tr>

Table Size Variations

Small Table
IDNameEmailRole
101John Smithjohn@example.comAdmin
102Jane Doejane@example.comUser
103Bob Johnsonbob@example.comEditor
<!-- Small table -->
            <table class="table table-sm">
            <thead>
                <tr>
                <th>ID</th>
                <th>Name</th>
                </tr>
            </thead>
            <tbody>
                <!-- Table body -->
            </tbody>
            </table>
Use Case: Perfect for dashboards and admin panels where space is limited.
Default vs Small Comparison
PropertyDefault TableSmall Table (.table-sm)
Cell Padding (Vertical)0.5rem0.25rem
Cell Padding (Horizontal)0.5rem0.3rem
Font Size1rem0.875rem
Header Font Size1rem0.875rem
Border Width1px1px
Use CaseGeneral data displayCompact layouts, dashboards
Note: Small tables might reduce readability for some users. Consider accessibility when using .table-sm.

Table with Contextual Classes

Contextual Table Rows

Add contextual classes to table rows to indicate different states.

ClassDescriptionExample
.table-primaryPrimary action or highlightSelected item
.table-successSuccessful or positive actionCompleted task
.table-dangerDangerous or negative actionError or warning
.table-warningWarning or caution neededPending review
.table-infoInformational contentNote or information
.table-lightLight background variantSubtle highlight
.table-darkDark background variantImportant item
<!-- Contextual table rows -->
            <table class="table">
            <tbody>
                <tr class="table-primary">
                <td>Primary row</td>
                <td>Highlighted content</td>
                </tr>
                <tr class="table-success">
                <td>Success row</td>
                <td>Completed action</td>
                </tr>
                <tr class="table-danger">
                <td>Danger row</td>
                <td>Error or issue</td>
                </tr>
                <tr class="table-warning">
                <td>Warning row</td>
                <td>Needs attention</td>
                </tr>
                <tr class="table-info">
                <td>Info row</td>
                <td>Informational</td>
                </tr>
            </tbody>
            </table>

Table with Hover Effects

Hoverable Rows

Add .table-hover to enable a hover state on table rows.

#CustomerOrder DateAmountStatus
1001John Smith2023-10-15$299.99Delivered
1002Jane Doe2023-10-16$149.50Processing
1003Bob Johnson2023-10-17$499.99Shipped
1004Alice Brown2023-10-18$89.99Cancelled
1005Charlie Wilson2023-10-19$199.99Delivered
Hover Effect Details
  • Default color: Light gray background on hover
  • Works with: All table rows in <tbody>
  • Doesn't affect: Table header or footer rows
  • Can be combined: With other table classes
  • Accessibility: Provides visual feedback
  • Customization: Can be customized with CSS
Code Example
<!-- Hoverable table -->
            <table class="table table-hover">
            <thead>
                <tr>
                <th>#</th>
                <th>Name</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <td>1</td>
                <td>John Doe</td>
                </tr>
                <tr>
                <td>2</td>
                <td>Jane Smith</td>
                </tr>
            </tbody>
            </table>

            <!-- Hover with other classes -->
            <table class="table table-hover table-striped">
            <!-- Combined effects -->
            </table>

Basic Table Combinations

Common Combinations
#FeatureClasses Used
1Striped with borders.table-striped .table-bordered
2Hoverable with dark header.table-hover .table-dark
3Small bordered table.table-sm .table-bordered
4Striped hoverable.table-striped .table-hover
5All features combined.table-striped .table-bordered .table-hover .table-sm
Complete Example
<!-- Complete table example -->
            <div class="table-responsive">
            <table class="table table-striped table-bordered 
                            table-hover table-sm">
                
                <caption>Employee performance report</caption>
                
                <thead class="table-dark">
                <tr>
                    <th scope="col" class="text-center">ID</th>
                    <th scope="col" class="text-start">Name</th>
                    <th scope="col" class="text-center">Department</th>
                    <th scope="col" class="text-end">Salary</th>
                </tr>
                </thead>
                
                <tbody>
                <tr class="table-success">
                    <td class="text-center">101</td>
                    <td class="text-start">John Smith</td>
                    <td class="text-center">Engineering</td>
                    <td class="text-end">$85,000</td>
                </tr>
                <tr>
                    <td class="text-center">102</td>
                    <td class="text-start">Jane Doe</td>
                    <td class="text-center">Marketing</td>
                    <td class="text-end">$75,000</td>
                </tr>
                </tbody>
                
                <tfoot>
                <tr class="table-info">
                    <td colspan="3" class="text-end">
                    <strong>Average:</strong>
                    </td>
                    <td class="text-end">
                    <strong>$80,000</strong>
                    </td>
                </tr>
                </tfoot>
                
            </table>
            </div>

Best Practices for Basic Tables

Basic Table Guidelines
  • ✅ Always start with .table class
  • ✅ Use .table-responsive wrapper for mobile support
  • ✅ Include proper table structure (<thead>, <tbody>, <tfoot>)
  • ✅ Add <caption> for accessibility
  • ✅ Use scope attributes in header cells
  • ✅ Choose appropriate table style based on content
  • ✅ Test table on different screen sizes
When to Use Which Style
  • Default table: General data display
  • Striped table: Long lists for better readability
  • Bordered table: Financial data, forms
  • Hover table: Interactive data tables
  • Small table: Dashboards, compact spaces
  • Borderless table: Minimalist designs