Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Table Utilities in Bootstrap 5

Table Utilities: Additional utility classes to customize table appearance and behavior beyond basic table classes.

Table Alignment Utilities

Control text alignment within table cells using Bootstrap's text alignment utilities.

Horizontal Alignment
Left AlignedCenter AlignedRight Aligned
Text aligned to leftCentered textRight aligned text
Product namesStatus badges$99.99
DescriptionsActionsNumbers, dates
Alignment Best Practices
  • Left align: Text, names, descriptions
  • Center align: Status indicators, actions
  • Right align: Numbers, currencies, dates
  • Consistency: Align similar data the same way
  • Headers: Match header alignment with data alignment
Code Example
<!-- Table alignment -->
            <table class="table">
            <thead>
                <tr>
                <th class="text-start">Left Header</th>
                <th class="text-center">Center Header</th>
                <th class="text-end">Right Header</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <td class="text-start">Left data</td>
                <td class="text-center">Center data</td>
                <td class="text-end">Right data</td>
                </tr>
            </tbody>
            </table>

Vertical Alignment

Vertical Cell Alignment
.align-top
Content aligned to top
.align-middle
Content vertically centered
.align-bottom
Content aligned to bottom
Use Cases
  • Align-top: Multiple lines of text
  • Align-middle: Icons with text, buttons
  • Align-bottom: Footer content, totals
  • Default: Bootstrap tables use top alignment
Practical Example
ProductImageDescriptionAction
Laptop ProLaptopHigh-performance laptop with 16GB RAM, 512GB SSD, and 15-inch display. Perfect for professionals and gamers alike.
<!-- Vertical alignment in practice -->
            <table class="table">
            <tbody>
                <tr>
                <td class="align-middle">Text with image</td>
                <td class="align-middle">
                    <img src="product.jpg" alt="Product">
                </td>
                <td class="align-top">
                    Multi-line description aligned to top
                </td>
                <td class="align-middle">
                    <button class="btn btn-primary">
                    Action button
                    </button>
                </td>
                </tr>
            </tbody>
            </table>

Table Width Utilities

Column Width Control
25% Width50% Width25% Width
Quarter width columnHalf width columnQuarter width column
Width Utility Classes
ClassWidthUse Case
.w-2525%Small columns, IDs, icons
.w-5050%Medium columns, names
.w-7575%Large columns, descriptions
.w-100100%Full width columns
.w-autoAutoNatural content width
Fixed Width Columns
Fixed 100pxFixed 150pxAuto Width
ID columnDate columnDescription column
<!-- Width control examples -->
            <table class="table">
            <thead>
                <tr>
                <!-- Percentage widths -->
                <th class="w-25">25% width</th>
                <th class="w-50">50% width</th>
                <th class="w-25">25% width</th>
                </tr>
                <tr>
                <!-- Fixed pixel widths -->
                <th style="width: 100px;">Fixed 100px</th>
                <th style="width: 150px;">Fixed 150px</th>
                <th class="w-auto">Auto width</th>
                </tr>
            </thead>
            </table>

Table Padding Utilities

Cell Padding Control
PaddingClassExample
Extra Small.p-1Tight padding
Small.p-2Compact padding
Medium.p-3Default padding
Large.p-4Spacious padding
Extra Large.p-5Very spacious
Note: Table cells already have built-in padding. Use these utilities to override or adjust specific cells.
Directional Padding
DirectionClassExample
Top Only.pt-3Top padding
Right Only.pe-3Right padding
Bottom Only.pb-3Bottom padding
Left Only.ps-3Left padding
Horizontal.px-3Left & right
Vertical.py-3Top & bottom
<!-- Directional padding in tables -->
            <table class="table">
            <tbody>
                <tr>
                <td class="pt-4">Extra top padding</td>
                <td class="pe-4">Extra right padding</td>
                <td class="pb-4">Extra bottom padding</td>
                <td class="ps-4">Extra left padding</td>
                </tr>
                <tr>
                <td class="px-4">Extra horizontal</td>
                <td class="py-4">Extra vertical</td>
                <td class="p-4">All directions</td>
                <td class="p-0">No padding</td>
                </tr>
            </tbody>
            </table>

