Table Colspan & Rowspan In HTML

This post will discuss HTML Table Colspan & Rowspan with relevant examples . This post contains much information that you can use as a learning tool.

The colspan and rowspan attributes are used in HTML tables to merge multiple cells into a single cell either horizontally or vertically, respectively.

  • colspan attribute: This attribute specifies the number of columns that a cell should span. For example, if you set colspan=”2″ for a cell, it will span across two columns.
  • rowspan attribute: This attribute specifies the number of rows that a cell should span.

Note: There is no restriction on how many rows and columns cells can span over a HTML table.



NAME
APRIL
2023
FIESTA

HTML Table – Rowspan

By specifying the rowspan attribute, you can make a cell span across several rows:

Example: 

<!DOCTYPE html> <html> <body> <table> <tr> <th>Brand/Model</th> <th>Type</th> <th>Year</th> </tr> <tr> <td rowspan="2">Trek Emonda</td> <td>Road</td> <td>2021</td> </tr> <tr> <td>Race</td> <td>-</td> </tr> <tr> <td>Specialized Roubaix</td> <td>Road</td> <td>2022</td> </tr> <tr> <td>Giant Trance</td> <td>Mountain</td> <td>2021</td> </tr> </table> </body> </html>

IMPORTANT: Assigning a value to the rowspan attribute indicates the number of rows to span.

HTML Table – Colspan

HTML table colspan & rowspan enable you to have a cell span across a number of columns by specifying the colspan attribute:

Example: 

<!DOCTYPE html> <html> <body> <table> <tr> <thcolspan>Bike Information <th>Type</th> <th>Year</th> </thcolspan></tr> <tr> <td colspan="2">Trek Emonda</td> <td>Road</td> <td>2021</td> </tr> <tr> <td colspan="2">Specialized Roubaix</td> <td>Road</td> <td>2022</td> </tr> <tr> <td colspan="2">Giant Trance</td> <td>Mountain</td> <td>2021</td> </tr> </table> </body> </html>

IMPORTANT: Assigning a value to the colspan attribute indicates the number of columns to span.


Rowspan and colspan can be applied at the same time. Here is an example:

Example: 

<!DOCTYPE html> <html> <body> <table> <tr> <th>Company</th> <th>Industry</th> <th>Founded</th> <th>HQ</th> </tr> <tr> <td rowspan="2">Apple</td> <td colspan="2">Technology</td> <td>Cupertino, CA</td> </tr> <tr> <td>1976</td> <td>-</td> </tr> <tr> <td>Microsoft</td> <td>Technology</td> <td>1975</td> <td>Redmond, WA</td> </tr> <tr> <td>Amazon</td> <td>E-commerce</td> <td>1994</td> <td>Seattle, WA</td> </tr> </table> </body> </html>

To apply CSS styles to a table cell with rowspan and/or colspan attributes, you can use the <td> selector in your CSS to target the cells:

Example: 

<!DOCTYPE html> <html> <head> <style> td { background-color: lightblue; font-size: 20px; border: 1px solid black; } </style></head> <body> <table> <tr> <td rowspan="2">Cell with rowspan</td> <td>Normal cell</td> </tr> <tr> <td colspan="2">Cell with colspan</td> </tr> </table> </body> </html>

HTML Table Colspan & Rowspan Benefits

The colspan and rowspan attributes in HTML tables offer several benefits:

  • By using colspan and rowspan, you can merge cells and create more complex table layouts. This can be especially useful when working with large data sets or when displaying data that requires more complex formatting.
  • By merging cells with colspan and rowspan, you can create tables that are easier to read and understand. This is because you can group related data together and reduce the number of individual cells required to display information.
  • Using colspan and rowspan can help to simplify the code required to create tables with complex layouts. By reducing the number of cells needed, you can also reduce the amount of HTML code required, which can make your code easier to read and maintain.
  • By grouping related data with colspan and rowspan, you can make your table more accessible to users who rely on assistive technologies like screen readers. This is because it can help to reduce the amount of information that needs to be conveyed and make it easier for users to understand the relationships between different data points.
  • Merging cells with colspan and rowspan can also help to create more visually interesting and engaging tables. By reducing the number of cells and creating larger blocks of data, you can create more visual interest and make your table more visually appealing.
Do leave your reaction below as a token of appreciation or a feedback to bring improvements to this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *