Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Table Colors in Bootstrap 5

Table Colors: Use contextual color classes to style table rows and cells with semantic meaning.

Contextual Table Classes

Bootstrap provides contextual classes to color table rows and cells based on their meaning or status.

All Table Color Variants
ClassDescriptionUsageExample
.table-primaryPrimary action or highlightSelected items, main actionsPrimary row
.table-secondarySecondary contentLess important itemsSecondary row
.table-successSuccessful or positive actionCompleted tasks, success statesSuccess row
.table-dangerDangerous or negative actionErrors, warnings, deletionsDanger row
.table-warningWarning or caution neededPending items, warningsWarning row
.table-infoInformational contentNotes, information, detailsInfo row
.table-lightLight background variantSubtle highlightsLight row
.table-darkDark background variantImportant items, headersDark row
<!-- Contextual table rows -->
            <table class="table">
            <tbody>
                <tr class="table-primary">
                <td>Primary</td>
                <td>Highlighted content</td>
                </tr>
                <tr class="table-success">
                <td>Success</td>
                <td>Completed action</td>
                </tr>
                <tr class="table-danger">
                <td>Danger</td>
                <td>Error or issue</td>
                </tr>
                <tr class="table-warning">
                <td>Warning</td>
                <td>Needs attention</td>
                </tr>
                <tr class="table-info">
                <td>Info</td>
                <td>Informational</td>
                </tr>
            </tbody>
            </table>

Table Header Colors

Colored Table Headers
#ProductPriceStock
1Laptop$99915
2Mouse$4942
#ProductPriceStock
1Keyboard$12928
2Monitor$29912
<!-- Colored table headers -->
            <table class="table">
            <thead class="table-primary">
                <tr>
                <th>#</th>
                <th>Product</th>
                </tr>
            </thead>
            <!-- body -->
            </table>

            <table class="table">
            <thead class="table-dark">
                <tr>
                <th>#</th>
                <th>Product</th>
                </tr>
            </thead>
            <!-- body -->
            </table>
All Header Colors
Primary Header
HeaderPrimary
Success Header
HeaderSuccess
Danger Header
HeaderDanger
Warning Header
HeaderWarning
Info Header
HeaderInfo

Individual Cell Coloring

Color Individual Cells

Apply color classes directly to <td> or <th> elements for granular control.

MetricQ1Q2Q3Q4Total
Revenue$125,000$135,000$110,000$95,000$465,000
Expenses$85,000$75,000$65,000$60,000$285,000
Profit$40,000$60,000$45,000$35,000$180,000
Color Coding System
  • Green: Positive performance
  • Yellow: Moderate performance
  • Red: Negative performance
  • Blue: Totals/Summaries
  • Light Blue: Information
Code Example
<!-- Individual cell coloring -->
            <table class="table">
            <tbody>
                <tr>
                <td>Normal Cell</td>
                <td class="table-success">Success Cell</td>
                <td class="table-warning">Warning Cell</td>
                <td class="table-danger">Danger Cell</td>
                </tr>
                <tr>
                <td class="table-primary">Primary Cell</td>
                <td class="table-info">Info Cell</td>
                <td class="table-light">Light Cell</td>
                <td class="table-dark text-white">Dark Cell</td>
                </tr>
            </tbody>
            </table>

Striped Tables with Colors

Colored Striped Tables
#FeatureStatus
1User AuthenticationActive
2Payment ProcessingActive
3Email NotificationsPending
4Analytics DashboardActive
5Mobile AppIn Development
<!-- Striped table with color -->
            <table class="table table-striped table-primary">
            <thead>
                <tr>
                <th>#</th>
                <th>Feature</th>
                </tr>
            </thead>
            <tbody>
                <!-- Striped rows with primary color -->
            </tbody>
            </table>
All Color Variants
Primary
Striped
Success
Striped
Danger
Striped
Warning
Striped
Info
Striped
Dark
Striped
Note: Striped tables with colors create alternating light and dark shades of the same color for better readability.

Hover Effects with Colors

Colored Tables with Hover
#TaskAssigneeDue DateStatus
1Design ReviewJohn Smith2023-10-15Completed
2Code ImplementationJane Doe2023-10-20In Progress
3Testing PhaseBob Johnson2023-10-25Pending
4DocumentationAlice Brown2023-10-30Not Started
5DeploymentCharlie Wilson2023-11-05Scheduled
Combination Examples
<!-- Colored table with hover -->
            <table class="table table-hover table-primary">
            <!-- Hover effect on primary colored rows -->
            </table>

            <!-- Striped colored table with hover -->
            <table class="table table-striped table-hover table-success">
            <!-- Striped success with hover -->
            </table>

            <!-- Bordered colored table with hover -->
            <table class="table table-bordered table-hover table-warning">
            <!-- Bordered warning with hover -->
            </table>
