Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Syntax and Parameters of HTML Rowspan
2.1.
Syntax
2.2.
Parameters and Attribute Value
3.
Use Cases of HTML Rowspan
4.
Example 1: Basic Row Spanning
4.1.
HTML
5.
Example 2: Complex Table with Row and Column Spanning
5.1.
HTML
6.
Frequently Asked Questions
6.1.
How to merge a row in HTML?
6.2.
What is Rowspan and Colspan in one tag?
6.3.
How to divide a row into two rows in HTML?
7.
Conclusion
Last Updated: Sep 21, 2024
Easy

HTML Rowspan

Author Gaurav Gandhi
0 upvote

Introduction 

HTML (HyperText Markup Language) is essential for creating and structuring websites. In the layout of web pages, tables are commonly used to organize data. The rowspan attribute in HTML is a significant feature for managing these tables. This attribute helps in altering the way tables are displayed by allowing cells to span across multiple rows. 

HTML Rowspan

Understanding and utilizing rowspan effectively can enhance the presentation of data in a web page, making it more readable and visually appealing. In this article, we will delve into the syntax, parameters, and use cases of the HTML rowspan attribute, including practical examples to demonstrate its application.

Syntax and Parameters of HTML Rowspan

Syntax

The rowspan attribute is used within a table cell element, like <td> or <th>. The basic syntax is as follows:

<td rowspan="number_of_rows">Cell Content</td>

Parameters and Attribute Value

Attribute Name: rowspan

Attribute Value: An integer specifying the number of rows a cell should span. The default value is 1, meaning no spanning occurs.

For instance, rowspan="3" makes the cell span across three rows.

Use Cases of HTML Rowspan

The rowspan attribute is particularly useful in various scenarios:

  • Creating Multi-Level Tables: In complex tables where data is categorized into different levels, rowspan helps in aligning related data vertically.
     
  • Improving Readability: It enhances table readability by minimizing repetition and allowing a more structured presentation of information.
     
  • Displaying Hierarchical Data: rowspan is ideal for displaying hierarchical data, where a single header needs to span across multiple rows of related information.

Example 1: Basic Row Spanning

Objective

To create a simple table where certain cells span multiple rows, demonstrating the basic functionality of rowspan.

Code

  • HTML

HTML

<!DOCTYPE html>

<html>

<head>

   <title>Rowspan Example</title>

   <style>

       table, th, td {

           border: 1px solid black;

           border-collapse: collapse;

       }

       th, td {

           padding: 10px;

           text-align: center;

       }

   </style>

</head>

<body>

   <table>

       <tr>

           <th>Day</th>

           <th>Meal</th>

           <th>Food</th>

       </tr>

       <tr>

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

           <td>Breakfast</td>

           <td>Oatmeal</td>

       </tr>

       <tr>

           <td>Lunch</td>

           <td>Pasta</td>

       </tr>

       <tr>

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

           <td>Breakfast</td>

           <td>Pancakes</td>

       </tr>

       <tr>

           <td>Lunch</td>

           <td>Sandwich</td>

       </tr>

   </table>

</body>

</html>

Output

Output

Explanation

This HTML document creates a table with three columns: Day, Meal, and Food.

The rowspan="2" attribute in the Day column allows 'Monday' and 'Tuesday' to span two rows each, aligning with their respective meals.

The table is styled for better visibility, with borders and centered text.

Example 2: Complex Table with Row and Column Spanning

Objective

To create a more complex table that combines both rowspan and colspan attributes for a comprehensive layout demonstration.

Code

  • HTML

HTML

<!DOCTYPE html>

<html>

<head>

   <title>Complex Table Example</title>

   <style>

       table, th, td {

           border: 1px solid black;

           border-collapse: collapse;

       }

       th, td {

           padding: 10px;

           text-align: center;

       }

   </style>

</head>

<body>

   <table>

       <tr>

           <th>Team</th>

           <th>Player</th>

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

       </tr>

       <tr>

           <td rowspan="2">Team A</td>

           <td>Player 1</td>

           <td>Goals: 4</td>

           <td>Assists: 7</td>

       </tr>

       <tr>

           <td>Player 2</td>

           <td>Goals: 6</td>

           <td>Assists: 5</td>

       </tr>

       <tr>

           <td rowspan="2">Team B</td>

           <td>Player 3</td>

           <td>Goals: 5</td>

           <td>Assists: 6</td>

       </tr>

       <tr>

           <td>Player 4</td>

           <td>Goals: 3</td>

           <td>Assists: 8</td>

       </tr>

   </table>

</body>

</html>

Output

Output

Explanation

This HTML document presents a more intricate table, detailing teams, players, and their statistics.

  • The rowspan="2" in the Team column allows each team name to cover two rows, one for each player.
     
  • The colspan="2" in the Statistics header cell spans it across two columns for goals and assists.

Frequently Asked Questions

How to merge a row in HTML?

To merge cells across a row in HTML, use the colspan attribute in a <td> or <th> tag.

What is Rowspan and Colspan in one tag?

Rowspan merges cells vertically across rows, while colspan merges horizontally across columns within a single HTML table.

How to divide a row into two rows in HTML?

To divide a row into two in HTML, simply add another <tr> element below the existing one in the table.

Conclusion

The rowspan attribute in HTML is a powerful tool for formatting tables, enabling cells to span multiple rows. This feature is essential for creating tables that are not only visually appealing but also logically structured, enhancing the presentation and readability of data. Through the examples and explanations provided, we've seen how rowspan can be applied in various scenarios, from simple to complex table layouts. Understanding and using rowspan effectively is a key skill in web development, contributing significantly to creating effective and accessible web content.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

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