Table color variants help you make tables easier to read by adding background colors to rows or cells. This makes important data stand out and improves user experience.
Table color variants in Bootsrap
<table class="table"> <thead> <tr class="table-primary"> <th>Header</th> </tr> </thead> <tbody> <tr class="table-success"> <td>Success row</td> </tr> <tr class="table-danger"> <td>Danger row</td> </tr> </tbody> </table>
Use classes like table-primary, table-success, table-danger, etc. on <tr> or <td> to add colors.
These classes come from Bootstrap and add background colors with good contrast automatically.
<tr class="table-primary"> <td>Primary colored row</td> </tr>
<td class="table-warning">Warning cell</td>
<tr class="table-success"> <td>Success row</td> </tr>
<tr class="table-danger"> <td>Danger row</td> </tr>
This example shows a table with different colored rows and one colored cell using Bootstrap color variant classes.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Table Color Variants</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container mt-4"> <h1>Table Color Variants Example</h1> <table class="table"> <thead> <tr class="table-primary"> <th scope="col">Name</th> <th scope="col">Status</th> </tr> </thead> <tbody> <tr class="table-success"> <td>John</td> <td>Active</td> </tr> <tr class="table-warning"> <td>Mary</td> <td>Pending</td> </tr> <tr class="table-danger"> <td>Steve</td> <td>Inactive</td> </tr> <tr> <td>Anna</td> <td class="table-info">Info</td> </tr> </tbody> </table> </main> </body> </html>
Use semantic HTML like <th scope="col"> for headers to improve accessibility.
Color variants should not be the only way to convey meaning; combine with text or icons for clarity.
Bootstrap color classes automatically handle text color for good contrast.
Table color variants add background colors to rows or cells to highlight data.
Use Bootstrap classes like table-success, table-danger, etc. on <tr> or <td>.
They improve readability and user experience by visually grouping or emphasizing table data.
Practice
Solution
Step 1: Understand Bootstrap color classes
Bootstrap uses specific classes to color table rows, such astable-successfor green,table-dangerfor red,table-warningfor yellow, andtable-infofor blue.Step 2: Match color to meaning
Green color indicates success, so the correct class istable-success.Final Answer:
table-success -> Option AQuick Check:
Green background = table-success [OK]
- Using table-danger for success (red means danger)
- Confusing table-warning with success
- Applying class to <table> instead of <tr> or <td>
Solution
Step 1: Identify correct Bootstrap class for table cell color
Bootstrap usestable-dangerto color table rows red. To color a single cell, the recommended class isbg-dangerwhich applies a red background color to any element.Step 2: Apply class to the correct element
To color a single cell, addclass='bg-danger'directly to the<td>element.Final Answer:
<td class='bg-danger'>Data</td> -> Option CQuick Check:
Red cell = td with bg-danger [OK]
- Using class 'danger' without 'table-' prefix
- Applying class to <tr> when only one cell should be colored
- Using table-danger which is for rows, not cells
<table class='table'> <tr><td>Row 1</td></tr> <tr class='table-warning'><td>Row 2</td></tr> <tr><td>Row 3</td></tr> </table>
Solution
Step 1: Identify the class on the second row
The second<tr>has the classtable-warning.Step 2: Understand Bootstrap color meaning
table-warningadds a yellow background color to the row.Final Answer:
Yellow background -> Option AQuick Check:
table-warning = yellow background [OK]
- Confusing table-warning with table-success
- Thinking no color applies without explicit style
- Mixing up row and cell classes
<tr class='danger'> <td>Error Row</td> </tr>
Solution
Step 1: Check Bootstrap class naming for table rows
Bootstrap requires the prefixtable-for table color variants, sodangeralone is invalid.Step 2: Correct the class name
The correct class to color a row red istable-danger.Final Answer:
Class should be 'table-danger' instead of 'danger' -> Option BQuick Check:
Table row red = table-danger class [OK]
- Using 'danger' without 'table-' prefix
- Applying class to <td> when <tr> is intended
- Using 'bg-danger' which is for general backgrounds
Solution
Step 1: Apply color classes to individual cells
To color only specific cells, addtable-successandtable-infoclasses directly to the<td>elements.Step 2: Avoid applying multiple classes to the row
Applying multiple color classes to the<tr>will override or conflict, so color should be on cells.Step 3: Use Bootstrap table color classes, not bg- classes for table variants
Usetable-successandtable-infofor table color variants, notbg-successorbg-info.Final Answer:
<tr><td class='table-success'>First</td><td>Middle</td><td class='table-info'>Last</td></tr> -> Option DQuick Check:
Color cells individually with table-success/info classes [OK]
- Applying multiple color classes to <tr> causing conflicts
- Using bg-success/bg-info instead of table-success/table-info
- Coloring entire row instead of specific cells
Ready to prove it?
Pass the test and this pattern moves unknown