Border Utilities for Tables

Custom Border Styling
Border UtilityDescriptionExample
.borderAdds border to all sidesBordered cell
.border-topTop border onlyTop border
.border-endRight border onlyRight border
.border-bottomBottom border onlyBottom border
.border-startLeft border onlyLeft border
.border-0Removes all bordersNo borders
.border-primaryPrimary color borderColored border
.border-22px border widthThicker border
Practical Border Example
Section 1Section 2Section 3
Column group 1Column group 2Column group 3
Total group 1Total group 2Total group 3
Border Color Variants
Primary
Success
Danger
Warning
Info
Dark
<!-- Border utilities in tables -->
            <table class="table">
            <tbody>
                <tr class="border-top border-3 border-primary">
                <td class="border-end border-2">Section divider</td>
                <td>Regular cell</td>
                </tr>
                <tr>
                <td class="border border-danger">Highlighted cell</td>
                <td class="border-start border-success">Success border</td>
                </tr>
            </tbody>
            </table>

Background Utilities for Tables

Cell Background Colors
ClassDescriptionExample
.bg-primaryPrimary backgroundPrimary
.bg-successSuccess backgroundSuccess
.bg-dangerDanger backgroundDanger
.bg-warningWarning backgroundWarning
.bg-infoInfo backgroundInfo
.bg-lightLight backgroundLight
.bg-darkDark backgroundDark
Background with Opacity
OpacityClassExample
100%.bg-primaryFull opacity
75%.bg-primary bg-opacity-7575% opacity
50%.bg-primary bg-opacity-5050% opacity
25%.bg-primary bg-opacity-2525% opacity
10%.bg-primary bg-opacity-1010% opacity
<!-- Background with opacity -->
            <table class="table">
            <tbody>
                <tr>
                <td class="bg-success bg-opacity-25">
                    Subtle success highlight
                </td>
                <td class="bg-warning bg-opacity-50">
                    Medium warning highlight
                </td>
                </tr>
                <tr>
                <td class="bg-danger bg-opacity-10">
                    Very subtle danger indicator
                </td>
                <td class="bg-info bg-opacity-75">
                    Strong info background
                </td>
                </tr>
            </tbody>
            </table>

Text Utilities for Tables

Text Styling in Tables
UtilityClassExample
Font Weight.fw-boldBold text
Font Weight.fw-semiboldSemibold text
Font Weight.fw-normalNormal weight
Font Weight.fw-lightLight text
Font Style.fst-italicItalic text
Font Style.fst-normalNormal style
Text Decoration.text-decoration-underlineUnderlined
Text Decoration.text-decoration-line-throughLine through
Text Transform.text-uppercaseUppercase text
Text Transform.text-lowercaseLOWERCASE TEXT
Text Transform.text-capitalizecapitalized text
Practical Text Styling
ProductStatusPriceDiscount
Laptop Proin stock$1,299$1,499
wireless mouselow stock$49No discount
Code Example
<!-- Text utilities in tables -->
            <table class="table">
            <tbody>
                <tr>
                <td class="fw-bold text-uppercase">
                    Important header
                </td>
                <td class="fst-italic text-muted">
                    Additional note
                </td>
                </tr>
                <tr>
                <td class="text-decoration-line-through">
                    Old price
                </td>
                <td class="fw-bold text-success">
                    New price
                </td>
                </tr>
                <tr>
                <td class="text-capitalize">
                    product name
                </td>
                <td class="fw-light">
                    Light description
                </td>
                </tr>
            </tbody>
            </table>

Display Utilities for Tables

