Attributes

HTML <rowspan> Attribute

Learn about the rowspan attribute (in both tl;dr and normal format), including its definition, syntax, use-cases and plenty of examples to go along with it.

Josh Hartman
Josh Hartman
Last updated: Apr 14, 2024

Definition

The rowspan attribute in HTML is an attribute of the <td> (table data) or <th> (table header) elements that specifies the number of rows the cell should span. It controls the visual layout of a table by merging adjacent cells into a single, taller cell, increasing its height across multiple rows.

Syntax

When using with <td>:

HTML
<td rowspan="number_of_rows">

Or, when using with <th>:

HTML
<th rowspan="number_of_rows">

Browser Support

The rowspan attribute is supported in all modern browsers.

Chrome

Yes

Edge

Yes

Safari

Yes

Firefox

Yes

Opera

Yes

HTML tables are fundamental elements for presenting data in a structured format on web pages. While tables offer a straightforward grid-like layout, sometimes, cells need to span across multiple rows vertically to accommodate various data structures. This is where the rowspan attribute comes into play.

What is rowspan in HTML?

rowspan is an HTML attribute, specifically used for HTML tables, that allows you to make a cell span across multiple rows. In a standard table, each cell would typically be associated with one row. However, when using rowspan, the cell can merge vertically across multiple rows.

When a cell contains the rowspan attribute, it expands vertically to cover the specified number of rows. This effectively merges it with adjacent cells in the same column, creating a visually cohesive layout. The content of the cell spans the entire height, making it appear as if it occupies multiple rows.

Use Cases

  • Header Spanning: In tables with multi-level headers, rowspan is often used to create header cells that span multiple rows. This helps in categorizing and organizing data under broader headings.
  • Data Consolidation: When presenting summarized data or grouped information, rowspan can be used to merge cells across rows, providing a clear representation of the relationships between different data sets.
  • Design Flexibility: rowspan offers designers the flexibility to create visually appealing layouts by merging cells to accommodate varying content lengths or to emphasize specific data points.

Best Practices

  • Semantic Structure: Use rowspan judiciously to maintain the semantic structure of your tables. Ensure that merged cells logically represent related data and avoid excessive merging that could obscure the meaning of the table.
  • Accessibility: Ensure that tables with rowspan are accessible to all users, including those who rely on screen readers. Provide alternative text or additional context to convey the merged cell's content accurately.
  • Responsive Design: Consider how rowspan affects the responsiveness of your table on different devices. Test the layout across various screen sizes to ensure optimal viewing and usability.
  • Consistency: Maintain consistency in your table layout by using rowspan in a predictable manner. Avoid mixing rowspan with colspan excessively, as it may lead to complex table structures that are difficult to understand and maintain.

By following these best practices and considering the appropriate use cases, you can effectively leverage the rowspan attribute to create clear, organized, and accessible HTML tables that enhance the presentation of your data.

How to Use rowspan in HTML

Header Spanning Multiple Rows

HTML
<table> <tr> <th rowspan="2">Employee Details</th> <th>Name</th> <th>Department</th> </tr> <tr> <td>John Doe</td> <td>Marketing</td> </tr> <tr> <th>Employee ID</th> <td>123</td> <td>456</td> </tr> </table>
Employee Details Name Department
John Doe Marketing
Employee ID 123 456

In this example, the "Employee Details" header spans across two rows to encompass both employee information and employee IDs.

Data Consolidation with Header

HTML
<table> <tr> <th>Name</th> <th colspan="2">Contact Information</th> </tr> <tr> <td rowspan="2">John Doe</td> <td>123-456-7890</td> <td>john@example.com</td> </tr> <tr> <td>987-654-3210</td> <td>jane@example.com</td> </tr> </table>
Name Contact Information
John Doe 123-456-7890 john@example.com
987-654-3210 jane@example.com

Here, the "Name" cell spans across two rows to consolidate contact information for both John Doe and Jane Doe.

Summary Row with Merged Cells

HTML
<table> <tr> <th>Month</th> <th>Sales</th> </tr> <tr> <td>January</td> <td>$10,000</td> </tr> <tr> <td rowspan="2">February-March</td> <td>$25,000</td> </tr> <tr> <td>$15,000</td> </tr> </table>
Month Sales
January $10,000
February-March $25,000
$15,000

In this example, the "February-March" cell spans across two rows to represent sales data for both February and March.

Table Header with Spanned Cell

HTML
<table> <tr> <th>ID</th> <th>Name</th> <th rowspan="2">Details</th> </tr> <tr> <td>1</td> <td>Product A</td> </tr> <tr> <td>2</td> <td>Product B</td> <td>Description: Product B is a premium offering.</td> </tr> </table>
ID Name Details
1 Product A
2 Product B Description: Product B is a premium offering.

Here, the "Details" cell spans across two rows to provide additional information about the products.

These examples illustrate the versatility and usefulness of the rowspan attribute in creating structured and informative HTML tables.

What's the Difference Between rowspan and colspan?

The rowspan and colspan attributes are both used in HTML tables to control the span of table cells, but they operate in different directions.

The rowspan attribute allows a cell to span vertically across multiple rows. When applied to a cell (<td> or <th>), it causes the cell to merge with adjacent cells in the same column, creating a taller cell that occupies multiple rows.

In contrast, the colspan attribute allows a cell to span horizontally across multiple columns. When applied to a cell, it merges with adjacent cells in the same row, creating a wider cell that occupies multiple columns.

Conclusion

The rowspan attribute is a powerful tool for enhancing the visual presentation and structure of HTML tables. By allowing cells to span multiple rows, it enables developers to create more dynamic and informative table layouts. When used thoughtfully and with best practices in mind, rowspan can greatly improve the usability and accessibility of tabular data on your website.

Looking to merge cells in the other direction? Check out the colspan attribute.

Copy