Bird
Raised Fist0
Bootsrapmarkup~5 mins

Basic table styling in Bootsrap

Choose your learning style10 modes available
Introduction

Tables organize data in rows and columns. Styling tables makes them easier to read and nicer to look at.

You want to show a list of products with prices.
You need to display a schedule or timetable.
You want to compare features side by side.
You want to highlight important rows or columns in a table.
Syntax
Bootsrap
<table class="table">
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
    </tr>
  </tbody>
</table>

Add class="table" to your <table> tag to apply Bootstrap's basic styling.

You can add more classes like table-striped or table-bordered for extra styles.

Examples
This is a simple table with basic Bootstrap styling.
Bootsrap
<table class="table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Alice</td>
      <td>30</td>
    </tr>
    <tr>
      <td>Bob</td>
      <td>25</td>
    </tr>
  </tbody>
</table>
Adding table-striped adds alternating row colors for easier reading.
Bootsrap
<table class="table table-striped">
  <thead>
    <tr>
      <th>Product</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Book</td>
      <td>$10</td>
    </tr>
    <tr>
      <td>Pen</td>
      <td>$2</td>
    </tr>
  </tbody>
</table>
Adding table-bordered adds borders around cells.
Bootsrap
<table class="table table-bordered">
  <thead>
    <tr>
      <th>City</th>
      <th>Country</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Paris</td>
      <td>France</td>
    </tr>
    <tr>
      <td>Tokyo</td>
      <td>Japan</td>
    </tr>
  </tbody>
</table>
Sample Program

This example shows a table with Bootstrap's basic styling, striped rows, borders, and a dark header for better contrast.

Bootsrap
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Bootstrap Basic Table Styling</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>Basic Table Styling with Bootstrap</h1>
    <table class="table table-striped table-bordered">
      <thead class="table-dark">
        <tr>
          <th>Item</th>
          <th>Quantity</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Apples</td>
          <td>10</td>
          <td>$5</td>
        </tr>
        <tr>
          <td>Bananas</td>
          <td>6</td>
          <td>$3</td>
        </tr>
        <tr>
          <td>Cherries</td>
          <td>20</td>
          <td>$15</td>
        </tr>
      </tbody>
    </table>
  </main>
</body>
</html>
OutputSuccess
Important Notes

Use semantic tags like <thead> and <tbody> to organize your table content.

Bootstrap's table classes make tables responsive and accessible by default.

Try viewing your table on different screen sizes to see how Bootstrap helps with responsiveness.

Summary

Use class="table" to add basic Bootstrap styling to tables.

Add classes like table-striped or table-bordered for extra visual effects.

Tables help organize data clearly and Bootstrap makes them look good easily.

Practice

(1/5)
1. What does adding class="table" to a <table> element in Bootstrap do?
easy
A. Adds sorting functionality to the table columns
B. Makes the table responsive automatically
C. Applies basic Bootstrap styling to the table for better appearance
D. Changes the table background color to blue

Solution

  1. Step 1: Understand the role of class="table"

    This class applies Bootstrap's default styling to tables, improving their look with padding, borders, and font styles.
  2. Step 2: Identify what it does not do

    It does not make tables responsive, add sorting, or change background color by default.
  3. Final Answer:

    Applies basic Bootstrap styling to the table for better appearance -> Option C
  4. Quick Check:

    Bootstrap table class = basic styling [OK]
Hint: Remember: table class styles tables simply [OK]
Common Mistakes:
  • Thinking it makes tables responsive
  • Assuming it adds sorting features
  • Believing it changes background color automatically
2. Which of the following is the correct way to add striped rows to a Bootstrap table?
easy
A. <table class="table-striped-table">
B. <table class="table-striped">
C. <table class="striped">
D. <table class="table table-striped">

Solution

  1. Step 1: Understand Bootstrap table classes

    The base class table must always be included for Bootstrap table styling to work.
  2. Step 2: Add table-striped alongside table

    To get striped rows, both classes must be present: class="table table-striped".
  3. Final Answer:

    <table class="table table-striped"> -> Option D
  4. Quick Check:

    Base table + table-striped = striped rows [OK]
Hint: Always include table class first for Bootstrap tables [OK]
Common Mistakes:
  • Using only table-striped without table
  • Swapping class order incorrectly
  • Using wrong class names like striped
3. What will be the visual effect of this code snippet?
<table class="table table-bordered">
  <tr><td>Cell 1</td><td>Cell 2</td></tr>
</table>
medium
A. Table with no borders around cells
B. Table with borders around each cell
C. Table with striped rows
D. Table with hover effect on rows

Solution

  1. Step 1: Identify the class table-bordered

    This class adds borders around all table cells and the table itself.
  2. Step 2: Confirm base styling with table

    The table class applies Bootstrap's basic styling, so the table looks neat with borders.
  3. Final Answer:

    Table with borders around each cell -> Option B
  4. Quick Check:

    table-bordered = borders on cells [OK]
Hint: Borders come from table-bordered class [OK]
Common Mistakes:
  • Confusing table-bordered with table-striped
  • Expecting hover effect without table-hover
  • Ignoring the need for base table class
4. You wrote this code but the table has no Bootstrap styling:
<table class="table-striped">
  <tr><td>Data</td></tr>
</table>

What is the error?
medium
A. Missing table class alongside table-striped
B. Wrong tag used instead of <table>
C. Class name should be striped-table
D. Bootstrap CSS file is not linked

Solution

  1. Step 1: Check the classes on the table

    The class table-striped alone does not apply Bootstrap styling; it must be combined with table.
  2. Step 2: Confirm the correct class usage

    Correct usage is class="table table-striped" to get both base styling and striped rows.
  3. Final Answer:

    Missing table class alongside table-striped -> Option A
  4. Quick Check:

    Always include table class for Bootstrap tables [OK]
Hint: Always pair table-striped with table [OK]
Common Mistakes:
  • Using only table-striped without table
  • Assuming wrong class names
  • Not linking Bootstrap CSS (though not asked here)
5. You want a table with basic Bootstrap styling, striped rows, and hover effect on rows. Which is the correct class attribute?
hard
A. class="table table-striped table-hover"
B. class="table-striped table-hover"
C. class="table-hover table-striped"
D. class="table table-hover-striped"

Solution

  1. Step 1: Include the base table class

    This is required for any Bootstrap table styling to work.
  2. Step 2: Add table-striped and table-hover classes

    These add striped rows and hover effect respectively. Both must be separate classes.
  3. Step 3: Check class order and spelling

    Order does not matter but all classes must be spelled correctly and separated by spaces.
  4. Final Answer:

    class="table table-striped table-hover" -> Option A
  5. Quick Check:

    Base table + stripes + hover = class="table table-striped table-hover" [OK]
Hint: Combine all classes with spaces, always include table [OK]
Common Mistakes:
  • Omitting the base table class
  • Combining classes incorrectly like table-hover-striped
  • Wrong class order or missing spaces
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