HTML | Colspan Attribute - GeeksforGeeks

The colspan attribute in HTML specifies the number of columns a cell should span. It allows the single table cell to span the width of more than one cell or column. It provides the same functionality as “merge cell” in a spreadsheet program like Excel.

Note: colspan=”0″ tells the browser to span the cell to the last column of the column group (colgroup).

Syntax:

<td colspan = "value">table content...</td>

Note: It can be used with <td> and <th> elements in an HTML Table.

Attribute Values

  • number: It specifies how many columns a cell should cover. When set to colspan=”0″, it instructs the browser to expand the cell to the last column of the table section (thead, tbody, or tfoot).

Using with <td> tag:

The colpan attribute when used with <td> tag determines the number of columns a table cell should span.

Syntax:

<td colspan = "value">table content...</td> // The value specifies the number of columns that the cell fills and it must be integer

Example: In this article we will see the implementation of colspan attribute with td attribute with an example.

html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>HTML colspan Attribute</title> <style> table, th, td{ border:1pxsolidblack; border-collapse:collapse; padding:6px; text-align:center; } </style> </head> <body> <center> <h6>The HTML colspan Attribute</h6> <table> <tr> <th>Name</th> <th>Expense</th> </tr> <tr> <td>Arun</td> <td>$10</td> </tr> <tr> <td>Priya</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> </center> </body> </html>

Output:

htmlattribute

Output

Using with <th> tag:

The colspan attribute when used with <th> tag determines the number of header cells it should span.

Syntax:

<th colspan = "value">table content...</th> // The value specifies the number of columns that the cell fills and it must be integer

Example: The implementation of rowspan attribute with th attribute with an example.

html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>HTML colspan Attribute</title> <style> table, th, td{ border:1pxsolidblack; border-collapse:collapse; padding:6px; text-align:center; } </style> </head> <body> <center> <h6>HTML colspan Attribute</h6> <table> <tr> <th colspan="2">Expense</th> </tr> <tr> <td>Arun</td> <td>$10</td> </tr> <tr> <td>Priya</td> <td>$8</td> </tr> </table> </center> </body> </html>

Output:

htmlattri

Output

Supported Browsers

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

HTML colspan Attribute – FAQs

Can I use the colspan attribute with any HTML element?

No, the colspan attribute is specifically used with <td> and <th> elements within an HTML table to control the width of the cells across multiple columns.

What is the default value of the colspan attribute?

The default value of the colspan attribute is 1, meaning the cell spans only one column if the attribute is not specified.

Can the colspan attribute be used to merge multiple columns in a table?

Yes, the colspan attribute effectively merges or spans a table cell across multiple columns, creating a single cell that covers the specified number of columns.

How does the colspan attribute affect table layout?

The colspan attribute affects the layout by expanding a cell to occupy multiple columns, which can alter the table’s overall width and the alignment of other cells in the same row.

Can the colspan attribute value be set dynamically with JavaScript?

Yes, the colspan attribute can be dynamically set or changed using JavaScript, for example: document.getElementById(‘myCell’).setAttribute(‘colspan’, ‘3’);.

HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

V

Vishal Chaudhary 2 Follow Improve Previous Article HTML cols Attribute Next Article HTML content Attribute

Từ khóa » Html Td Colspan Style