Bird
Raised Fist0
Bootsrapmarkup~5 mins

Table color variants in Bootsrap

Choose your learning style10 modes available
Introduction

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.

You want to highlight different types of data in a table, like success or warning messages.
You need to make a table visually appealing and easier to scan.
You want to show status or categories with colors in a table.
You want to improve accessibility by using color to separate rows or columns.
You want to quickly show different states like active, danger, or info in a table.
Syntax
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.

Examples
This adds a blue background to the entire row.
Bootsrap
<tr class="table-primary">
  <td>Primary colored row</td>
</tr>
This colors only one cell with a yellow background.
Bootsrap
<td class="table-warning">Warning cell</td>
This colors the whole row green to show success.
Bootsrap
<tr class="table-success">
  <td>Success row</td>
</tr>
This colors the row red to indicate danger or error.
Bootsrap
<tr class="table-danger">
  <td>Danger row</td>
</tr>
Sample Program

This example shows a table with different colored rows and one colored cell using Bootstrap color variant classes.

Bootsrap
<!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>
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. Which Bootstrap class would you use to add a green background color to a table row to indicate success?
easy
A. table-success
B. table-danger
C. table-warning
D. table-info

Solution

  1. Step 1: Understand Bootstrap color classes

    Bootstrap uses specific classes to color table rows, such as table-success for green, table-danger for red, table-warning for yellow, and table-info for blue.
  2. Step 2: Match color to meaning

    Green color indicates success, so the correct class is table-success.
  3. Final Answer:

    table-success -> Option A
  4. Quick Check:

    Green background = table-success [OK]
Hint: Green means success, so use table-success class [OK]
Common Mistakes:
  • Using table-danger for success (red means danger)
  • Confusing table-warning with success
  • Applying class to <table> instead of <tr> or <td>
2. Which of the following is the correct way to apply a red background color to a single table cell using Bootstrap?
easy
A. <td class='danger'>Data</td>
B. <tr class='table-danger'><td>Data</td></tr>
C. <td class='bg-danger'>Data</td>
D. <td class='table-danger'>Data</td>

Solution

  1. Step 1: Identify correct Bootstrap class for table cell color

    Bootstrap uses table-danger to color table rows red. To color a single cell, the recommended class is bg-danger which applies a red background color to any element.
  2. Step 2: Apply class to the correct element

    To color a single cell, add class='bg-danger' directly to the <td> element.
  3. Final Answer:

    <td class='bg-danger'>Data</td> -> Option C
  4. Quick Check:

    Red cell = td with bg-danger [OK]
Hint: Use bg-danger on <td> for red cell background [OK]
Common Mistakes:
  • 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
3. What will be the background color of the second row in this table?
<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>
medium
A. Yellow background
B. Green background
C. Red background
D. No background color

Solution

  1. Step 1: Identify the class on the second row

    The second <tr> has the class table-warning.
  2. Step 2: Understand Bootstrap color meaning

    table-warning adds a yellow background color to the row.
  3. Final Answer:

    Yellow background -> Option A
  4. Quick Check:

    table-warning = yellow background [OK]
Hint: table-warning means yellow background color [OK]
Common Mistakes:
  • Confusing table-warning with table-success
  • Thinking no color applies without explicit style
  • Mixing up row and cell classes
4. Identify the error in this code snippet that tries to color a table row red:
<tr class='danger'>
  <td>Error Row</td>
</tr>
medium
A. Class name should be 'bg-danger' for table rows
B. Class should be 'table-danger' instead of 'danger'
C. Class should be applied to <td> not <tr>
D. No error, code is correct

Solution

  1. Step 1: Check Bootstrap class naming for table rows

    Bootstrap requires the prefix table- for table color variants, so danger alone is invalid.
  2. Step 2: Correct the class name

    The correct class to color a row red is table-danger.
  3. Final Answer:

    Class should be 'table-danger' instead of 'danger' -> Option B
  4. Quick Check:

    Table row red = table-danger class [OK]
Hint: Table color classes always start with 'table-' prefix [OK]
Common Mistakes:
  • Using 'danger' without 'table-' prefix
  • Applying class to <td> when <tr> is intended
  • Using 'bg-danger' which is for general backgrounds
5. You want to highlight only the first and last cells of a table row with different colors using Bootstrap. Which code snippet correctly applies a green background to the first cell and a blue background to the last cell?
hard
A. <tr class='table-success'><td>First</td><td>Middle</td><td class='table-info'>Last</td></tr>
B. <tr class='table-success table-info'><td>First</td><td>Middle</td><td>Last</td></tr>
C. <tr><td class='bg-success'>First</td><td>Middle</td><td class='bg-info'>Last</td></tr>
D. <tr><td class='table-success'>First</td><td>Middle</td><td class='table-info'>Last</td></tr>

Solution

  1. Step 1: Apply color classes to individual cells

    To color only specific cells, add table-success and table-info classes directly to the <td> elements.
  2. 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.
  3. Step 3: Use Bootstrap table color classes, not bg- classes for table variants

    Use table-success and table-info for table color variants, not bg-success or bg-info.
  4. Final Answer:

    <tr><td class='table-success'>First</td><td>Middle</td><td class='table-info'>Last</td></tr> -> Option D
  5. Quick Check:

    Color cells individually with table-success/info classes [OK]
Hint: Add table-success/info classes to individual <td> cells [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard