Attributes

HTML <colspan> Attribute

Learn about the colspan attribute (in both tl;dr and normal format), including its definition, syntax, use-cases and plenty of examples to go along with it.

Josh Hartman
Josh Hartman
Last updated: Apr 14, 2024

Definition

The colspan attribute in HTML is an attribute of the <td> (table data) or <th> (table header) elements that specifies the number of columns the cell should span. It controls the visual layout of a table by merging adjacent cells into a single, wider cell, increasing its width across multiple columns.

Syntax

When using with <td>:

HTML
<td colspan="number_of_columns">

Or, when using with <th>:

HTML
<th colspan="number_of_columns">

Browser Support

The colspan attribute is supported in all modern browsers.

Chrome

Yes

Edge

Yes

Safari

Yes

Firefox

Yes

Opera

Yes

Tables help keep information organized on web pages. But sometimes, you need to make cells in a row stretch across more than one column. That's where HTML's colspan attribute comes in handy.

What is colspan in HTML?

Colspan is an HTML attribute, specifically used for HTML tables, that allows you to make a row or column span across multiple <td> or <th> cells. In a standard table, each cell would be associated with one row or column. However, when using colspan, the cells merge across multiple columns.

When a cell contains the colspan attribute, it expands horizontally to cover the specified number of columns. This effectively merges it with adjacent cells in the same row, creating a visually cohesive layout. The content of the cell spans the entire width, making it appear as if it occupies multiple columns.

Use Cases

  • Header Spanning: In tables with multi-level headers, colspan is often used to create header cells that span multiple columns. This helps in categorizing and organizing data under broader headings.
  • Data Consolidation: When presenting summarized data or grouped information, colspan can be used to merge cells across columns, providing a clear representation of the relationships between different data sets.
  • Design Flexibility: Colspan offers designers the flexibility to create visually appealing layouts by merging cells to accommodate varying content lengths or to emphasize specific data points.

Best Practices

  • Semantic Structure: Use colspan judiciously to maintain the semantic structure of your tables. Avoid excessive merging that could obscure the meaning of the data.
  • Accessibility: Ensure that tables with colspan are accessible to all users, including those who rely on screen readers. Provide alternative text or additional context to convey the merged cell's content accurately.
  • Responsive Design: Consider how colspan affects the responsiveness of your table on different devices. Test the layout across various screen sizes to ensure optimal viewing and usability.

How to Use colspan in HTML

Header Spanning Multiple Columns

HTML
<table> <tr> <th colspan="2">Employee Details</th> </tr> <tr> <th>Name</th> <th>Department</th> </tr> <tr> <td>John Doe</td> <td>Marketing</td> </tr> </table>
Employee Details
Name Department
John Doe Marketing

In this example, the "Employee Details" header spans across two columns to encompass the entire employee information section.

Data Consolidation with Header

HTML
<table> <tr> <th>Name</th> <th colspan="2">Contact Information</th> </tr> <tr> <td>John Doe</td> <td>123-456-7890</td> <td>john@example.com</td> </tr> </table>
Name Contact Information
John Doe 123-456-7890 john@example.com

Here, the "Contact Information" header spans across two columns to consolidate both phone number and email address under one heading.

Summary Row with Merged Cells

HTML
<table> <tr> <th>Month</th> <th colspan="2">Sales</th> </tr> <tr> <td>January</td> <td>$10,000</td> <td>$12,000</td> </tr> <tr> <td>February</td> <td colspan="2">$15,000</td> </tr> </table>
Month Sales
January $10,000 $12,000
February $15,000

In this example, the second row represents summary sales data for January and February, with the "Sales" header spanning across two columns for each month.

Table Header with Spanned Cell

HTML
<table> <tr> <th>Product ID</th> <th>Product Name</th> <th colspan="2">Price</th> </tr> <tr> <td>1</td> <td>Product A</td> <td>$10</td> <td>$15</td> </tr> </table>
Product ID Product Name Price
1 Product A $10 $15

Here, the "Price" header spans across two columns to accommodate both the regular price and discounted price for "Product A".

Conclusion

The colspan attribute is a powerful tool for enhancing the visual presentation and structure of HTML tables. By allowing cells to span multiple columns, it enables developers to create more dynamic and informative table layouts. When used thoughtfully and with best practices in mind, colspan can greatly improve the usability and accessibility of tabular data on your website.

Looking to merge cells in the other direction? Check out the rowspan attribute.

Copy
Webflow affiliate banner - design and develop at the same time
This website (and app) is built in Webflow. How?