write code to produce following html table

How to Create an HTML Table
How to Create an HTML Table

Creating a table in HTML is very simple. First, you need to understand four basic HTML table elements.

<table> = Complete Table <tr> = Table Row <th> = Heading Cell <td> = Data Cell

1. The Most Basic HTML Table

Suppose you want to create this table:

Name Age City
Rahul 18 Delhi
Aman 19 Agra

The HTML Code

<table border=”1″> <tr> <th>Name</th> <th>Age</th> <th>City</th> </tr> <tr> <td>Rahul</td> <td>18</td> <td>Delhi</td> </tr> <tr> <td>Aman</td> <td>19</td> <td>Agra</td> </tr> </table>
<table> starts the complete table.
<tr> creates a table row.
<th> creates a heading cell.
<td> creates a normal data cell.

2. Understanding the Code Line by Line

Step 1: Start the Table

<table border=”1″>

This starts the HTML table.

border=”1″ gives the table a visible border.

Step 2: Start a Row

<tr>

This starts one table row.

TR = Table Row

Step 3: Create Heading Cells

<th>Name</th> <th>Age</th> <th>City</th>

These create three heading cells.

TH = Table Header

Step 4: End the Row

</tr>

This closes the current table row.

Step 5: Create Normal Data Cells

<tr> <td>Rahul</td> <td>18</td> <td>Delhi</td> </tr>

This creates one row containing three normal data cells.

TD = Table Data

3. The Easiest Way to Understand a Table

Suppose a table has 3 columns and 4 rows. Think about its structure like this:

TABLE ├── ROW 1 │ ├── CELL │ ├── CELL │ └── CELL │ ├── ROW 2 │ ├── CELL │ ├── CELL │ └── CELL │ ├── ROW 3 │ ├── CELL │ ├── CELL │ └── CELL │ └── ROW 4 ├── CELL ├── CELL └── CELL
Every <tr> = One Row

Inside each <tr>, every <td> or <th> represents one cell.

4. Table With Headings

If the first row contains headings, use <th>.

Student Marks
Rahul 85
Aman 90
<table border=”1″> <tr> <th>Student</th> <th>Marks</th> </tr> <tr> <td>Rahul</td> <td>85</td> </tr> <tr> <td>Aman</td> <td>90</td> </tr> </table>

5. Table With Only Normal Cells

If there are no headings, you can use <td> for all cells.

<table border=”1″> <tr> <td>Apple</td> <td>Red</td> </tr> <tr> <td>Banana</td> <td>Yellow</td> </tr> </table>
This creates a simple 2 × 2 table.
That means 2 rows and 2 columns.

6. When Do We Use colspan?

colspan is used when one cell needs to cover multiple columns horizontally.

Student Information
Name Marks
Rahul 80

Here, Student Information covers two columns.

<table border=”1″> <tr> <th colspan=”2″>Student Information</th> </tr> <tr> <th>Name</th> <th>Marks</th> </tr> <tr> <td>Rahul</td> <td>80</td> </tr> </table>
colspan=”2″ means that the cell occupies the space of 2 columns.

7. When Do We Use rowspan?

rowspan is used when one cell needs to cover multiple rows vertically.

Name Subject Marks
Rahul English 80
Maths 90
<table border=”1″> <tr> <th>Name</th> <th>Subject</th> <th>Marks</th> </tr> <tr> <td rowspan=”2″>Rahul</td> <td>English</td> <td>80</td> </tr> <tr> <td>Maths</td> <td>90</td> </tr> </table>
rowspan=”2″ means that the Rahul cell occupies 2 rows.

8. The Most Important Rule

colspan

Left → Right

One cell occupies multiple columns.

colspan → Columns → Horizontal

rowspan

Top → Bottom

One cell occupies multiple rows.

rowspan → Rows → Vertical

9. Easy Memory Trick

COLspan

Think: COL = Column

colspan → columns → horizontal

ROWspan

Think: ROW = Row

rowspan → rows → vertical

10. Complete Basic Formula for Creating a Table

Whenever you need to create an HTML table, start with this basic structure:

<table> <tr> <th>Heading</th> <th>Heading</th> </tr> <tr> <td>Data</td> <td>Data</td> </tr> </table>

Then add more rows and cells according to the table shown in the question.

11. Think of an HTML Table as Boxes

The easiest way to understand an HTML table is to imagine that you are creating boxes.

Box Box
Box Box
<table border=”1″> <tr> <td>Box</td> <td>Box</td> </tr> <tr> <td>Box</td> <td>Box</td> </tr> </table>
This has 2 rows × 2 columns.

12. Understanding colspan Visually

Box
Box Box

The top box occupies two columns.

<table border=”1″> <tr> <td colspan=”2″>Box</td> </tr> <tr> <td>Box</td> <td>Box</td> </tr> </table>
Use colspan=”2″ because the top cell covers two columns.

13. Understanding rowspan Visually

Box Box
Box

The left box occupies two rows.

<table border=”1″> <tr> <td rowspan=”2″>Box</td> <td>Box</td> </tr> <tr> <td>Box</td> </tr> </table>
Use rowspan=”2″ because the left cell covers two rows.

14. How to Solve “Write Code to Produce the Following HTML Table”

When you see this question in an examination:

“Write code to produce the following HTML table.”

Do not immediately start writing code. First examine the table carefully.

Step 1 — Count the Rows

Determine how many horizontal rows are present.

Step 2 — Count the Columns

Determine how many vertical columns are present.

Step 3 — Identify Headings

Determine which cells are headings.

Step 4 — Identify Normal Data

Determine which cells contain ordinary information.

Step 5 — Look for Merged Cells

Does any cell extend downward?

Downward → rowspan

Does any cell extend sideways?

Sideways → colspan

Step 6 — Check the Borders

If the question shows borders, make the table borders visible.

Step 7 — Write the HTML

Create the table, then create its rows, and finally create the heading and data cells.

15. Important Difference Between HTML Table Elements

Element Purpose
<table> Creates the complete table
<tr> Creates a table row
<th> Creates a heading cell
<td> Creates a normal data cell
rowspan Makes a cell span multiple rows
colspan Makes a cell span multiple columns

16. Common Mistakes

❌ Mistake 1 — Confusing rowspan and colspan

Remember:

Down = rowspan
Across = colspan

❌ Mistake 2 — Forgetting a Row

If the question contains four rows, your HTML structure should represent all four rows.

❌ Mistake 3 — Adding an Extra Cell After rowspan

If a cell already occupies two rows, do not create another cell for the same merged position in the next row.

❌ Mistake 4 — Adding Extra Cells After colspan

If one cell occupies three columns, those three columns have already been occupied by that cell.

❌ Mistake 5 — Not Looking at the Diagram Carefully

The most important part of these questions is understanding the structure of the given table.

17. The Complete Concept in One Sentence

An HTML table is made using <table>, rows are created with <tr>, heading cells with <th>, normal cells with <td>, vertical merging with rowspan, and horizontal merging with colspan.

18. Final Memory Trick

TABLE → ROW → CELL

<table> → Complete table

<tr> → Row

<th> → Heading

<td> → Data

rowspan → Down

colspan → Across

🧠 Final Exam Strategy

Whenever you see:

“Write code to produce the following HTML table”

Follow this order:

Look at the table → Count rows → Count columns → Identify headings → Identify cells → Find rowspan/colspan → Write the HTML structure.

Leave a Comment