Building a Simple HTML Table with Rows and Columns
📖 Scenario: You are creating a simple webpage to show a small table of fruits and their colors. This is like making a list on paper but using a table so it looks neat and organized.
🎯 Goal: Build a basic HTML table with a header row and two data rows. The table will have two columns: one for the fruit name and one for its color.
📋 What You'll Learn
Use semantic HTML table elements:
, , ,
,
, and
.
Create exactly two columns: 'Fruit' and 'Color'.
Add a header row with column titles.
Add two rows of fruit data: 'Apple' with color 'Red', and 'Banana' with color 'Yellow'.
Ensure the HTML is valid and renders correctly in a browser.
💡 Why This Matters
🌍 Real World
Tables are used everywhere on websites to show organized data like schedules, prices, or lists.
💼 Career
Knowing how to build and structure tables is a basic skill for web developers and content creators.
Progress0 / 4 steps
1
Create the basic table structure
Write the opening and closing <table> tags and inside them add a <thead> section with one row <tr>. Inside this row, add two header cells <th> with the exact text Fruit and Color.
HTML
Hint
Remember to use <th> tags inside the header row <tr> to create column titles.
2
Add the table body section
Below the <thead>, add a <tbody> section inside the <table> tags. Leave it empty for now.
HTML
Hint
The <tbody> tag groups the main rows of the table separate from the header.
3
Add the first data row for Apple
Inside the <tbody>, add a row <tr> with two cells <td>. The first cell should contain the exact text Apple and the second cell should contain Red.
HTML
Hint
Use <td> tags inside the row <tr> for each piece of data.
4
Add the second data row for Banana
Below the first data row inside <tbody>, add another row <tr> with two cells <td>. The first cell should contain Banana and the second cell should contain Yellow.
HTML
Hint
Remember to add the new row inside the <tbody> after the first row.
Practice
(1/5)
1. Which HTML tag is used to create a row in a table?
easy
A. <tr>
B. <td>
C. <th>
D. <table>
Solution
Step 1: Understand table structure
A table is made of rows and columns. Rows group cells horizontally.
Step 2: Identify the row tag
The <tr> tag defines a table row, while <td> and <th> define cells inside rows.
Final Answer:
<tr> -> Option A
Quick Check:
Row tag = <tr> [OK]
Hint: Rows use <tr>, cells use <td> or <th> [OK]
Common Mistakes:
Confusing <td> as row tag
Using <th> for rows instead of headers
Using <table> as row tag
2. Which of the following is the correct way to create a table header cell?
easy
A. <table>Header</table>
B. <td>Header</td>
C. <tr>Header</tr>
D. <th>Header</th>
Solution
Step 1: Know header cell tags
Table headers use <th> tags to label columns or rows.
Step 2: Check the options
Only <th>Header</th> uses <th> correctly for a header cell.
Final Answer:
<th>Header</th> -> Option D
Quick Check:
Headers use <th> tags [OK]
Hint: Headers always use <th>, not <td> [OK]
Common Mistakes:
Using <td> for headers
Using <tr> or <table> as cells
Missing closing tags
3. What will the following HTML code display in a browser?
The first <tr> is opened but not closed before the second <tr> starts.
Step 2: Confirm correct nesting
Each <tr> must have a matching closing </tr>. Here, the first row is missing its closing tag.
Final Answer:
Missing closing tag for the first <tr>. -> Option B
Quick Check:
Every
needs a closing
[OK]
Hint: Check every
has a matching
[OK]
Common Mistakes:
Ignoring missing closing tags
Confusing <td> and <th> errors
Assuming table tag is unclosed
5. You want to create a table with 3 columns: 'Product', 'Price', and 'Stock'. The first row should be headers. Which code snippet correctly creates this table structure?