Problem Statement
What does the colspan attribute do in a table cell?
Explanation
The colspan attribute makes a table cell span across multiple columns. When you set colspan to a number, that cell takes up that many columns worth of space. For example, colspan equals 2 means the cell spans two columns. This is useful when you need to merge cells horizontally, such as creating section headers that span multiple columns. When using colspan, remember that the total number of cells in each row should match. If one row has a cell with colspan equals 2, you need one fewer cell in that row. The remaining cells in the row will appear after the spanning cell. Colspan is commonly used for creating complex table layouts, grouping related columns under a common header, or creating summary rows.
Code Solution
SolutionRead Only
<!-- Cell spanning 2 columns -->
<table border="1">
<tr>
<th colspan="2">Full Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>25</td>
</tr>
</table>
<!-- Header spanning all columns -->
<table border="1">
<tr>
<th colspan="3">Employee Information</th>
</tr>
<tr>
<th>Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
<tr>
<td>Alice</td>
<td>HR</td>
<td>$60,000</td>
</tr>
</table>