The colspan attribute in the <td> tag is used to make a table cell span across multiple columns, allowing you to merge cells horizontally.
- Used to merge two or more columns into a single cell.
- Accepts a numeric value indicating the number of columns to span.
- Helps create flexible and structured table layouts.
- Commonly used in headers or grouped data rows.
- Works only with table cells (<td> or <th>).
Syntax
<td colspan="number"> Attribute Value
It contains the numeric value which specifies the number of columns a cell should span.
Example: Illustrates the use of colspan attribute in Table data Element.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>
HTML <td>colspan Attribute
</title>
<style>
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
padding: 6px;
}
</style>
</head>
<body>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h2>
HTML <td>colspan
Attribute
</h2>
<!--Driver Code Ends-->
<table>
<tr>
<th>Name</th>
<th>Expense</th>
</tr>
<tr>
<td>Alen</td>
<td>₹10</td>
</tr>
<tr>
<td>Nicol</td>
<td>₹8</td>
</tr>
<!-- The last row -->
<tr>
<!-- This td will span
two columns, that is
a single column will
take up the space of 2 -->
<td colspan="2">
Sum: ₹18
</td>
</tr>
</table>
</body>
<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->