📖 Scenario: You are building a webpage to display a list of products and their prices. The table should look good on all devices, including phones and tablets.
🎯 Goal: Create a responsive table using Bootstrap that adjusts nicely on smaller screens by allowing horizontal scrolling.
📋 What You'll Learn
Use Bootstrap 5 classes for styling and responsiveness
Create a table with headers: Product, Category, Price
Make the table horizontally scrollable on small screens
Use semantic HTML elements
Ensure accessibility with proper table markup
💡 Why This Matters
🌍 Real World
Responsive tables are essential for displaying data clearly on all devices, especially on mobile phones where screen width is limited.
💼 Career
Web developers often need to create tables that look good and work well on any screen size. Knowing how to use Bootstrap's responsive table features is a valuable skill.
Progress0 / 4 steps
1
Create the basic HTML table structure
Write the HTML code to create a <table> with the headers Product, Category, and Price. Add three rows with these exact entries: Apple, Fruit, $1 Carrot, Vegetable, $0.5 Milk, Dairy, $2. Use semantic tags <table>, <thead>, <tbody>, <tr>, <th>, and <td>.
Bootsrap
Hint
Use <table> with <thead> for headers and <tbody> for rows. Add three rows with the exact data.
2
Add Bootstrap classes for styling
Add the Bootstrap classes table and table-striped to the <table> element to style it with stripes and default Bootstrap table styles.
Bootsrap
Hint
Add the classes inside the <table> tag like class="table table-striped".
3
Make the table responsive
Wrap the <table> element inside a <div> with the Bootstrap class table-responsive to make the table horizontally scrollable on small screens.
Bootsrap
Hint
Wrap the entire <table> block inside a <div> with class table-responsive.
4
Add Bootstrap CSS link and viewport meta for responsiveness
Add the Bootstrap 5 CSS CDN link inside the <head> section and add the viewport meta tag <meta name="viewport" content="width=device-width, initial-scale=1"> to ensure proper scaling on mobile devices. Wrap the responsive table inside a <main> tag for semantic structure.
Bootsrap
Hint
Include the viewport meta tag and Bootstrap CSS link inside <head>. Wrap the table inside <main> in the <body>.
Practice
(1/5)
1. What is the main purpose of wrapping a table inside a div with the class table-responsive in Bootstrap?
easy
A. To center the table on the page
B. To add borders around the table cells
C. To change the table background color
D. To make the table horizontally scrollable on small screens
Solution
Step 1: Understand the role of table-responsive
The table-responsive class wraps the table and adds horizontal scrolling on smaller screens.
Step 2: Compare with other options
Adding borders or changing background color is done with other classes like table-bordered or CSS, not table-responsive.
Final Answer:
To make the table horizontally scrollable on small screens -> Option D
Quick Check:
Responsive tables scroll horizontally on small screens [OK]
2. Which of the following is the correct way to make a Bootstrap table responsive?
easy
A. <div class='table-responsive'><table class='table'>...</table></div>
B. <table class='table-responsive table'>...</table>
C. <div class='responsive-table'><table class='table'>...</table></div>
D. <table class='table' responsive>...</table>
Solution
Step 1: Recall Bootstrap responsive table syntax
Bootstrap requires wrapping the <table> inside a <div> with class table-responsive.
Step 2: Check each option
<div class='table-responsive'><table class='table'>...</table></div> correctly wraps the table inside div.table-responsive. The other options use incorrect class names or attributes.
Final Answer:
<div class='table-responsive'><table class='table'>...</table></div> -> Option A
Quick Check:
Wrap table in div.table-responsive [OK]
Hint: Wrap tables in div.table-responsive for responsiveness [OK]
Common Mistakes:
Adding table-responsive directly to table
Using wrong class names like responsive-table
Using attributes instead of classes
3. What will happen when the following code is viewed on a small mobile screen?
A. The table columns will stack vertically automatically
B. The table will scroll horizontally if it doesn't fit the screen
C. The table will be hidden on small screens
D. The table will overflow outside the screen without scroll
Solution
Step 1: Understand table-responsive behavior
The table-responsive wrapper adds horizontal scrolling on small screens if the table is wider than the viewport.
Step 2: Analyze other options
The table columns do not stack vertically automatically (that requires other techniques). The table is not hidden, and it won't overflow without scroll because of the wrapper.
Final Answer:
The table will scroll horizontally if it doesn't fit the screen -> Option B
Quick Check:
Responsive tables scroll horizontally on small devices [OK]
Hint: Responsive tables scroll horizontally on small screens [OK]
Common Mistakes:
Thinking columns stack vertically automatically
Assuming table hides on small screens
Ignoring horizontal scroll behavior
4. You wrote this code but the table does not scroll on small screens:
A. Add inline style overflow-x: scroll; to the table
B. Add class table-responsive directly to the table
C. Wrap the table inside a div with class table-responsive
D. Replace table-bordered with table-responsive
Solution
Step 1: Identify missing wrapper
The table is missing the required div.table-responsive wrapper that enables horizontal scrolling on small screens.
Step 2: Evaluate other options
Adding table-responsive class directly to table or replacing table-bordered is incorrect. Inline styles on table won't work properly for responsiveness.
Final Answer:
Wrap the table inside a div with class table-responsive -> Option C
Quick Check:
Wrap tables in div.table-responsive for scroll [OK]
Hint: Always wrap tables in div.table-responsive for scrolling [OK]
Common Mistakes:
Adding table-responsive class to table instead of wrapper
Removing other styling classes
Using inline styles on table instead of wrapper
5. You want to create a responsive table that has striped rows and borders, but only scrolls horizontally on screens smaller than medium size (less than 768px). Which Bootstrap class should you use to wrap the table?