Hover Behavior
  • Color intensity: Hover darkens the row color
  • Consistency: Works with all table color variants
  • Accessibility: Provides visual feedback
  • Customization: Can be customized with CSS
  • Performance: CSS-based, no JavaScript needed
  • Mobile friendly: Works on touch devices

Color Combinations and Patterns

Patterned Color Usage
PriorityBug IDDescriptionStatus
CriticalBUG-101Login not workingOpen
HighBUG-102Payment page slowIn Progress
MediumBUG-103Typo in FAQ pageResolved
LowBUG-104Profile picture uploadClosed
InfoBUG-105New feature requestReview
Color Pattern Meaning
CriticalHighMediumLowInfo
Status-Based Coloring
Order #CustomerAmountStatus
ORD-1001John Smith$299.99Delivered
ORD-1002Jane Doe$149.50Shipped
ORD-1003Bob Johnson$499.99Processing
ORD-1004Alice Brown$89.99Pending
ORD-1005Charlie Wilson$199.99Cancelled
Status Color Mapping
  • Delivered: Green
  • Shipped: Blue
  • Processing: Yellow
  • Pending: Light Blue
  • Cancelled: Red

Custom Table Colors

Custom Color Classes

Create custom table color classes using CSS or SCSS.

Custom Purple#6f42c1
Custom Pink#e83e8c
Custom Teal#20c997
Custom Orange#fd7e14
Custom Indigo#6610f2
Custom Cyan#17a2b8
CSS Custom Classes
/* Custom table color classes */
            .table-custom-purple {
            --bs-table-bg: #6f42c1;
            --bs-table-striped-bg: #5a36a0;
            --bs-table-striped-color: #fff;
            --bs-table-active-bg: #4d2e8a;
            --bs-table-active-color: #fff;
            --bs-table-hover-bg: #543596;
            --bs-table-hover-color: #fff;
            color: #fff;
            border-color: #5a36a0;
            }

            .custom-table-purple {
            background-color: #6f42c1;
            color: white;
            }

            .custom-table-purple:hover {
            background-color: #5a36a0;
            }

            /* Create multiple custom classes */
            .custom-table-teal {
            background-color: #20c997;
            color: white;
            }

            .custom-table-orange {
            background-color: #fd7e14;
            color: white;
            }
SCSS Customization
// SCSS: Extend table variants
            $custom-colors: (
            "purple": #6f42c1,
            "pink": #e83e8c,
            "teal": #20c997,
            "orange": #fd7e14,
            "indigo": #6610f2,
            "cyan": #17a2b8
            );

            @each $name, $color in $custom-colors {
            .table-#{$name} {
                --bs-table-bg: #{$color};
                --bs-table-striped-bg: #{darken($color, 10%)};
                --bs-table-striped-color: #fff;
                --bs-table-active-bg: #{darken($color, 15%)};
                --bs-table-active-color: #fff;
                --bs-table-hover-bg: #{darken($color, 5%)};
                --bs-table-hover-color: #fff;
                color: #fff;
                border-color: #{darken($color, 10%)};
            }
            }

            // Import Bootstrap
            @import "bootstrap/scss/bootstrap";

Accessibility Considerations

Color and Accessibility
WCAG Guidelines for Color
  • Contrast ratio: Minimum 4.5:1 for normal text
  • Color blindness: Don't rely solely on color
  • Alternative indicators: Use icons, text, or patterns
  • Text readability: Ensure text is readable on colored backgrounds
  • Testing: Test with accessibility tools
  • Focus states: Maintain visible focus indicators
Warning: Some color combinations (like yellow text on white) have poor contrast and should be avoided.
Accessible Color Examples
StatusGood ExamplePoor Example
Success✅ Dark text on light green❌ White text on dark green
Warning✅ Dark text on yellow❌ White text on yellow
Danger✅ Dark text on light red❌ White text on dark red
Info✅ Dark text on light blue❌ White text on dark blue
Tip: Use .table-* classes instead of .bg-* for better accessibility as they provide appropriate text colors.

Best Practices for Table Colors

Color Usage Guidelines
  • ✅ Use colors consistently throughout your application
  • ✅ Follow semantic meaning (green for success, red for errors)
  • ✅ Ensure sufficient contrast for accessibility
  • ✅ Don't use too many different colors in one table
  • ✅ Use colors to highlight, not decorate
  • ✅ Test color schemes in different lighting conditions
  • ✅ Consider color-blind users when choosing colors
When to Use Which Color
  • Primary: Main actions, selected items
  • Success: Completed tasks, positive results
  • Danger: Errors, warnings, deletions
  • Warning: Pending items, cautions
  • Info: Informational content, notes
  • Light/Dark: Visual separation, headers