Friday, December 20, 2024

HTML Table Colspan & Rowspan

 In HTML, the colspan and rowspan attributes are used to control the number of columns and rows a cell should span, respectively, in a table. Here's how they work:

colspan Attribute

  • The colspan attribute is used to make a table cell span multiple columns.
  • It specifies how many columns a cell should cover.
  • The value of colspan is the number of columns to span.

rowspan Attribute

  • The rowspan attribute is used to make a table cell span multiple rows.
  • It specifies how many rows a cell should cover.
  • The value of rowspan is the number of rows to span.

Example of Using colspan and rowspan:

<table border="1">
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
  <tr>
    <td rowspan="2">Row 1, Column 1 (spans 2 rows)</td>
    <td colspan="2">Row 1, Column 2 and 3 (spans 2 columns)</td>
  </tr>
  <tr>
    <td>Row 2, Column 2</td>
    <td>Row 2, Column 3</td>
  </tr>
</table>

Explanation:

  1. In the first row, the second cell spans two columns using colspan="2".
  2. In the second row, the first cell spans two rows using rowspan="2".
  3. The table adjusts to combine cells where needed.

This results in a table with merged cells both horizontally and vertically.

No comments:

Post a Comment

How can you refer to a CSS file in a web page?

 To refer to a CSS file in a web page, you use the <link> element inside the <head> section of the HTML document. Here's t...