HTML - rowspan Attribute



The rowspan is an HTML attribute that specifies the number of rows spanned by a cell or grid cell within a table or grid.

If a row spans two rows, it represents it will take up two rows in that table. It is similar to the merge cell in a spreadsheet program such as Excel. And it allows a single table cell to span the height of many cells or rows.

Following is the element where the rowspan attribute can be used −

  • <td>
  • <th>

Syntax

Following is the syntax of the rowspan attribute −

<th|td rowspan="2 | 3 | 4, …" >

Example

In the following example, we are demonstrating the usage of the rowspan within the <td> element to span the rows by 2.

<!DOCTYPE html>
<html>
<head>
   <style>
      table,
      th,
      td {
         border: 1px solid black;
      }
   </style>
</head>
<body>
   <h1>The td rowspan attribute</h1>
   <table>
      <tr>
         <th>Company</th>
         <th>Location</th>
         <th>Managment</th>
      </tr>
      <tr>
         <td>tutorialspoint</td>
         <td>hyderabad</td>
         <td rowspan="2">MD. Mohtashim</td>
      </tr>
      <tr>
         <td>tutorix</td>
         <td>Noida</td>
      </tr>
   </table>
</body>
</html>

On running the above code, it will generate an output consisting of the table filled with data displayed on the webpage.

Example

Considering the another scenario, where we are going to use the rowspan with the th element to span the rows by 2.

<!DOCTYPE html>
<html>
<head>
   <style>
      th {
         background-color: gray;
      }

      td {
         border: 1px solid black;
         background-color: pink;
      }
   </style>
</head>
<body>
   <h1>The th rowspan attribute</h1>
   <table>
      <tr>
         <th rowspan="2">Name of Student</th>
         <th rowspan="2">Company</th>
         <th rowspan="2">Location</th>
      </tr>
      <tr></tr>
      <tr>
         <td>Aman Kumar</td>
         <td>tutorialspoint</td>
         <td>Hyderabad</td>
      </tr>
      <tr>
         <td>Akash Kumar</td>
         <td>Ltts</td>
         <td>Mysore</td>
      </tr>
   </table>
</body>
</html>

When we run the above code, it will generate an output consisting of the table filled with data along with a CSS applied displayed on the webpage.

Example

Let's look into the following example, where we are going to use the rowspan with the th element to span the rows by 3.

<!DOCTYPE html>
<html>
<head>
   <style>
      th {
         background-color: gray;
      }

      td {
         border: 1px solid black;
         background-color: pink;
      }
   </style>
</head>
<body>
   <h1>The th rowspan attribute</h1>
   <table>
      <tr>
         <th rowspan="3">tutorialspoint</th>
         <th>Employees</th>
         <th>Designation</th>
      </tr>
      <tr>
         <td>Aman Kumar</td>
         <td>technical content writer</td>
      </tr>
      <tr>
         <td>Vivek Kumar</td>
         <td>Developer</td>
      </tr>
   </table>
</body>
</html>

On running the above code, the output window will pop up displaying the table on the webpage.

html_attributes_reference.htm
Advertisements