Show/Hide Table Elements
Display ClassBehaviorExample
.d-noneHiddenThis is hidden
.d-table-cellDisplay as table cellTable cell
.d-md-table-cellTable cell on medium+Visible md+
.d-lg-noneHidden on large+Hidden lg+
Tip: Use .d-none .d-md-table-cell to hide cells on mobile and show them on tablet and desktop.
Responsive Display Example
ProductCategorySupplierPriceStockAction
Laptop ProElectronicsTechCorp$1,29915
<!-- Responsive display in tables -->
            <table class="table">
            <thead>
                <tr>
                <th>Always visible</th>
                <th class="d-none d-sm-table-cell">Visible sm+</th>
                <th class="d-none d-md-table-cell">Visible md+</th>
                <th class="d-none d-lg-table-cell">Visible lg+</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <td>Data</td>
                <td class="d-none d-sm-table-cell">Sm+ data</td>
                <td class="d-none d-md-table-cell">Md+ data</td>
                <td class="d-none d-lg-table-cell">Lg+ data</td>
                </tr>
            </tbody>
            </table>

Combining Utilities

Complex Utility Combinations
IDProduct DetailsStatusFinancials
P-1001
Laptop Pro
High-performance model
In Stock
$1,299
$1,499
P-1002
wireless mouse
Ergonomic design
Low Stock
$49
Regular price
Utility Combination Strategy
  • Start with structure: Basic table with data
  • Add alignment: Use text and vertical alignment
  • Apply styling: Add colors, borders, backgrounds
  • Enhance typography: Use font weights, styles
  • Make responsive: Add display utilities
  • Test thoroughly: Check all screen sizes
Code Snippet
<!-- Complex utility combination -->
            <table class="table table-bordered">
            <tbody>
                <tr class="border-top border-3">
                <td class="text-center align-middle 
                            fw-bold border-end">
                    <!-- Multiple utilities -->
                </td>
                <td>
                    <div class="fw-bold text-uppercase">
                    Product name
                    </div>
                    <div class="text-muted fst-italic">
                    Description
                    </div>
                </td>
                <td class="text-center align-middle">
                    <span class="badge bg-success 
                                text-uppercase fw-bold 
                                px-3 py-2">
                    Status
                    </span>
                </td>
                <td class="text-end align-middle">
                    <div class="fw-bold fs-5">$99</div>
                    <div class="text-decoration-line-through 
                                text-muted">
                    Old price
                    </div>
                </td>
                </tr>
            </tbody>
            </table>

Performance Considerations

Utility Class Performance
  • CSS specificity: Utility classes have low specificity
  • File size: Unused utilities increase CSS size
  • Render performance: Many classes can slow rendering
  • Mobile performance: Test on actual mobile devices
  • Purge CSS: Remove unused utilities in production
  • Class ordering: Order doesn't affect performance
  • Browser rendering: Different browsers handle utilities differently
Best Practices
  • ✅ Use utilities for one-off styling
  • ✅ Create custom CSS for repeated patterns
  • ✅ Limit the number of utility classes per element
  • ✅ Use responsive utilities strategically
  • ✅ Test performance with many table rows
  • ✅ Consider CSS custom properties for theming
  • ✅ Document complex utility combinations
  • ✅ Use build tools to remove unused CSS

Summary and Best Practices

Table Utilities Guidelines
  • ✅ Use alignment utilities for better data presentation
  • ✅ Apply width utilities to control column proportions
  • ✅ Use padding utilities for custom cell spacing
  • ✅ Apply border utilities for visual separation
  • ✅ Use background utilities for highlighting
  • ✅ Apply text utilities for better typography
  • ✅ Use display utilities for responsive behavior
  • ✅ Combine utilities thoughtfully for complex designs
When to Use Utilities vs Custom CSS
  • Use utilities: One-off styling, prototypes, simple adjustments
  • Use custom CSS: Repeated patterns, complex designs, performance-critical sections
  • Hybrid approach: Use utilities for layout, custom CSS for complex styling
  • Maintenance: Utilities are easier to maintain for simple changes
  • Performance: Too many utilities can impact performance