Tables organize data in rows and columns. Styling tables makes them easier to read and nicer to look at.
Basic table styling in 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.
<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>
table-striped adds alternating row colors for easier reading.<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>
table-bordered adds borders around cells.<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>
This example shows a table with Bootstrap's basic styling, striped rows, borders, and a dark header for better contrast.
<!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>
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.
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
class="table" to a <table> element in Bootstrap do?Solution
Step 1: Understand the role of
This class applies Bootstrap's default styling to tables, improving their look with padding, borders, and font styles.class="table"Step 2: Identify what it does not do
It does not make tables responsive, add sorting, or change background color by default.Final Answer:
Applies basic Bootstrap styling to the table for better appearance -> Option CQuick Check:
Bootstraptableclass = basic styling [OK]
table class styles tables simply [OK]- Thinking it makes tables responsive
- Assuming it adds sorting features
- Believing it changes background color automatically
Solution
Step 1: Understand Bootstrap table classes
The base classtablemust always be included for Bootstrap table styling to work.Step 2: Add
To get striped rows, both classes must be present:table-stripedalongsidetableclass="table table-striped".Final Answer:
<table class="table table-striped"> -> Option DQuick Check:
Basetable+table-striped= striped rows [OK]
table class first for Bootstrap tables [OK]- Using only
table-stripedwithouttable - Swapping class order incorrectly
- Using wrong class names like
striped
<table class="table table-bordered"> <tr><td>Cell 1</td><td>Cell 2</td></tr> </table>
Solution
Step 1: Identify the class
This class adds borders around all table cells and the table itself.table-borderedStep 2: Confirm base styling with
Thetabletableclass applies Bootstrap's basic styling, so the table looks neat with borders.Final Answer:
Table with borders around each cell -> Option BQuick Check:
table-bordered= borders on cells [OK]
table-bordered class [OK]- Confusing
table-borderedwithtable-striped - Expecting hover effect without
table-hover - Ignoring the need for base
tableclass
<table class="table-striped"> <tr><td>Data</td></tr> </table>
What is the error?
Solution
Step 1: Check the classes on the table
The classtable-stripedalone does not apply Bootstrap styling; it must be combined withtable.Step 2: Confirm the correct class usage
Correct usage isclass="table table-striped"to get both base styling and striped rows.Final Answer:
Missingtableclass alongsidetable-striped-> Option AQuick Check:
Always includetableclass for Bootstrap tables [OK]
table-striped with table [OK]- Using only
table-stripedwithouttable - Assuming wrong class names
- Not linking Bootstrap CSS (though not asked here)
Solution
Step 1: Include the base
This is required for any Bootstrap table styling to work.tableclassStep 2: Add
These add striped rows and hover effect respectively. Both must be separate classes.table-stripedandtable-hoverclassesStep 3: Check class order and spelling
Order does not matter but all classes must be spelled correctly and separated by spaces.Final Answer:
class="table table-striped table-hover" -> Option AQuick Check:
Basetable+ stripes + hover =class="table table-striped table-hover"[OK]
table [OK]- Omitting the base
tableclass - Combining classes incorrectly like
table-hover-striped - Wrong class order or missing spaces
Ready to prove it?
Pass the test and this pattern moves unknown
