Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
HTML Table with Colspan
2.1.
HTML
2.2.
Example
2.3.
HTML
3.
HTML Table with Rowspan
3.1.
HTML
3.2.
Example
3.3.
HTML
4.
Frequently Asked Questions
4.1.
Can you use colspan & rowspan together in the same table?
4.2.
Is there a limit to the number of columns or rows a cell can span?
4.3.
Can you apply colspan or rowspan to the <table> or <tr> elements?
5.
Conclusion
Last Updated: Jul 10, 2024
Easy

HTML Table Rowspan and Colspan

Author Rinki Deka
0 upvote

Introduction

HTML tables are a fundamental part of web development. They allow you to organize & display data in a structured way on your web pages. Two important attributes that enhance the functionality of HTML tables are "colspan" & "rowspan". These attributes help you to merge cells horizontally or vertically, creating more complex table layouts. 

HTML Table Rowspan and Colspan

In this article, we will learn how to use colspan & rowspan in HTML tables, along with examples to understand their use.

HTML Table with Colspan

The "colspan" attribute in HTML tables allows you to merge cells horizontally. When you apply the colspan attribute to a table cell (<td> or <th>), it spans across multiple columns, effectively combining them into a single cell.

To use the colspan attribute, you simply need to add it to the opening tag of the cell & specify the number of columns you want the cell to span. For example, if you want a cell to span across two columns, you would use colspan="2".

For example : 

  • HTML

HTML

<table>

 <tr>

   <th>Name</th>

   <th colspan="2">Contact</th>

 </tr>

 <tr>

   <td>Rahul</td>

   <td>rahul@example.com</td>

   <td>123-456-7890</td>

 </tr>

 <tr>

   <td>Rinki</td>

   <td>rinki@example.com</td>

   <td>987-654-3210</td>

 </tr>

</table>


Output

Output

In this example, the second <th> element has a colspan="2" attribute, which means it spans across two columns. The "Contact" header effectively merges the email & phone number columns into a single cell.

When you use colspan, the merged cell takes up the space of the specified number of columns, & the remaining cells in the row adjust accordingly. It's important to ensure that the total number of cells in each row matches the number of columns in the table, taking into account any merged cells.

Example

Now, let's take a look at a more comprehensive example to understand how colspan can be used in real-world situations. 

Consider a table that displays a student's report card:

  • HTML

HTML

<table>

 <tr>

   <th>Subject</th>

   <th>Marks</th>

   <th>Grade</th>

 </tr>

 <tr>

   <td>Math</td>

   <td>85</td>

   <td>A</td>

 </tr>

 <tr>

   <td>Science</td>

   <td>90</td>

   <td>A+</td>

 </tr>

 <tr>

   <td>English</td>

   <td>80</td>

   <td>B+</td>

 </tr>

 <tr>

   <td colspan="2">Total Marks</td>

   <td>255</td>

 </tr>

 <tr>

   <td colspan="2">Average Grade</td>

   <td>A</td>

 </tr>

</table>


Output

Output

In this example, we have a table with five rows & three columns. The last two rows use the colspan attribute to merge the "Subject" & "Marks" columns.

The fourth row has <td colspan="2">Total Marks</td>, which combines the "Subject" & "Marks" columns into a single cell with the text "Total Marks". The total marks value is then displayed in the third column.

Similarly, the fifth row uses <td colspan="2">Average Grade</td> to merge the "Subject" & "Marks" columns, displaying the text "Average Grade". The average grade value is shown in the third column.

HTML Table with Rowspan

In addition to colspan, HTML tables also support the "rowspan" attribute, which allows you to merge cells vertically. When you apply the rowspan attribute to a table cell (<td> or <th>), it spans across multiple rows, effectively combining them into a single cell.

To use the rowspan attribute, you add it to the opening tag of the cell & specify the number of rows you want the cell to span. For example, if you want a cell to span across three rows, you would use rowspan="3".

For example : 

  • HTML

HTML

<table>

 <tr>

   <th>Name</th>

   <td>Harsh</td>

 </tr>

 <tr>

   <th rowspan="2">Contact</th>

   <td>harsh@example.com</td>

 </tr>

 <tr>

   <td>123-456-7890</td>

 </tr>

</table>


Output

In this example, the second <th> element has a rowspan="2" attribute, which means it spans across two rows. The "Contact" header effectively merges the email & phone number rows into a single cell.

When you use rowspan, the merged cell takes up the space of the specified number of rows, & the remaining cells in the column adjust accordingly. It's important to ensure that the total number of cells in each column matches the number of rows in the table, taking into account any merged cells.

Example

Now, let's see a practical example that will show the usage of rowspan in an HTML table. 

Consider a table that represents a simple schedule:

  • HTML

HTML

<table>

 <tr>

   <th>Time</th>

   <th>Monday</th>

   <th>Tuesday</th>

   <th>Wednesday</th>

 </tr>

 <tr>

   <td>9:00 AM</td>

   <td rowspan="2">Meeting</td>

   <td>Training</td>

   <td>Conference</td>

 </tr>

 <tr>

   <td>10:00 AM</td>

   <td>Break</td>

   <td>Meeting</td>

 </tr>

 <tr>

   <td>11:00 AM</td>

   <td>Lunch</td>

   <td>Lunch</td>

   <td>Lunch</td>

 </tr>

 <tr>

   <td>12:00 PM</td>

   <td>Presentation</td>

   <td rowspan="2">Workshop</td>

   <td>Discussion</td>

 </tr>

 <tr>

   <td>1:00 PM</td>

   <td>Break</td>

   <td>Break</td>

 </tr>

</table>


Output

Output

In this example, we have a table with five rows & four columns representing a schedule for Monday, Tuesday, & Wednesday.

The second row has <td rowspan="2">Meeting</td>, which merges the cells for the 9:00 AM & 10:00 AM time slots on Monday. This indicates that the meeting spans across two hours.

Similarly, in the fourth row, <td rowspan="2">Workshop</td> is used to merge the cells for the 12:00 PM & 1:00 PM time slots on Tuesday. This shows that the workshop extends across two hours.

Note: Rowspan & colspan can be combined within the same table to create even more complex & customized table structures based on your specific requirements.

Frequently Asked Questions

Can you use colspan & rowspan together in the same table?

Yes, you can use both colspan & rowspan attributes together in the same table to create complex cell merging configurations.

Is there a limit to the number of columns or rows a cell can span?

No, there is no specific limit. However, it's important to ensure that the table structure remains logical & visually coherent.

Can you apply colspan or rowspan to the <table> or <tr> elements?

No, colspan & rowspan attributes are only applicable to <td> & <th> elements within a table.

Conclusion

In this article, we learned about the colspan & rowspan attributes in HTML tables. Colspan allows you to merge cells horizontally, while rowspan enables you to merge cells vertically. With the help of these attributes, you can create more complex & visually appealing table layouts. Apart from these, we saw practical examples that demonstrated how colspan & rowspan can be used to organize & present data effectively. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass